jsonwriter

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2015 License: MIT Imports: 7 Imported by: 3

README

JsonWriter

Write JSON to an io.Writer. Useful for simple cases where you want to avoid encoding/json or require greater control.

Usage

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 object
  • RootArray(nest func()) - Generate a document as an array
  • KeyValue(key string, value interface) - Write the key value pair
  • Value(value interface) - Write the value (only useful within an array)
  • Object(key string, nest func()) - Start a nested object
  • Array(key string, nest func()) - Start an array
  • ArrayObject(nest func()) - Used to place an object within an array
  • ArrayValues(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.

Misc

The Typed library provides the opposite functionality: a lightweight approaching to reading JSON data.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

type Writer struct {
	W io.Writer
	// contains filtered or unexported fields
}

func New

func New(w io.Writer) *Writer

Creates a JsonWriter that writes to the provided io.Writer

func (*Writer) Array

func (w *Writer) Array(key string, f func())

Star an array with the given key

func (*Writer) ArrayObject

func (w *Writer) ArrayObject(f func())

Star an object within an array (a keyless object)

func (*Writer) ArrayValues

func (w *Writer) ArrayValues(key string, values interface{})

Writes an array with the given array of values (values must be an array or a slice)

func (*Writer) Key

func (w *Writer) Key(key string)

Writes a key. The key is placed within quotes and ends with a colon

func (*Writer) KeyInt

func (w *Writer) KeyInt(key string, value int)

writes a key: value where value is a int This is an optimized Write method

func (*Writer) KeyString

func (w *Writer) KeyString(key string, value string)

writes a key: value where value is a string This is an optimized Write method

func (*Writer) KeyValue

func (w *Writer) KeyValue(key string, value interface{})

writes a key: value This is the same as calling WriteKey(key) followe by WriteValue(value)

func (*Writer) Object

func (w *Writer) Object(key string, f func())

Star an object with the given key

func (*Writer) Raw

func (w *Writer) Raw(data []byte)

Writes the value as-is, leaving delimiters and encoding up to the caller

func (*Writer) RootArray

func (w *Writer) RootArray(f func())

Starts the writing process by creating an array. Should only be called once

func (*Writer) RootObject

func (w *Writer) RootObject(f func())

Starts the writing process by creating an object. Should only be called once

func (*Writer) Separator

func (w *Writer) Separator()

func (*Writer) Value

func (w *Writer) Value(value interface{})

value can be a string, byte, u?int(8|16|32|64)?, float(32|64)?, time.Time, bool, nil or encoding/json.Marshaller

Jump to

Keyboard shortcuts

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