2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2011-12-03 03:17:34 +01:00
|
|
|
"errors"
|
2010-12-03 05:34:57 +01:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Decoder reads and decodes JSON objects from an input stream.
|
|
|
|
type Decoder struct {
|
|
|
|
r io.Reader
|
|
|
|
buf []byte
|
|
|
|
d decodeState
|
|
|
|
scan scanner
|
2011-12-03 03:17:34 +01:00
|
|
|
err error
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDecoder returns a new decoder that reads from r.
|
2012-03-02 17:38:43 +01:00
|
|
|
//
|
|
|
|
// The decoder introduces its own buffering and may
|
|
|
|
// read data from r beyond the JSON values requested.
|
2010-12-03 05:34:57 +01:00
|
|
|
func NewDecoder(r io.Reader) *Decoder {
|
|
|
|
return &Decoder{r: r}
|
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// Decode reads the next JSON-encoded value from its
|
|
|
|
// input and stores it in the value pointed to by v.
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
|
|
|
// See the documentation for Unmarshal for details about
|
|
|
|
// the conversion of JSON into a Go value.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (dec *Decoder) Decode(v interface{}) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
if dec.err != nil {
|
|
|
|
return dec.err
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := dec.readValue()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't save err from unmarshal into dec.err:
|
|
|
|
// the connection is still usable since we read a complete JSON
|
|
|
|
// object from it before the error happened.
|
|
|
|
dec.d.init(dec.buf[0:n])
|
|
|
|
err = dec.d.unmarshal(v)
|
|
|
|
|
|
|
|
// Slide rest of data down.
|
|
|
|
rest := copy(dec.buf, dec.buf[n:])
|
|
|
|
dec.buf = dec.buf[0:rest]
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// readValue reads a JSON value into dec.buf.
|
|
|
|
// It returns the length of the encoding.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (dec *Decoder) readValue() (int, error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
dec.scan.reset()
|
|
|
|
|
|
|
|
scanp := 0
|
2011-12-03 03:17:34 +01:00
|
|
|
var err error
|
2010-12-03 05:34:57 +01:00
|
|
|
Input:
|
|
|
|
for {
|
|
|
|
// Look in the buffer for a new value.
|
|
|
|
for i, c := range dec.buf[scanp:] {
|
2011-05-20 02:18:15 +02:00
|
|
|
dec.scan.bytes++
|
2010-12-03 05:34:57 +01:00
|
|
|
v := dec.scan.step(&dec.scan, int(c))
|
|
|
|
if v == scanEnd {
|
|
|
|
scanp += i
|
|
|
|
break Input
|
|
|
|
}
|
|
|
|
// scanEnd is delayed one byte.
|
|
|
|
// We might block trying to get that byte from src,
|
|
|
|
// so instead invent a space byte.
|
|
|
|
if v == scanEndObject && dec.scan.step(&dec.scan, ' ') == scanEnd {
|
|
|
|
scanp += i + 1
|
|
|
|
break Input
|
|
|
|
}
|
|
|
|
if v == scanError {
|
|
|
|
dec.err = dec.scan.err
|
|
|
|
return 0, dec.scan.err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scanp = len(dec.buf)
|
|
|
|
|
|
|
|
// Did the last read have an error?
|
|
|
|
// Delayed until now to allow buffer scan.
|
|
|
|
if err != nil {
|
2011-12-03 03:17:34 +01:00
|
|
|
if err == io.EOF {
|
2010-12-03 05:34:57 +01:00
|
|
|
if dec.scan.step(&dec.scan, ' ') == scanEnd {
|
|
|
|
break Input
|
|
|
|
}
|
|
|
|
if nonSpace(dec.buf) {
|
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dec.err = err
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make room to read more into the buffer.
|
|
|
|
const minRead = 512
|
|
|
|
if cap(dec.buf)-len(dec.buf) < minRead {
|
|
|
|
newBuf := make([]byte, len(dec.buf), 2*cap(dec.buf)+minRead)
|
|
|
|
copy(newBuf, dec.buf)
|
|
|
|
dec.buf = newBuf
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read. Delay error for next iteration (after scan).
|
|
|
|
var n int
|
|
|
|
n, err = dec.r.Read(dec.buf[len(dec.buf):cap(dec.buf)])
|
|
|
|
dec.buf = dec.buf[0 : len(dec.buf)+n]
|
|
|
|
}
|
|
|
|
return scanp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func nonSpace(b []byte) bool {
|
|
|
|
for _, c := range b {
|
2011-12-02 20:34:41 +01:00
|
|
|
if !isSpace(rune(c)) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// An Encoder writes JSON objects to an output stream.
|
|
|
|
type Encoder struct {
|
|
|
|
w io.Writer
|
|
|
|
e encodeState
|
2011-12-03 03:17:34 +01:00
|
|
|
err error
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEncoder returns a new encoder that writes to w.
|
|
|
|
func NewEncoder(w io.Writer) *Encoder {
|
|
|
|
return &Encoder{w: w}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode writes the JSON encoding of v to the connection.
|
|
|
|
//
|
|
|
|
// See the documentation for Marshal for details about the
|
|
|
|
// conversion of Go values to JSON.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (enc *Encoder) Encode(v interface{}) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
if enc.err != nil {
|
|
|
|
return enc.err
|
|
|
|
}
|
|
|
|
enc.e.Reset()
|
|
|
|
err := enc.e.marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate each value with a newline.
|
|
|
|
// This makes the output look a little nicer
|
|
|
|
// when debugging, and some kind of space
|
|
|
|
// is required if the encoded value was a number,
|
|
|
|
// so that the reader knows there aren't more
|
|
|
|
// digits coming.
|
|
|
|
enc.e.WriteByte('\n')
|
|
|
|
|
|
|
|
if _, err = enc.w.Write(enc.e.Bytes()); err != nil {
|
|
|
|
enc.err = err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawMessage is a raw encoded JSON object.
|
|
|
|
// It implements Marshaler and Unmarshaler and can
|
|
|
|
// be used to delay JSON decoding or precompute a JSON encoding.
|
|
|
|
type RawMessage []byte
|
|
|
|
|
|
|
|
// MarshalJSON returns *m as the JSON encoding of m.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (m *RawMessage) MarshalJSON() ([]byte, error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return *m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON sets *m to a copy of data.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (m *RawMessage) UnmarshalJSON(data []byte) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
if m == nil {
|
2011-12-03 03:17:34 +01:00
|
|
|
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
*m = append((*m)[0:0], data...)
|
2010-12-03 05:34:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Marshaler = (*RawMessage)(nil)
|
|
|
|
var _ Unmarshaler = (*RawMessage)(nil)
|