Documentation
¶
Overview ¶
Package chanson provides a flexible way to construct JSON documents. As chanson populates Arrays and Objects from functions, it's perfectly suitable for streaming jsons as you build it (see examples). It is not an encoder it self, by default it relies on json.Encoder but its flexible enough to let you use whatever you want.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Array ¶
type Array struct {
// contains filtered or unexported fields
}
Array struct represents a Json Array ([]).
type Chanson ¶
type Chanson struct {
// contains filtered or unexported fields
}
Chanson struct is the handler representing the current json being encoded
Example ¶
We will read from channels inside a goroutine While we channels are open we will stream the output as json
ch := make(chan int) go func() { ch <- 1 ch <- 2 ch <- 3 ch <- 4 close(ch) }() buf := bytes.NewBuffer(nil) cs := New(buf) cs.Array(func(a Array) { for i := range ch { a.Push(i) } }) fmt.Printf("%v", buf.String())
Output:
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
Object struct represent a Json Object ({}).
Example ¶
buf := bytes.NewBuffer(nil) cs := New(ioutil.Discard) cs.Object(func(obj Object) { obj.Set("foo", "bar") obj.Set("fun", func(enc *json.Encoder) { _ = enc.Encode([]int{1, 2, 3}) }) }) fmt.Printf("%v", buf.String())
Output:
Click to show internal directories.
Click to hide internal directories.