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 objects from an input stream.
22 // NewDecoder returns a new decoder that reads from r.
23 func NewDecoder(r io.Reader) *Decoder {
27 // Decode reads the next JSON-encoded value from the
28 // connection and stores it in the value pointed to by v.
30 // See the documentation for Unmarshal for details about
31 // the conversion of JSON into a Go value.
32 func (dec *Decoder) Decode(v interface{}) os.Error {
37 n, err := dec.readValue()
42 // Don't save err from unmarshal into dec.err:
43 // the connection is still usable since we read a complete JSON
44 // object from it before the error happened.
45 dec.d.init(dec.buf[0:n])
46 err = dec.d.unmarshal(v)
48 // Slide rest of data down.
49 rest := copy(dec.buf, dec.buf[n:])
50 dec.buf = dec.buf[0:rest]
55 // readValue reads a JSON value into dec.buf.
56 // It returns the length of the encoding.
57 func (dec *Decoder) readValue() (int, os.Error) {
64 // Look in the buffer for a new value.
65 for i, c := range dec.buf[scanp:] {
66 v := dec.scan.step(&dec.scan, int(c))
71 // scanEnd is delayed one byte.
72 // We might block trying to get that byte from src,
73 // so instead invent a space byte.
74 if v == scanEndObject && dec.scan.step(&dec.scan, ' ') == scanEnd {
79 dec.err = dec.scan.err
80 return 0, dec.scan.err
85 // Did the last read have an error?
86 // Delayed until now to allow buffer scan.
89 if dec.scan.step(&dec.scan, ' ') == scanEnd {
92 if nonSpace(dec.buf) {
93 err = io.ErrUnexpectedEOF
100 // Make room to read more into the buffer.
102 if cap(dec.buf)-len(dec.buf) < minRead {
103 newBuf := make([]byte, len(dec.buf), 2*cap(dec.buf)+minRead)
104 copy(newBuf, dec.buf)
108 // Read. Delay error for next iteration (after scan).
110 n, err = dec.r.Read(dec.buf[len(dec.buf):cap(dec.buf)])
111 dec.buf = dec.buf[0 : len(dec.buf)+n]
116 func nonSpace(b []byte) bool {
117 for _, c := range b {
118 if !isSpace(int(c)) {
125 // An Encoder writes JSON objects to an output stream.
126 type Encoder struct {
132 // NewEncoder returns a new encoder that writes to w.
133 func NewEncoder(w io.Writer) *Encoder {
134 return &Encoder{w: w}
137 // Encode writes the JSON encoding of v to the connection.
139 // See the documentation for Marshal for details about the
140 // conversion of Go values to JSON.
141 func (enc *Encoder) Encode(v interface{}) os.Error {
146 err := enc.e.marshal(v)
151 // Terminate each value with a newline.
152 // This makes the output look a little nicer
153 // when debugging, and some kind of space
154 // is required if the encoded value was a number,
155 // so that the reader knows there aren't more
157 enc.e.WriteByte('\n')
159 if _, err = enc.w.Write(enc.e.Bytes()); err != nil {
165 // RawMessage is a raw encoded JSON object.
166 // It implements Marshaler and Unmarshaler and can
167 // be used to delay JSON decoding or precompute a JSON encoding.
168 type RawMessage []byte
170 // MarshalJSON returns *m as the JSON encoding of m.
171 func (m *RawMessage) MarshalJSON() ([]byte, os.Error) {
175 // UnmarshalJSON sets *m to a copy of data.
176 func (m *RawMessage) UnmarshalJSON(data []byte) os.Error {
178 return os.NewError("json.RawMessage: UnmarshalJSON on nil pointer")
180 *m = bytes.Add((*m)[0:0], data)
184 var _ Marshaler = (*RawMessage)(nil)
185 var _ Unmarshaler = (*RawMessage)(nil)