github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/encoding/json/stream.go (about)

     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 connection.
   152  //
   153  // See the documentation for Marshal for details about the
   154  // conversion of Go values to JSON.
   155  func (enc *Encoder) Encode(v interface{}) error {
   156  	if enc.err != nil {
   157  		return enc.err
   158  	}
   159  	e := newEncodeState()
   160  	err := e.marshal(v)
   161  	if err != nil {
   162  		return err
   163  	}
   164  
   165  	// Terminate each value with a newline.
   166  	// This makes the output look a little nicer
   167  	// when debugging, and some kind of space
   168  	// is required if the encoded value was a number,
   169  	// so that the reader knows there aren't more
   170  	// digits coming.
   171  	e.WriteByte('\n')
   172  
   173  	if _, err = enc.w.Write(e.Bytes()); err != nil {
   174  		enc.err = err
   175  	}
   176  	putEncodeState(e)
   177  	return err
   178  }
   179  
   180  // RawMessage is a raw encoded JSON object.
   181  // It implements Marshaler and Unmarshaler and can
   182  // be used to delay JSON decoding or precompute a JSON encoding.
   183  type RawMessage []byte
   184  
   185  // MarshalJSON returns *m as the JSON encoding of m.
   186  func (m *RawMessage) MarshalJSON() ([]byte, error) {
   187  	return *m, nil
   188  }
   189  
   190  // UnmarshalJSON sets *m to a copy of data.
   191  func (m *RawMessage) UnmarshalJSON(data []byte) error {
   192  	if m == nil {
   193  		return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
   194  	}
   195  	*m = append((*m)[0:0], data...)
   196  	return nil
   197  }
   198  
   199  var _ Marshaler = (*RawMessage)(nil)
   200  var _ Unmarshaler = (*RawMessage)(nil)