1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // A Decoder reads and decodes JSON values from an input stream.
18 scanp
int // start of unread data in buf
19 scanned
int64 // amount of data already scanned
27 // NewDecoder returns a new decoder that reads from r.
29 // The decoder introduces its own buffering and may
30 // read data from r beyond the JSON values requested.
31 func NewDecoder(r io
.Reader
) *Decoder
{
35 // UseNumber causes the Decoder to unmarshal a number into an interface{} as a
36 // Number instead of as a float64.
37 func (dec
*Decoder
) UseNumber() { dec
.d
.useNumber
= true }
39 // DisallowUnknownFields causes the Decoder to return an error when the destination
40 // is a struct and the input contains object keys which do not match any
41 // non-ignored, exported fields in the destination.
42 func (dec
*Decoder
) DisallowUnknownFields() { dec
.d
.disallowUnknownFields
= true }
44 // Decode reads the next JSON-encoded value from its
45 // input and stores it in the value pointed to by v.
47 // See the documentation for Unmarshal for details about
48 // the conversion of JSON into a Go value.
49 func (dec
*Decoder
) Decode(v
interface{}) error
{
54 if err
:= dec
.tokenPrepareForDecode(); err
!= nil {
58 if !dec
.tokenValueAllowed() {
59 return &SyntaxError
{msg
: "not at beginning of value", Offset
: dec
.offset()}
62 // Read whole value into buffer.
63 n
, err
:= dec
.readValue()
67 dec
.d
.init(dec
.buf
[dec
.scanp
: dec
.scanp
+n
])
70 // Don't save err from unmarshal into dec.err:
71 // the connection is still usable since we read a complete JSON
72 // object from it before the error happened.
73 err
= dec
.d
.unmarshal(v
)
75 // fixup token streaming state
81 // Buffered returns a reader of the data remaining in the Decoder's
82 // buffer. The reader is valid until the next call to Decode.
83 func (dec
*Decoder
) Buffered() io
.Reader
{
84 return bytes
.NewReader(dec
.buf
[dec
.scanp
:])
87 // readValue reads a JSON value into dec.buf.
88 // It returns the length of the encoding.
89 func (dec
*Decoder
) readValue() (int, error
) {
96 // Look in the buffer for a new value.
97 for i
, c
:= range dec
.buf
[scanp
:] {
99 v
:= dec
.scan
.step(&dec
.scan
, c
)
104 // scanEnd is delayed one byte.
105 // We might block trying to get that byte from src,
106 // so instead invent a space byte.
107 if (v
== scanEndObject || v
== scanEndArray
) && dec
.scan
.step(&dec
.scan
, ' ') == scanEnd
{
112 dec
.err
= dec
.scan
.err
113 return 0, dec
.scan
.err
118 // Did the last read have an error?
119 // Delayed until now to allow buffer scan.
122 if dec
.scan
.step(&dec
.scan
, ' ') == scanEnd
{
125 if nonSpace(dec
.buf
) {
126 err
= io
.ErrUnexpectedEOF
133 n
:= scanp
- dec
.scanp
135 scanp
= dec
.scanp
+ n
137 return scanp
- dec
.scanp
, nil
140 func (dec
*Decoder
) refill() error
{
141 // Make room to read more into the buffer.
142 // First slide down data already consumed.
144 dec
.scanned
+= int64(dec
.scanp
)
145 n
:= copy(dec
.buf
, dec
.buf
[dec
.scanp
:])
146 dec
.buf
= dec
.buf
[:n
]
150 // Grow buffer if not large enough.
152 if cap(dec
.buf
)-len(dec
.buf
) < minRead
{
153 newBuf
:= make([]byte, len(dec
.buf
), 2*cap(dec
.buf
)+minRead
)
154 copy(newBuf
, dec
.buf
)
158 // Read. Delay error for next iteration (after scan).
159 n
, err
:= dec
.r
.Read(dec
.buf
[len(dec
.buf
):cap(dec
.buf
)])
160 dec
.buf
= dec
.buf
[0 : len(dec
.buf
)+n
]
165 func nonSpace(b
[]byte) bool {
166 for _
, c
:= range b
{
174 // An Encoder writes JSON values to an output stream.
175 type Encoder
struct {
180 indentBuf
*bytes
.Buffer
185 // NewEncoder returns a new encoder that writes to w.
186 func NewEncoder(w io
.Writer
) *Encoder
{
187 return &Encoder
{w
: w
, escapeHTML
: true}
190 // Encode writes the JSON encoding of v to the stream,
191 // followed by a newline character.
193 // See the documentation for Marshal for details about the
194 // conversion of Go values to JSON.
195 func (enc
*Encoder
) Encode(v
interface{}) error
{
199 e
:= newEncodeState()
200 err
:= e
.marshal(v
, encOpts
{escapeHTML
: enc
.escapeHTML
})
205 // Terminate each value with a newline.
206 // This makes the output look a little nicer
207 // when debugging, and some kind of space
208 // is required if the encoded value was a number,
209 // so that the reader knows there aren't more
214 if enc
.indentPrefix
!= "" || enc
.indentValue
!= "" {
215 if enc
.indentBuf
== nil {
216 enc
.indentBuf
= new(bytes
.Buffer
)
218 enc
.indentBuf
.Reset()
219 err
= Indent(enc
.indentBuf
, b
, enc
.indentPrefix
, enc
.indentValue
)
223 b
= enc
.indentBuf
.Bytes()
225 if _
, err
= enc
.w
.Write(b
); err
!= nil {
228 encodeStatePool
.Put(e
)
232 // SetIndent instructs the encoder to format each subsequent encoded
233 // value as if indented by the package-level function Indent(dst, src, prefix, indent).
234 // Calling SetIndent("", "") disables indentation.
235 func (enc
*Encoder
) SetIndent(prefix
, indent
string) {
236 enc
.indentPrefix
= prefix
237 enc
.indentValue
= indent
240 // SetEscapeHTML specifies whether problematic HTML characters
241 // should be escaped inside JSON quoted strings.
242 // The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e
243 // to avoid certain safety problems that can arise when embedding JSON in HTML.
245 // In non-HTML settings where the escaping interferes with the readability
246 // of the output, SetEscapeHTML(false) disables this behavior.
247 func (enc
*Encoder
) SetEscapeHTML(on
bool) {
251 // RawMessage is a raw encoded JSON value.
252 // It implements Marshaler and Unmarshaler and can
253 // be used to delay JSON decoding or precompute a JSON encoding.
254 type RawMessage
[]byte
256 // MarshalJSON returns m as the JSON encoding of m.
257 func (m RawMessage
) MarshalJSON() ([]byte, error
) {
259 return []byte("null"), nil
264 // UnmarshalJSON sets *m to a copy of data.
265 func (m
*RawMessage
) UnmarshalJSON(data
[]byte) error
{
267 return errors
.New("json.RawMessage: UnmarshalJSON on nil pointer")
269 *m
= append((*m
)[0:0], data
...)
273 var _ Marshaler
= (*RawMessage
)(nil)
274 var _ Unmarshaler
= (*RawMessage
)(nil)
276 // A Token holds a value of one of these types:
278 // Delim, for the four JSON delimiters [ ] { }
279 // bool, for JSON booleans
280 // float64, for JSON numbers
281 // Number, for JSON numbers
282 // string, for JSON string literals
283 // nil, for JSON null
285 type Token
interface{}
299 // advance tokenstate from a separator state to a value state
300 func (dec
*Decoder
) tokenPrepareForDecode() error
{
301 // Note: Not calling peek before switch, to avoid
302 // putting peek into the standard Decode path.
303 // peek is only called when using the Token API.
304 switch dec
.tokenState
{
305 case tokenArrayComma
:
311 return &SyntaxError
{"expected comma after array element", dec
.offset()}
314 dec
.tokenState
= tokenArrayValue
315 case tokenObjectColon
:
321 return &SyntaxError
{"expected colon after object key", dec
.offset()}
324 dec
.tokenState
= tokenObjectValue
329 func (dec
*Decoder
) tokenValueAllowed() bool {
330 switch dec
.tokenState
{
331 case tokenTopValue
, tokenArrayStart
, tokenArrayValue
, tokenObjectValue
:
337 func (dec
*Decoder
) tokenValueEnd() {
338 switch dec
.tokenState
{
339 case tokenArrayStart
, tokenArrayValue
:
340 dec
.tokenState
= tokenArrayComma
341 case tokenObjectValue
:
342 dec
.tokenState
= tokenObjectComma
346 // A Delim is a JSON array or object delimiter, one of [ ] { or }.
349 func (d Delim
) String() string {
353 // Token returns the next JSON token in the input stream.
354 // At the end of the input stream, Token returns nil, io.EOF.
356 // Token guarantees that the delimiters [ ] { } it returns are
357 // properly nested and matched: if Token encounters an unexpected
358 // delimiter in the input, it will return an error.
360 // The input stream consists of basic JSON values—bool, string,
361 // number, and null—along with delimiters [ ] { } of type Delim
362 // to mark the start and end of arrays and objects.
363 // Commas and colons are elided.
364 func (dec
*Decoder
) Token() (Token
, error
) {
372 if !dec
.tokenValueAllowed() {
373 return dec
.tokenError(c
)
376 dec
.tokenStack
= append(dec
.tokenStack
, dec
.tokenState
)
377 dec
.tokenState
= tokenArrayStart
378 return Delim('['), nil
381 if dec
.tokenState
!= tokenArrayStart
&& dec
.tokenState
!= tokenArrayComma
{
382 return dec
.tokenError(c
)
385 dec
.tokenState
= dec
.tokenStack
[len(dec
.tokenStack
)-1]
386 dec
.tokenStack
= dec
.tokenStack
[:len(dec
.tokenStack
)-1]
388 return Delim(']'), nil
391 if !dec
.tokenValueAllowed() {
392 return dec
.tokenError(c
)
395 dec
.tokenStack
= append(dec
.tokenStack
, dec
.tokenState
)
396 dec
.tokenState
= tokenObjectStart
397 return Delim('{'), nil
400 if dec
.tokenState
!= tokenObjectStart
&& dec
.tokenState
!= tokenObjectComma
{
401 return dec
.tokenError(c
)
404 dec
.tokenState
= dec
.tokenStack
[len(dec
.tokenStack
)-1]
405 dec
.tokenStack
= dec
.tokenStack
[:len(dec
.tokenStack
)-1]
407 return Delim('}'), nil
410 if dec
.tokenState
!= tokenObjectColon
{
411 return dec
.tokenError(c
)
414 dec
.tokenState
= tokenObjectValue
418 if dec
.tokenState
== tokenArrayComma
{
420 dec
.tokenState
= tokenArrayValue
423 if dec
.tokenState
== tokenObjectComma
{
425 dec
.tokenState
= tokenObjectKey
428 return dec
.tokenError(c
)
431 if dec
.tokenState
== tokenObjectStart || dec
.tokenState
== tokenObjectKey
{
433 old
:= dec
.tokenState
434 dec
.tokenState
= tokenTopValue
435 err
:= dec
.Decode(&x
)
440 dec
.tokenState
= tokenObjectColon
446 if !dec
.tokenValueAllowed() {
447 return dec
.tokenError(c
)
450 if err
:= dec
.Decode(&x
); err
!= nil {
458 func (dec
*Decoder
) tokenError(c
byte) (Token
, error
) {
460 switch dec
.tokenState
{
462 context
= " looking for beginning of value"
463 case tokenArrayStart
, tokenArrayValue
, tokenObjectValue
:
464 context
= " looking for beginning of value"
465 case tokenArrayComma
:
466 context
= " after array element"
468 context
= " looking for beginning of object key string"
469 case tokenObjectColon
:
470 context
= " after object key"
471 case tokenObjectComma
:
472 context
= " after object key:value pair"
474 return nil, &SyntaxError
{"invalid character " + quoteChar(c
) + " " + context
, dec
.offset()}
477 // More reports whether there is another element in the
478 // current array or object being parsed.
479 func (dec
*Decoder
) More() bool {
481 return err
== nil && c
!= ']' && c
!= '}'
484 func (dec
*Decoder
) peek() (byte, error
) {
487 for i
:= dec
.scanp
; i
< len(dec
.buf
); i
++ {
495 // buffer has been scanned, now report any error
503 func (dec
*Decoder
) offset() int64 {
504 return dec
.scanned
+ int64(dec
.scanp
)