Documentation ¶
Overview ¶
Package json is a JSON parser.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Unmarshal ¶
Unmarshal deserializes data from the scanner to value v. In the case of struct values, only exported fields will be decoded. The lowercase field name is used as the key for each exported field, but this behavior may be changed using the respective field tag. The tag may also contain flags to tweak the decoding behavior for the field.
Types ¶
type ArrayScanner ¶
type ArrayScanner struct {
// contains filtered or unexported fields
}
ArrayScanner is a helper for scanning arrays.
func (*ArrayScanner) Scan ¶
func (as *ArrayScanner) Scan() bool
Scan advances to the next element at the current nesting level, possibly skipping over nested elements. Scan returns false at the End element for the current level or if an error is encountered.
type DecodeTypeError ¶
type DecodeTypeError struct { Kind Kind // description of JSON value Type reflect.Type // type of Go value it could not be assigned to }
A DecodeTypeError describes a JSON value that was not appropriate for a value of a specific Go type.
func (*DecodeTypeError) Error ¶
func (e *DecodeTypeError) Error() string
type Kind ¶
type Kind int
Kind represents the kind a JSON document element.
const ( // Null represents a JSON null element. Null Kind = iota // Bool represents a JSON bool element. Bool // String represents a JSON string element. String // Number represents a JSON number element. Number // Array represents the start of a JSON array element. Array // Object represents the start of a JSON object element. Object // End represents the end of an object or array element. End )
type ObjectScanner ¶
type ObjectScanner struct {
// contains filtered or unexported fields
}
ObjectScanner is a helper for scanning objects.
func (*ObjectScanner) Name ¶
func (os *ObjectScanner) Name() string
Name returns the last member named scanned at the current nesting level.
func (*ObjectScanner) Scan ¶
func (os *ObjectScanner) Scan() bool
Scan advances to the next element at the current nesting level, possibly skipping over nested elements. Scan returns false at the End element for the current level or if an error is encountered.
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner reads a JSON document from an io.Reader. Successive calls to the Scan method step through the elements of the document as follows:
element = Null | Bool | Number | String | object | array array = Array element* End object = Object element* End
Scanning stops unrecoverably at EOF, the first I/O error, or a syntax error. When a scan stops, the reader may have advanced arbitrarily far past the last token.
When scanning strings, invalid UTF-8 or invalid UTF-16 surrogate pairs are not treated as an error. Instead, they are replaced by the Unicode replacement character U+FFFD.
Example ¶
This example shows how to decode a JSON value to a tree of maps and slices.
package main import ( "fmt" "strconv" "strings" "github.com/garyburd/json" ) const jsonText = ` [ { "name": "redigo", "keywords": ["database", "redis"], "imports": 10 }, { "name": "mgo", "keywords": ["database", "mongodb"], "imports": 22 } ] ` var emptySlice = make([]interface{}, 0, 0) func decodeValue(s *json.Scanner) (interface{}, error) { switch s.Kind() { case json.Number: return strconv.ParseFloat(string(s.Value()), 64) case json.String: return string(s.Value()), nil case json.Array: v := emptySlice as := s.ArrayScanner() for as.Scan() { subv, err := decodeValue(s) if err != nil { return v, err } v = append(v, subv) } return v, s.Err() case json.Object: v := make(map[string]interface{}) os := s.ObjectScanner() for os.Scan() { subv, err := decodeValue(s) if err != nil { return v, err } v[os.Name()] = subv } return v, s.Err() case json.Bool: return s.BoolValue(), nil case json.Null: return nil, nil default: return nil, fmt.Errorf("unexpected %v", s.Kind()) } } // This example shows how to decode a JSON value to a tree of maps and slices. func main() { s := json.NewScanner(strings.NewReader(jsonText)) if !s.Scan() { fmt.Printf("error %v\n", s.Err()) return } v, err := decodeValue(s) if err != nil { fmt.Printf("error %v\n", err) return } s.Scan() if s.Err() != nil { fmt.Printf("error %v\n", s.Err()) return } fmt.Println(v) }
Output: [map[name:redigo keywords:[database redis] imports:10] map[name:mgo keywords:[database mongodb] imports:22]]
func NewScanner ¶
NewScanner allocates and initializes a new scanner.
func (*Scanner) ArrayScanner ¶
func (s *Scanner) ArrayScanner() ArrayScanner
func (*Scanner) Name ¶
Name returns the object member name of the current value. The underlying array may point to data that will be overwritten by a subsequent call to Scan.
func (*Scanner) ObjectScanner ¶
func (s *Scanner) ObjectScanner() ObjectScanner
type SyntaxError ¶
type SyntaxError struct { Pos int // contains filtered or unexported fields }
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string