json

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package json provides an API for JSON serialization that automatically infers types. See the examples below for usage.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyInput  = errors.New("empty input")
	ErrInvalidRune = errors.New("invalid rune")

	BytesTrue  = []byte("true")
	BytesFalse = []byte("false")
	BytesNull  = []byte("null")
)

Functions

func Marshal

func Marshal(obj interface{}, pretty bool) ([]byte, error)

Marshal formats an object into a slice of bytes of JSON. If the pretty parameter is set, then prints pretty output with an indent.

Example (Map)

This example shows you can marshal a map into a JSON object

obj := map[string]interface{}{
	"a": 1,
	"b": 2,
	"c": 3,
}
b, err := Marshal(obj, false)
if err != nil {
	panic(err)
}
fmt.Println(string(b))
Output:

{"a":1,"b":2,"c":3}
Example (Pretty)

This example shows you can marshal a map into a JSON object with pretty formatting.

obj := map[string]interface{}{
	"a": 1,
	"b": 2,
	"c": 3,
}
b, err := Marshal(obj, true)
if err != nil {
	panic(err)
}
fmt.Println(string(b))
Output:

{
  "a": 1,
  "b": 2,
  "c": 3
}
Example (Slice)

This examples shows that you can marshal a slice into a JSON array.

obj := []interface{}{"1", "2", "3"}
b, err := Marshal(obj, false)
if err != nil {
	panic(err)
}
fmt.Println(string(b))
Output:

["1","2","3"]

func Unmarshal

func Unmarshal(b []byte) (interface{}, error)

Unmarshal parses a slice of bytes into an object using a few simple type inference rules. This package is useful when your program needs to parse data, that you have no a priori awareness of its structure or type. If no input is given, then returns ErrEmptyInput. If the first rune is invalid, then returns ErrInvalidRune.

  • true => true (bool)
  • false => false (bool)
  • null => nil
  • [...] => []interface{}
  • {...} => map[string]interface{}
  • "..." => string
  • otherwise trys to parse as float
Example (Map)

This example shows you can unmarshal a JSON object into a map.

str := "{\"a\":1,\"b\":2,\"c\":3}"
obj, err := Unmarshal([]byte(str))
if err != nil {
	panic(err)
}
m, ok := obj.(map[string]interface{})
if ok {
	if v, ok := m["b"]; ok {
		fmt.Println("B:", v)
	}
}
Output:

B: 2
Example (Slice)

This examples shows that you can unmarshal a JSON array into a slice.

str := "[1,2,3]"
obj, err := Unmarshal([]byte(str))
if err != nil {
	panic(err)
}
slc, ok := obj.([]interface{})
if ok {
	fmt.Println("Length:", len(slc))
}
Output:

Length: 3

func UnmarshalType

func UnmarshalType(b []byte, outputType reflect.Type) (interface{}, error)

UnmarshalType parses a slice of bytes into an object of a given type. If no input is given, then returns ErrEmptyInput. If the first rune is invalid, then returns ErrInvalidRune.

Example (Map)

This example shows UnmarshalType accepts a type parameter and returns the object as that type if possible.

str := "{\"a\":1,\"b\":2,\"c\":3}"
obj, err := UnmarshalType([]byte(str), reflect.TypeOf(map[string]float64{}))
if err != nil {
	panic(err)
}
m, ok := obj.(map[string]float64)
if ok {
	if v, ok := m["b"]; ok {
		fmt.Println("B:", v)
	}
}
Output:

B: 2
Example (Slice)

This example shows that UnmarshalType accepts a type parameter and returns the object as that type if possible.

str := "[1,2,3]"
obj, err := UnmarshalType([]byte(str), reflect.TypeOf([]float64{}))
if err != nil {
	panic(err)
}
slc, ok := obj.([]float64)
if ok {
	fmt.Println("Length:", len(slc))
}
Output:

Length: 3

Types

type ErrInvalidKind

type ErrInvalidKind struct {
	Value    reflect.Type
	Expected []reflect.Kind
}

func (ErrInvalidKind) Error

func (e ErrInvalidKind) Error() string

Error returns the error formatted as a string.

Jump to

Keyboard shortcuts

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