Write JSON to an io.Writer. Useful for simple cases where you want to avoid encoding/json or require greater control.
JsonWriter can serialize strings, ints (unsigned/signed, 8/16/32/64), floats, bools, nulls values and types which implement encoding/json.Marshaler.
buffer := new(bytes.Buffer)
writer := jsonwriter.New(buffer)
writer.RootObject(func(){
writer.KeyValue("id", "leto")
writer.KeyValue("age", 3000)
writer.KeyValue("god", true)
writer.Object("sister", func(){
writer.KeyValue("id", "ghanima")
writer.KeyValue("alive", false)
})
writer.Array("friends", func(){
writer.Value("duncan", "moneo")
})
})
RootObject(nest func())
- Generate a document as an objectRootArray(nest func())
- Generate a document as an arrayKeyValue(key string, value interface)
- Write the key value pairValue(value interface)
- Write the value (only useful within an array)Object(key string, nest func())
- Start a nested objectArray(key string, nest func())
- Start an arrayArrayObject(nest func())
- Used to place an object within an arrayArrayValues(key string, []string{....})
- Used to write an array. The array can be of any serialiazable type
You can write raw data directly to the writer, circumventing any delimiter insertion or character escaping via the Raw(data []byte)
method.
The Typed library provides the opposite functionality: a lightweight approaching to reading JSON data.