Skip to content

Commit

Permalink
Add benchmark, examples, update README, fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerBrock committed May 27, 2018
1 parent 7bf832e commit 2d7f5c0
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 18 deletions.
56 changes: 39 additions & 17 deletions README.md
@@ -1,15 +1,20 @@
Color JSON Pretty Printer for Go
================================
ColorJSON: The Fast Color JSON Marshaller for Go
================================================
![ColorJSON Output](https://i.imgur.com/pLtCXhb.png)
What is this?
-------------

This package is based heavily on hokaccha/go-prettyjson but has some noticible differences:
- Over twice as fast (recursive descent serializer)
- 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
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](http://github.com/TylerBrock/saw).

Installation
------------

Expand All @@ -20,20 +25,37 @@ go get -u github.com/TylerBrock/colorjson
Usage
-----

Setup

```go
import "github.com/TylerBrock/colorjson"

v := map[string]interface{}{
"str": "foo",
"num": 100,
"bool": false,
"null": nil,
"array": []string{"foo", "bar", "baz"},
"map": map[string]interface{}{
"foo": "bar",
},
}

s, _ := colorjson.Marshal(v)
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

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

Customization (Custom Indent)
```go
f := colorjson.NewFormatter()
f.Indent = 2

s, _ := f.Marshal(v)
fmt.Println(string(s))
```
1 change: 0 additions & 1 deletion colorjson.go
Expand Up @@ -118,7 +118,6 @@ func (f *Formatter) marshalArray(a []interface{}, buf *bytes.Buffer, depth int)
return
}

f.writeIndent(buf, depth)
buf.WriteString(startArray)
f.writeObjSep(buf)

Expand Down
4 changes: 4 additions & 0 deletions colorjson_test.go
Expand Up @@ -8,6 +8,8 @@ func benchmarkMarshall(i int, b *testing.B) {
simpleMap := make(map[string]interface{})
simpleMap["a"] = 1
simpleMap["b"] = "bee"
simpleMap["c"] = [3]int{1,2,3}
simpleMap["d"] = [3]string{"one", "two", "three"}

// run the Fib function b.N times
for n := 0; n < b.N; n++ {
Expand All @@ -19,6 +21,8 @@ func benchmarkPrettyJSON(i int, b *testing.B) {
simpleMap := make(map[string]interface{})
simpleMap["a"] = 1
simpleMap["b"] = "bee"
simpleMap["c"] = [3]int{1,2,3}
simpleMap["d"] = [3]string{"one", "two", "three"}

// run the Fib function b.N times
for n := 0; n < b.N; n++ {
Expand Down
30 changes: 30 additions & 0 deletions examples/indent.go
@@ -0,0 +1,30 @@
package main

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

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

var obj map[string]interface{}
json.Unmarshal([]byte(str), &obj)

// Make a custom formatter with indent set
f := colorjson.NewFormatter()
f.Indent = 4

// Marshall the Colorized JSON
s, _ := f.Marshal(obj)
fmt.Println(string(s))
}

26 changes: 26 additions & 0 deletions examples/simple.go
@@ -0,0 +1,26 @@
package main

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

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

var obj map[string]interface{}
json.Unmarshal([]byte(str), &obj)

// Marshall the Colorized JSON
s, _ := colorjson.Marshal(obj)
fmt.Println(string(s))
}

0 comments on commit 2d7f5c0

Please sign in to comment.