...
Run Format

Source file src/pkg/encoding/json/stream.go

     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 json
     6	
     7	import (
     8		"bytes"
     9		"errors"
    10		"io"
    11	)
    12	
    13	// A Decoder reads and decodes JSON objects from an input stream.
    14	type Decoder struct {
    15		r    io.Reader
    16		buf  []byte
    17		d    decodeState
    18		scan scanner
    19		err  error
    20	}
    21	
    22	// NewDecoder returns a new decoder that reads from r.
    23	//
    24	// The decoder introduces its own buffering and may
    25	// read data from r beyond the JSON values requested.
    26	func NewDecoder(r io.Reader) *Decoder {
    27		return &Decoder{r: r}
    28	}
    29	
    30	// UseNumber causes the Decoder to unmarshal a number into an interface{} as a
    31	// Number instead of as a float64.
    32	func (dec *Decoder) UseNumber() { dec.d.useNumber = true }
    33	
    34	// Decode reads the next JSON-encoded value from its
    35	// input and stores it in the value pointed to by v.
    36	//
    37	// See the documentation for Unmarshal for details about
    38	// the conversion of JSON into a Go value.
    39	func (dec *Decoder) Decode(v interface{}) error {
    40		if dec.err != nil {
    41			return dec.err
    42		}
    43	
    44		n, err := dec.readValue()
    45		if err != nil {
    46			return err
    47		}
    48	
    49		// Don't save err from unmarshal into dec.err:
    50		// the connection is still usable since we read a complete JSON
    51		// object from it before the error happened.
    52		dec.d.init(dec.buf[0:n])
    53		err = dec.d.unmarshal(v)
    54	
    55		// Slide rest of data down.
    56		rest := copy(dec.buf, dec.buf[n:])
    57		dec.buf = dec.buf[0:rest]
    58	
    59		return err
    60	}
    61	
    62	// Buffered returns a reader of the data remaining in the Decoder's
    63	// buffer. The reader is valid until the next call to Decode.
    64	func (dec *Decoder) Buffered() io.Reader {
    65		return bytes.NewReader(dec.buf)
    66	}
    67	
    68	// readValue reads a JSON value into dec.buf.
    69	// It returns the length of the encoding.
    70	func (dec *Decoder) readValue() (int, error) {
    71		dec.scan.reset()
    72	
    73		scanp := 0
    74		var err error
    75	Input:
    76		for {
    77			// Look in the buffer for a new value.
    78			for i, c := range dec.buf[scanp:] {
    79				dec.scan.bytes++
    80				v := dec.scan.step(&dec.scan, int(c))
    81				if v == scanEnd {
    82					scanp += i
    83					break Input
    84				}
    85				// scanEnd is delayed one byte.
    86				// We might block trying to get that byte from src,
    87				// so instead invent a space byte.
    88				if (v == scanEndObject || v == scanEndArray) && dec.scan.step(&dec.scan, ' ') == scanEnd {
    89					scanp += i + 1
    90					break Input
    91				}
    92				if v == scanError {
    93					dec.err = dec.scan.err
    94					return 0, dec.scan.err
    95				}
    96			}
    97			scanp = len(dec.buf)
    98	
    99			// Did the last read have an error?
   100			// Delayed until now to allow buffer scan.
   101			if err != nil {
   102				if err == io.EOF {
   103					if dec.scan.step(&dec.scan, ' ') == scanEnd {
   104						break Input
   105					}
   106					if nonSpace(dec.buf) {
   107						err = io.ErrUnexpectedEOF
   108					}
   109				}
   110				dec.err = err
   111				return 0, err
   112			}
   113	
   114			// Make room to read more into the buffer.
   115			const minRead = 512
   116			if cap(dec.buf)-len(dec.buf) < minRead {
   117				newBuf := make([]byte, len(dec.buf), 2*cap(dec.buf)+minRead)
   118				copy(newBuf, dec.buf)
   119				dec.buf = newBuf
   120			}
   121	
   122			// Read.  Delay error for next iteration (after scan).
   123			var n int
   124			n, err = dec.r.Read(dec.buf[len(dec.buf):cap(dec.buf)])
   125			dec.buf = dec.buf[0 : len(dec.buf)+n]
   126		}
   127		return scanp, nil
   128	}
   129	
   130	func nonSpace(b []byte) bool {
   131		for _, c := range b {
   132			if !isSpace(rune(c)) {
   133				return true
   134			}
   135		}
   136		return false
   137	}
   138	
   139	// An Encoder writes JSON objects to an output stream.
   140	type Encoder struct {
   141		w   io.Writer
   142		e   encodeState
   143		err error
   144	}
   145	
   146	// NewEncoder returns a new encoder that writes to w.
   147	func NewEncoder(w io.Writer) *Encoder {
   148		return &Encoder{w: w}
   149	}
   150	
   151	// Encode writes the JSON encoding of v to the stream,
   152	// followed by a newline character.
   153	//
   154	// See the documentation for Marshal for details about the
   155	// conversion of Go values to JSON.
   156	func (enc *Encoder) Encode(v interface{}) error {
   157		if enc.err != nil {
   158			return enc.err
   159		}
   160		e := newEncodeState()
   161		err := e.marshal(v)
   162		if err != nil {
   163			return err
   164		}
   165	
   166		// Terminate each value with a newline.
   167		// This makes the output look a little nicer
   168		// when debugging, and some kind of space
   169		// is required if the encoded value was a number,
   170		// so that the reader knows there aren't more
   171		// digits coming.
   172		e.WriteByte('\n')
   173	
   174		if _, err = enc.w.Write(e.Bytes()); err != nil {
   175			enc.err = err
   176		}
   177		encodeStatePool.Put(e)
   178		return err
   179	}
   180	
   181	// RawMessage is a raw encoded JSON object.
   182	// It implements Marshaler and Unmarshaler and can
   183	// be used to delay JSON decoding or precompute a JSON encoding.
   184	type RawMessage []byte
   185	
   186	// MarshalJSON returns *m as the JSON encoding of m.
   187	func (m *RawMessage) MarshalJSON() ([]byte, error) {
   188		return *m, nil
   189	}
   190	
   191	// UnmarshalJSON sets *m to a copy of data.
   192	func (m *RawMessage) UnmarshalJSON(data []byte) error {
   193		if m == nil {
   194			return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
   195		}
   196		*m = append((*m)[0:0], data...)
   197		return nil
   198	}
   199	
   200	var _ Marshaler = (*RawMessage)(nil)
   201	var _ Unmarshaler = (*RawMessage)(nil)
   202	

View as plain text