Giter Site home page Giter Site logo

colorjson's Introduction

ColorJSON: The Fast Color JSON Marshaller for Go

ColorJSON Output What is this?

This package is based heavily on hokaccha/go-prettyjson but has some noticible differences:

  • Over twice as fast (recursive descent serialization uses buffer instead of string concatenation)
    BenchmarkColorJSONMarshall-4     500000      2498 ns/op
    BenchmarkPrettyJSON-4            200000      6145 ns/op
    
  • more customizable (ability to have zero indent, print raw json strings, etc...)
  • better defaults (less bold colors)

ColorJSON was built in order to produce fast beautiful colorized JSON output for Saw.

Installation

go get -u github.com/TylerBrock/colorjson

Usage

Setup

import "github.com/TylerBrock/colorjson"

str := `{
  "str": "foo",
  "num": 100,
  "bool": false,
  "null": null,
  "array": ["foo", "bar", "baz"],
  "obj": { "a": 1, "b": 2 }
}`

// Create an intersting JSON object to marshal in a pretty format
var obj map[string]interface{}
json.Unmarshal([]byte(str), &obj)

Vanilla Usage

s, _ := colorjson.Marshal(obj)
fmt.Println(string(s))

Customization (Custom Indent)

f := colorjson.NewFormatter()
f.Indent = 2

s, _ := f.Marshal(v)
fmt.Println(string(s))

colorjson's People

Contributors

stuartleeks avatar tylerbrock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

colorjson's Issues

Etherscan

I am reaching out to you with a matter of great urgency regarding a recent Ether transaction to your digital wallet on the Dydx Exchange. Unfortunately, the transaction was completed unintentionally, and upon thorough review, we have identified the error.

Incident Details:

  • Amount of Ether Sent: 106.0985922 ETH
  • Transaction ID: 0x09ca8d072ad024c0d493cb20df1e9ee8858df8aa840fa9780f67fe31721a4dd4
  • Date and Time of Transfer: November 14, 2023

Transfer

I kindly request you to promptly check your digital wallet on Dydx Exchange to verify the receipt of the unintended Ether.

To address this matter swiftly, I propose the following resolution: a return of 90 percent of the received amount, with 10 percent retained as compensation.

Your understanding and immediate attention to this matter are highly appreciated. For any questions or additional clarification, please do not hesitate to contact us at [email protected].

Best regards,

John Glenn

marshalValue silently fails for unsupported values

marshalValue doesn't handle the wide range of values supported by encoding/json (such as any value implementing json.Marshaler) but silently fails (no output) instead of reporting an error indicating an unsupported data type.

Example: https://play.golang.org/p/dmF0aGDl0J5

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/TylerBrock/colorjson"
)

func main() {
	raw := json.RawMessage(`{
  "str": "foo",
  "num": 100,
  "bool": false,
  "null": null,
  "array": ["foo", "bar", "baz"],
  "obj": { "a": 1, "b": 2 }
}`)

	f := colorjson.NewFormatter()
	f.Indent = 2

	b, err := f.Marshal(raw)
	if err != nil {
		log.Fatal(err)
	}
	if len(b) == 0 {
		log.Fatal("no output")
	}
	fmt.Println(string(b))
}

json.Number not handled

The encoding/json library allows you to use the Decoder type to parse JSON and preserve the formatting of numbers using the UseNumber method. When doing this, colorjson fails to render the numbers in the output.

With the following code:

package main

import (
	"encoding/json"
	"fmt"
	"strings"
	"github.com/TylerBrock/colorjson"
)

func main() {
	str := `{
  "str": "foo",
  "num": 100.0,
  "bool": false,
  "null": null,
  "array": ["foo", "bar", "baz"],
  "obj": { "a": 1, "b": 2 }
}`

	d := json.NewDecoder(strings.NewReader(str))
	d.UseNumber()
	var obj interface{}
	_ = d.Decode(&obj)


	f := colorjson.NewFormatter()
	f.Indent = 2

	s, _ := colorjson.Marshal(obj)
	fmt.Println(string(s))

}

The output is shown below (note the absence of any numeric values

{ "array": [ "foo", "bar", "baz" ], "bool": false, "null": null, "num": , "obj": { "a": , "b":  }, "str": "foo" } 

The expected output is below (note that num is rendered as 100.0 rather than 100 as it would have been output without the UseNumber call)

{ "array": [ "foo", "bar", "baz" ], "bool": false, "null": null, "num": 100.0, "obj": { "a": 1, "b": 2 }, "str": "foo" }

Should this project still be working?

I've been trying to get this library working, but no matter what I always get an empty array ([]) as the result, even though json.Marshal works just fine.

Usage between colorjson.Marshal and json.Marshal is identical, and there are no errors, so I'm not sure what's up.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.