Documentation
¶
Overview ¶
Example ¶
package main import ( "fmt" "io" "strings" "github.com/go-restit/lzjson" ) func dummyBody() io.Reader { return strings.NewReader(`{ "hello": [ { "name": "world 1", "size": 123 }, { "name": "world 2" }, { "name": "world 3" } ] }`) } type Namer struct { Name string `json:"name"` } func main() { body := dummyBody() data := lzjson.Decode(body) // reading a certain node in the json is straight forward fmt.Println(data.Get("hello").GetN(1).Get("name").String()) // output "world 2" fmt.Printf("%#v\n", data.Get("hello").GetN(0).Get("size").Int()) // output "123" // you may unmarshal the selected child item without defining parent struct var namer Namer data.Get("hello").GetN(2).Unmarshal(&namer) fmt.Println(namer.Name) // output "world 3" // you may count elements in an array without defining the array type if err, count := data.Get("hello").ParseError(), data.Get("hello").Len(); err == nil { fmt.Printf("numbers of item in json.hello: %#v\n", count) // output "numbers of item in json.hello: 3" } // parse errors inherit along the path, no matter how deep you went if err := data.Get("foo").GetN(0).ParseError(); err != nil { fmt.Println(err.Error()) // output "json.foo: undefined" } if err := data.Get("hello").Get("notexists").Get("name").ParseError(); err != nil { fmt.Println(err.Error()) // output "json.hello: not an object" } if err := data.Get("hello").GetN(0).Get("notexists").Get("name").ParseError(); err != nil { fmt.Println(err.Error()) // output "json.hello[0].notexists: undefined" } if err := data.Get("hello").GetN(10).GetN(0).Get("name").ParseError(); err != nil { fmt.Println(err.Error()) // output "json.hello[10]: undefined" } }
Output: world 2 123 world 3 numbers of item in json.hello: 3 json.foo: undefined json.hello: not an object json.hello[0].notexists: undefined json.hello[10]: undefined
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Node ¶
type Node interface { // Unmarshal parses the JSON node data into variable v Unmarshal(v interface{}) error // UnmarshalJSON implements json.Unmarshaler UnmarshalJSON(b []byte) error // Raw returns the raw JSON string in []byte Raw() []byte // Type returns the Type of the containing JSON value Type() Type // GetKeys gets an array object's keys, // or nil if not an object GetKeys() []string // Get gets object's inner value. // Only works with Object value type Get(key string) (inner Node) // Len gets the length of the value // Only works with Array and String value type Len() int // GetN gets array's inner value. // Only works with Array value type. // 0 for the first item. GetN(nth int) Node // String unmarshal the JSON into string then return String() (v string) // Number unmarshal the JSON into float64 then return Number() (v float64) // Int unmarshal the JSON into int the return Int() (v int) // Bool unmarshal the JSON into bool then return Bool() (v bool) // IsNull tells if the JSON value is null or not IsNull() bool // ParseError returns the JSON parse error, if any ParseError() error }
Node is an interface for all JSON nodes
type ParseError ¶
type ParseError int
ParseError describe error natures in parsing process
const ( ErrorUndefined ParseError = iota ErrorNotObject ErrorNotArray )
types of error
func (ParseError) Error ¶
func (err ParseError) Error() string
func (ParseError) GoString ¶
func (err ParseError) GoString() string
GoString implements fmt.GoStringer
func (ParseError) String ¶
func (i ParseError) String() string
type Type ¶
type Type int
Type represents the different type of JSON values (string, number, object, array, true, false, null) true and false are combined as bool for obvious reason
const ( TypeError Type = -1 TypeUndefined Type = iota TypeString TypeNumber TypeObject TypeArray TypeBool TypeNull )
These constant represents different JSON value types as specified in http://www.json.org/ with some exception: 1. true and false are combined as bool for obvious reason; and 2. TypeUnknown for empty strings
Click to show internal directories.
Click to hide internal directories.