chanson

package module
v0.0.0-...-a3148fa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2018 License: BSD-2-Clause Imports: 3 Imported by: 1

README

Chanson Build Status Coverage Status

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. It is not an encoder itself, by default it relies on json.Encoder but its flexible enough to let you use whatever you want.

Example

package main

import (
	"bytes"
	"fmt"

	"github.com/gchaincl/chanson"
)

func main() {
	ch := make(chan int)
	go func() {
		ch <- 1
		ch <- 2
		ch <- 3
		ch <- 4
		close(ch)
	}()

	buf := bytes.NewBuffer(nil)
	cs := chanson.New(buf)
	cs.Array(func(a chanson.Array) {
		for i := range ch {
			a.Push(i)
		}
	})

	fmt.Printf("%v", buf.String())
}

For more examples and documentarion see the Godoc.

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 ([]).

func (*Array) Push

func (a *Array) Push(val Value)

Push pushes an item into the 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:

func New

func New(w io.Writer) Chanson

New returns a new json stream. The stream will use w for write the output

func (Chanson) Array

func (cs Chanson) Array(f func(Array))

Array will execute the callback inside an array context this is: "[" f() "]"

func (Chanson) Object

func (cs Chanson) Object(f func(Object))

Object will execute the callback inside an object context this is: "{" f() "}"

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:

func (*Object) Set

func (obj *Object) Set(key string, val Value)

Set add an element into the object

type Value

type Value interface{}

Value is the types that functions like Array.Push() or Object.Set() can accepts as values. Custom Value types are:

  • func(Array)
  • func(Object)
  • func(io.Writer)

If Value is none of the above, it will be encoded using json.Encoder

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL