Source file src/encoding/json/v2_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  //go:build goexperiment.jsonv2
     6  
     7  package json
     8  
     9  import (
    10  	"bytes"
    11  	"errors"
    12  	"io"
    13  
    14  	"encoding/json/jsontext"
    15  	jsonv2 "encoding/json/v2"
    16  )
    17  
    18  // A Decoder reads and decodes JSON values from an input stream.
    19  type Decoder struct {
    20  	dec  *jsontext.Decoder
    21  	opts jsonv2.Options
    22  	err  error
    23  
    24  	// hadPeeked reports whether [Decoder.More] was called.
    25  	// It is reset by [Decoder.Decode] and [Decoder.Token].
    26  	hadPeeked bool
    27  }
    28  
    29  // NewDecoder returns a new decoder that reads from r.
    30  //
    31  // The decoder introduces its own buffering and may
    32  // read data from r beyond the JSON values requested.
    33  func NewDecoder(r io.Reader) *Decoder {
    34  	// Hide bytes.Buffer from jsontext since it implements optimizations that
    35  	// also limits certain ways it could be used. For example, one cannot write
    36  	// to the bytes.Buffer while it is in use by jsontext.Decoder.
    37  	if _, ok := r.(*bytes.Buffer); ok {
    38  		r = struct{ io.Reader }{r}
    39  	}
    40  
    41  	dec := new(Decoder)
    42  	dec.opts = DefaultOptionsV1()
    43  	dec.dec = jsontext.NewDecoder(r, dec.opts)
    44  	return dec
    45  }
    46  
    47  // UseNumber causes the Decoder to unmarshal a number into an
    48  // interface value as a [Number] instead of as a float64.
    49  func (dec *Decoder) UseNumber() {
    50  	if useNumber, _ := jsonv2.GetOption(dec.opts, unmarshalAnyWithRawNumber); !useNumber {
    51  		dec.opts = jsonv2.JoinOptions(dec.opts, unmarshalAnyWithRawNumber(true))
    52  	}
    53  }
    54  
    55  // DisallowUnknownFields causes the Decoder to return an error when the destination
    56  // is a struct and the input contains object keys which do not match any
    57  // non-ignored, exported fields in the destination.
    58  func (dec *Decoder) DisallowUnknownFields() {
    59  	if reject, _ := jsonv2.GetOption(dec.opts, jsonv2.RejectUnknownMembers); !reject {
    60  		dec.opts = jsonv2.JoinOptions(dec.opts, jsonv2.RejectUnknownMembers(true))
    61  	}
    62  }
    63  
    64  // Decode reads the next JSON-encoded value from its
    65  // input and stores it in the value pointed to by v.
    66  //
    67  // See the documentation for [Unmarshal] for details about
    68  // the conversion of JSON into a Go value.
    69  func (dec *Decoder) Decode(v any) error {
    70  	if dec.err != nil {
    71  		return dec.err
    72  	}
    73  	b, err := dec.dec.ReadValue()
    74  	if err != nil {
    75  		dec.err = transformSyntacticError(err)
    76  		if dec.err.Error() == errUnexpectedEnd.Error() {
    77  			// NOTE: Decode has always been inconsistent with Unmarshal
    78  			// with regard to the exact error value for truncated input.
    79  			dec.err = io.ErrUnexpectedEOF
    80  		}
    81  		return dec.err
    82  	}
    83  	dec.hadPeeked = false
    84  	return jsonv2.Unmarshal(b, v, dec.opts)
    85  }
    86  
    87  // Buffered returns a reader of the data remaining in the Decoder's
    88  // buffer. The reader is valid until the next call to [Decoder.Decode].
    89  func (dec *Decoder) Buffered() io.Reader {
    90  	return bytes.NewReader(dec.dec.UnreadBuffer())
    91  }
    92  
    93  // An Encoder writes JSON values to an output stream.
    94  type Encoder struct {
    95  	w    io.Writer
    96  	opts jsonv2.Options
    97  	err  error
    98  
    99  	buf       bytes.Buffer
   100  	indentBuf bytes.Buffer
   101  
   102  	indentPrefix string
   103  	indentValue  string
   104  }
   105  
   106  // NewEncoder returns a new encoder that writes to w.
   107  func NewEncoder(w io.Writer) *Encoder {
   108  	enc := new(Encoder)
   109  	enc.w = w
   110  	enc.opts = DefaultOptionsV1()
   111  	return enc
   112  }
   113  
   114  // Encode writes the JSON encoding of v to the stream,
   115  // followed by a newline character.
   116  //
   117  // See the documentation for [Marshal] for details about the
   118  // conversion of Go values to JSON.
   119  func (enc *Encoder) Encode(v any) error {
   120  	if enc.err != nil {
   121  		return enc.err
   122  	}
   123  
   124  	buf := &enc.buf
   125  	buf.Reset()
   126  	if err := jsonv2.MarshalWrite(buf, v, enc.opts); err != nil {
   127  		return err
   128  	}
   129  	if len(enc.indentPrefix)+len(enc.indentValue) > 0 {
   130  		enc.indentBuf.Reset()
   131  		if err := Indent(&enc.indentBuf, buf.Bytes(), enc.indentPrefix, enc.indentValue); err != nil {
   132  			return err
   133  		}
   134  		buf = &enc.indentBuf
   135  	}
   136  	buf.WriteByte('\n')
   137  
   138  	if _, err := enc.w.Write(buf.Bytes()); err != nil {
   139  		enc.err = err
   140  		return err
   141  	}
   142  	return nil
   143  }
   144  
   145  // SetIndent instructs the encoder to format each subsequent encoded
   146  // value as if indented by the package-level function Indent(dst, src, prefix, indent).
   147  // Calling SetIndent("", "") disables indentation.
   148  func (enc *Encoder) SetIndent(prefix, indent string) {
   149  	enc.indentPrefix = prefix
   150  	enc.indentValue = indent
   151  }
   152  
   153  // SetEscapeHTML specifies whether problematic HTML characters
   154  // should be escaped inside JSON quoted strings.
   155  // The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e
   156  // to avoid certain safety problems that can arise when embedding JSON in HTML.
   157  //
   158  // In non-HTML settings where the escaping interferes with the readability
   159  // of the output, SetEscapeHTML(false) disables this behavior.
   160  func (enc *Encoder) SetEscapeHTML(on bool) {
   161  	if escape, _ := jsonv2.GetOption(enc.opts, jsontext.EscapeForHTML); escape != on {
   162  		enc.opts = jsonv2.JoinOptions(enc.opts, jsontext.EscapeForHTML(on))
   163  	}
   164  }
   165  
   166  // RawMessage is a raw encoded JSON value.
   167  // It implements [Marshaler] and [Unmarshaler] and can
   168  // be used to delay JSON decoding or precompute a JSON encoding.
   169  type RawMessage = jsontext.Value
   170  
   171  // A Token holds a value of one of these types:
   172  //
   173  //   - [Delim], for the four JSON delimiters [ ] { }
   174  //   - bool, for JSON booleans
   175  //   - float64, for JSON numbers
   176  //   - [Number], for JSON numbers
   177  //   - string, for JSON string literals
   178  //   - nil, for JSON null
   179  type Token any
   180  
   181  // A Delim is a JSON array or object delimiter, one of [ ] { or }.
   182  type Delim rune
   183  
   184  func (d Delim) String() string {
   185  	return string(d)
   186  }
   187  
   188  // Token returns the next JSON token in the input stream.
   189  // At the end of the input stream, Token returns nil, [io.EOF].
   190  //
   191  // Token guarantees that the delimiters [ ] { } it returns are
   192  // properly nested and matched: if Token encounters an unexpected
   193  // delimiter in the input, it will return an error.
   194  //
   195  // The input stream consists of basic JSON values—bool, string,
   196  // number, and null—along with delimiters [ ] { } of type [Delim]
   197  // to mark the start and end of arrays and objects.
   198  // Commas and colons are elided.
   199  func (dec *Decoder) Token() (Token, error) {
   200  	if dec.err != nil {
   201  		return nil, dec.err
   202  	}
   203  	tok, err := dec.dec.ReadToken()
   204  	if err != nil {
   205  		// Historically, v1 would report just [io.EOF] if
   206  		// the stream is a prefix of a valid JSON value.
   207  		// It reports an unwrapped [io.ErrUnexpectedEOF] if
   208  		// truncated within a JSON token such as a literal, number, or string.
   209  		if errors.Is(err, io.ErrUnexpectedEOF) {
   210  			if len(bytes.Trim(dec.dec.UnreadBuffer(), " \r\n\t,:")) == 0 {
   211  				return nil, io.EOF
   212  			}
   213  			return nil, io.ErrUnexpectedEOF
   214  		}
   215  		return nil, transformSyntacticError(err)
   216  	}
   217  	dec.hadPeeked = false
   218  	switch k := tok.Kind(); k {
   219  	case 'n':
   220  		return nil, nil
   221  	case 'f':
   222  		return false, nil
   223  	case 't':
   224  		return true, nil
   225  	case '"':
   226  		return tok.String(), nil
   227  	case '0':
   228  		if useNumber, _ := jsonv2.GetOption(dec.opts, unmarshalAnyWithRawNumber); useNumber {
   229  			return Number(tok.String()), nil
   230  		}
   231  		return tok.Float(), nil
   232  	case '{', '}', '[', ']':
   233  		return Delim(k), nil
   234  	default:
   235  		panic("unreachable")
   236  	}
   237  }
   238  
   239  // More reports whether there is another element in the
   240  // current array or object being parsed.
   241  func (dec *Decoder) More() bool {
   242  	dec.hadPeeked = true
   243  	k := dec.dec.PeekKind()
   244  	if k == 0 {
   245  		if dec.err == nil {
   246  			// PeekKind doesn't distinguish between EOF and error,
   247  			// so read the next token to see which we get.
   248  			_, err := dec.dec.ReadToken()
   249  			if err == nil {
   250  				// This is only possible if jsontext violates its documentation.
   251  				err = errors.New("json: successful read after failed peek")
   252  			}
   253  			dec.err = transformSyntacticError(err)
   254  		}
   255  		return dec.err != io.EOF
   256  	}
   257  	return k != ']' && k != '}'
   258  }
   259  
   260  // InputOffset returns the input stream byte offset of the current decoder position.
   261  // The offset gives the location of the end of the most recently returned token
   262  // and the beginning of the next token.
   263  func (dec *Decoder) InputOffset() int64 {
   264  	offset := dec.dec.InputOffset()
   265  	if dec.hadPeeked {
   266  		// Historically, InputOffset reported the location of
   267  		// the end of the most recently returned token
   268  		// unless [Decoder.More] is called, in which case, it reported
   269  		// the beginning of the next token.
   270  		unreadBuffer := dec.dec.UnreadBuffer()
   271  		trailingTokens := bytes.TrimLeft(unreadBuffer, " \n\r\t")
   272  		if len(trailingTokens) > 0 {
   273  			leadingWhitespace := len(unreadBuffer) - len(trailingTokens)
   274  			offset += int64(leadingWhitespace)
   275  		}
   276  	}
   277  	return offset
   278  }
   279  

View as plain text