Procedural JSON printer for Go with pretty and compact output formatting.
$ go get github.com/hayamiz/go-projson
Basic usage of go-projson
is:
- Create
JsonPrinter
object by callingprojson.NewPrinter()
function. - Put JSON elements (int, float, string, object, array) one by one with following APIs:
PutInt
,PutFloat
,PutString
... functions for putting JSON primitive data.BeginArray
,FinishArray
... functions for putting arrays. Elements of an array are constructed by projson API calls between correspondingBeginArray
andFinishArray
.BeginObject
,FinishObject
... functions for putting objects. Members of an object are constructed by projson API calls between correspondingBeginObject
andFinishObject
, and each member must be keyed by a precedingPutKey
API call.
- Get JSON output string with
String
function
package main
import (
projson "github.com/hayamiz/go-projson"
)
func main() {
printer := projson.NewPrinter()
// Building JSON output by calling Put*/Begin*/Finish* API functions
printer.BeginObject()
printer.PutKey("key1")
printer.PutInt(12345) // => "key1":12345
printer.PutKey("key2")
printer.BeginArray()
printer.PutInt(12)
printer.PutFloat(345.67)
printer.PutString("hello, go-projson")
printer.FinishArray() // => "key2":[12, 345.67, "hello, go-projson"]
printer.FinishObject()
if str, err := printer.String(); err != nil {
panic(err)
} else {
fmt.Println(str) // prints {"key1":12345,"key2":"key2":[12,345.67,"hello, go-projson"]}
}
}
printer := projson.NewPrinter()
printer.PutInt(42)
str, _ := printer.String() // => 42
printer := projson.NewPrinter()
printer.PutFloat(123.45)
str, _ := printer.String() // => 123.45
printer := projson.NewPrinter()
printer.PutString("hello, projson")
str, _ := printer.String() // => "hello, projson"
printer := projson.NewPrinter()
printer.BeginArray()
printer.PutInt(123)
printer.BeginArray()
printer.PutFloat(456.7)
printer.BeginArray()
printer.PutString("nest, nest, and nest")
printer.FinishArray()
printer.PutString("nest depth = two here")
printer.FinishArray()
printer.FinishArray()
str, _ := printer.String() // => [123,[456.7,["nest, nest and nest"],"nest depth = two here"]]
printer := projson.NewPrinter()
printer.BeginObject()
printer.PutKey("key1")
printer.BeginObject()
printer.PutKey("nested key1")
printer.PutInt(12345)
printer.PutKey("nested key2")
printer.BeginObject()
printer.PutKey("double nested key1")
printer.PutFloat(678.9)
printer.FinishObject()
printer.FinishObject()
printer.FinishObject()
str, _ := printer.String() // => {"key1":{"nested key1":12345,"nested key2":{"double nested key1":678.9}}}
printer := projson.NewPrinter()
// build JSON output here ...
str, _ := printer.String()
fmt.Println(str)
printer := projson.NewPrinter()
printer.SetStyle(projson.SmartStyle)
// build JSON output here ...
str, _ := printer.String()
fmt.Println(str)
printer := projson.NewPrinter()
printer.SetStyle(projson.SmartStyle)
printer.SetColor(true)
// build JSON output here ...
str, _ := printer.String()
fmt.Println(str)
MIT license
Yuto Hayamizu (hayamiz)