| 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. |
| 4 |
|
| 5 |
package json5 |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"errors" |
| 10 |
"io" |
| 11 |
) |
| 12 |
|
| 13 |
// A Decoder reads and decodes JSON values from an input stream. |
| 14 |
type Decoder struct { |
| 15 |
r io.Reader |
| 16 |
buf []byte |
| 17 |
d decodeState |
| 18 |
scanp int // start of unread data in buf |
| 19 |
scan scanner |
| 20 |
err error |
| 21 |
|
| 22 |
tokenState int |
| 23 |
tokenStack []int |
| 24 |
} |
| 25 |
|
| 26 |
// NewDecoder returns a new decoder that reads from r. |
| 27 |
// |
| 28 |
// The decoder introduces its own buffering and may |
| 29 |
// read data from r beyond the JSON values requested. |
| 30 |
func NewDecoder(r io.Reader) *Decoder { |
| 31 |
return &Decoder{r: r} |
| 32 |
} |
| 33 |
|
| 34 |
// UseNumber causes the Decoder to unmarshal a number into an interface{} as a |
| 35 |
// Number instead of as a float64. |
| 36 |
func (dec *Decoder) UseNumber() { dec.d.useNumber = true } |
| 37 |
|
| 38 |
// DisallowUnknownFields causes the Decoder to return an error when the destination |
| 39 |
// is a struct and the input contains object keys which do not match any |
| 40 |
// non-ignored, exported fields in the destination. |
| 41 |
func (dec *Decoder) DisallowUnknownFields() { dec.d.disallowUnknownFields = true } |
| 42 |
|
| 43 |
// Decode reads the next JSON-encoded value from its |
| 44 |
// input and stores it in the value pointed to by v. |
| 45 |
// |
| 46 |
// See the documentation for Unmarshal for details about |
| 47 |
// the conversion of JSON into a Go value. |
| 48 |
func (dec *Decoder) Decode(v interface{}) error { |
| 49 |
if dec.err != nil { |
| 50 |
return dec.err |
| 51 |
} |
| 52 |
|
| 53 |
// Read whole value into buffer. |
| 54 |
n, err := dec.readValue() |
| 55 |
if err != nil { |
| 56 |
return err |
| 57 |
} |
| 58 |
dec.d.init(dec.buf[dec.scanp : dec.scanp+n]) |
| 59 |
dec.scanp += n |
| 60 |
|
| 61 |
// Don't save err from unmarshal into dec.err: |
| 62 |
// the connection is still usable since we read a complete JSON |
| 63 |
// object from it before the error happened. |
| 64 |
err = dec.d.unmarshal(v) |
| 65 |
|
| 66 |
return err |
| 67 |
} |
| 68 |
|
| 69 |
// Buffered returns a reader of the data remaining in the Decoder's |
| 70 |
// buffer. The reader is valid until the next call to Decode. |
| 71 |
func (dec *Decoder) Buffered() io.Reader { |
| 72 |
return bytes.NewReader(dec.buf[dec.scanp:]) |
| 73 |
} |
| 74 |
|
| 75 |
// readValue reads a JSON value into dec.buf. |
| 76 |
// It returns the length of the encoding. |
| 77 |
func (dec *Decoder) readValue() (int, error) { |
| 78 |
dec.scan.reset() |
| 79 |
|
| 80 |
scanp := dec.scanp |
| 81 |
var err error |
| 82 |
Input: |
| 83 |
for { |
| 84 |
// Look in the buffer for a new value. |
| 85 |
for i, c := range dec.buf[scanp:] { |
| 86 |
dec.scan.bytes++ |
| 87 |
v := dec.scan.step(&dec.scan, c) |
| 88 |
if v == scanEnd { |
| 89 |
scanp += i |
| 90 |
break Input |
| 91 |
} |
| 92 |
// scanEnd is delayed one byte. |
| 93 |
// We might block trying to get that byte from src, |
| 94 |
// so instead invent a space byte. |
| 95 |
if (v == scanEndObject || v == scanEndArray) && dec.scan.step(&dec.scan, ' ') == scanEnd { |
| 96 |
scanp += i + 1 |
| 97 |
break Input |
| 98 |
} |
| 99 |
if v == scanError { |
| 100 |
dec.err = dec.scan.err |
| 101 |
return 0, dec.scan.err |
| 102 |
} |
| 103 |
} |
| 104 |
scanp = len(dec.buf) |
| 105 |
|
| 106 |
// Did the last read have an error? |
| 107 |
// Delayed until now to allow buffer scan. |
| 108 |
if err != nil { |
| 109 |
if err == io.EOF { |
| 110 |
if dec.scan.step(&dec.scan, ' ') == scanEnd { |
| 111 |
break Input |
| 112 |
} |
| 113 |
if nonSpace(dec.buf) { |
| 114 |
err = io.ErrUnexpectedEOF |
| 115 |
} |
| 116 |
} |
| 117 |
dec.err = err |
| 118 |
return 0, err |
| 119 |
} |
| 120 |
|
| 121 |
n := scanp - dec.scanp |
| 122 |
err = dec.refill() |
| 123 |
scanp = dec.scanp + n |
| 124 |
} |
| 125 |
return scanp - dec.scanp, nil |
| 126 |
} |
| 127 |
|
| 128 |
func (dec *Decoder) refill() error { |
| 129 |
// Make room to read more into the buffer. |
| 130 |
// First slide down data already consumed. |
| 131 |
if dec.scanp > 0 { |
| 132 |
n := copy(dec.buf, dec.buf[dec.scanp:]) |
| 133 |
dec.buf = dec.buf[:n] |
| 134 |
dec.scanp = 0 |
| 135 |
} |
| 136 |
|
| 137 |
// Grow buffer if not large enough. |
| 138 |
const minRead = 512 |
| 139 |
if cap(dec.buf)-len(dec.buf) < minRead { |
| 140 |
newBuf := make([]byte, len(dec.buf), 2*cap(dec.buf)+minRead) |
| 141 |
copy(newBuf, dec.buf) |
| 142 |
dec.buf = newBuf |
| 143 |
} |
| 144 |
|
| 145 |
// Read. Delay error for next iteration (after scan). |
| 146 |
n, err := dec.r.Read(dec.buf[len(dec.buf):cap(dec.buf)]) |
| 147 |
dec.buf = dec.buf[0 : len(dec.buf)+n] |
| 148 |
|
| 149 |
return err |
| 150 |
} |
| 151 |
|
| 152 |
func nonSpace(b []byte) bool { |
| 153 |
for _, c := range b { |
| 154 |
if !isSpace(c) { |
| 155 |
return true |
| 156 |
} |
| 157 |
} |
| 158 |
return false |
| 159 |
} |
| 160 |
|
| 161 |
// RawMessage is a raw encoded JSON value. |
| 162 |
// It implements Marshaler and Unmarshaler and can |
| 163 |
// be used to delay JSON decoding or precompute a JSON encoding. |
| 164 |
type RawMessage []byte |
| 165 |
|
| 166 |
// UnmarshalJSON sets *m to a copy of data. |
| 167 |
func (m *RawMessage) UnmarshalJSON(data []byte) error { |
| 168 |
if m == nil { |
| 169 |
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer") |
| 170 |
} |
| 171 |
*m = append((*m)[0:0], data...) |
| 172 |
return nil |
| 173 |
} |
| 174 |
|
| 175 |
var _ Unmarshaler = (*RawMessage)(nil) |