Documentation ¶
Overview ¶
Package json decodes JSON.
Index ¶
Examples ¶
Constants ¶
const ( ObjectStart = '{' // { ObjectEnd = '}' // } String = '"' // " Colon = ':' // : Comma = ',' // , ArrayStart = '[' // [ ArrayEnd = ']' // ] True = 't' // t False = 'f' // f Null = 'n' // n )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder decodes JSON values from an input stream.
func NewDecoder ¶
NewDecoder returns a new Decoder for the supplied Reader r.
func NewDecoderBuffer ¶
NewDecoderBuffer returns a new Decoder for the supplier Reader r, using the []byte buf provided for working storage.
func (*Decoder) Decode ¶
Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.
Example ¶
package main import ( "fmt" "log" "strings" "github.com/pkg/json" ) func main() { input := `{"a": 1,"b": 123.456, "c": [null]}` dec := json.NewDecoder(strings.NewReader(input)) var i interface{} err := dec.Decode(&i) if err != nil { log.Fatal(err) } fmt.Printf("%v\n", i) }
Output: map[a:1 b:123.456 c:[<nil>]]
func (*Decoder) NextToken ¶
NextToken returns a []byte referencing the next logical token in the stream. The []byte is valid until Token is called again. At the end of the input stream, Token returns nil, io.EOF.
Token guarantees that the delimiters [ ] { } it returns are properly nested and matched: if Token encounters an unexpected delimiter in the input, it will return an error.
A valid token begins with one of the following:
{ Object start [ Array start } Object end ] Array End t JSON true f JSON false n JSON null " A string, possibly containing backslash escaped entites. -, 0-9 A number
Commas and colons are elided.
Example ¶
package main import ( "fmt" "io" "log" "strings" "github.com/pkg/json" ) func main() { input := `{"a": 1,"b": 123.456, "c": [null]}` dec := json.NewDecoder(strings.NewReader(input)) for { tok, err := dec.NextToken() if err == io.EOF { break } if err != nil { log.Fatal(err) } fmt.Printf("%s\n", tok) } }
Output: { "a" 1 "b" 123.456 "c" [ null ] }
func (*Decoder) Token ¶
Token returns the next JSON token in the input stream. At the end of the input stream, Token returns nil, io.EOF.
Token guarantees that the delimiters [ ] { } it returns are properly nested and matched: if Token encounters an unexpected delimiter in the input, it will return an error.
The input stream consists of basic JSON values—bool, string, number, and null—along with delimiters [ ] { } of type json.Delim to mark the start and end of arrays and objects. Commas and colons are elided.
Note: this API is provided for compatibility with the encoding/json package and carries a significant allocation cost. See NextToken for a more efficient API.
Example ¶
package main import ( "fmt" "io" "log" "strings" "github.com/pkg/json" ) func main() { input := `{"a": 1,"b": 123.456, "c": [null]}` dec := json.NewDecoder(strings.NewReader(input)) for { tok, err := dec.Token() if err == io.EOF { break } if err != nil { log.Fatal(err) } fmt.Printf("%v\n", tok) } }
Output: { a 1 b 123.456 c [ <nil> ] }
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner implements a JSON scanner as defined in RFC 7159.
func NewScanner ¶
NewScanner returns a new Scanner for the io.Reader r. A Scanner reads from the supplied io.Reader and produces via Next a stream of tokens, expressed as []byte slices.
func (*Scanner) Error ¶
Error returns the first error encountered. When underlying reader is exhausted, Error returns io.EOF.
func (*Scanner) Next ¶
Next returns a []byte referencing the the next lexical token in the stream. The []byte is valid until Next is called again. If the stream is at its end, or an error has occurred, Next returns a zero length []byte slice.
A valid token begins with one of the following:
{ Object start [ Array start } Object end ] Array End , Literal comma : Literal colon t JSON true f JSON false n JSON null " A string, possibly containing backslash escaped entites. -, 0-9 A number
Example ¶
package main import ( "fmt" "io" "log" "strings" "github.com/pkg/json" ) func main() { input := `{"a": 1,"b": 123.456, "c": [null]}` sc := json.NewScanner(strings.NewReader(input)) for { tok := sc.Next() if len(tok) < 1 { break } fmt.Printf("%s\n", tok) } if err := sc.Error(); err != nil && err != io.EOF { log.Fatal(err) } }
Output: { "a" : 1 , "b" : 123.456 , "c" : [ null ] }