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 ¶
Functions ¶
func Marshal ¶
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 ¶
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 ¶
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 ¶
func (ErrInvalidKind) Error ¶
func (e ErrInvalidKind) Error() string
Error returns the error formatted as a string.