| 1 |
// Copyright 2023 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 jsontext implements syntactic processing of JSON |
| 8 |
// as specified in RFC 4627, RFC 7159, RFC 7493, RFC 8259, and RFC 8785. |
| 9 |
// JSON is a simple data interchange format that can represent |
| 10 |
// primitive data types such as booleans, strings, and numbers, |
| 11 |
// in addition to structured data types such as objects and arrays. |
| 12 |
// |
| 13 |
// This package (encoding/json/jsontext) is experimental, |
| 14 |
// and not subject to the Go 1 compatibility promise. |
| 15 |
// It only exists when building with the GOEXPERIMENT=jsonv2 environment variable set. |
| 16 |
// Most users should use [encoding/json]. |
| 17 |
// |
| 18 |
// The [Encoder] and [Decoder] types are used to encode or decode |
| 19 |
// a stream of JSON tokens or values. |
| 20 |
// |
| 21 |
// # Tokens and Values |
| 22 |
// |
| 23 |
// A JSON token refers to the basic structural elements of JSON: |
| 24 |
// |
| 25 |
// - a JSON literal (i.e., null, true, or false) |
| 26 |
// - a JSON string (e.g., "hello, world!") |
| 27 |
// - a JSON number (e.g., 123.456) |
| 28 |
// - a begin or end delimiter for a JSON object (i.e., '{' or '}') |
| 29 |
// - a begin or end delimiter for a JSON array (i.e., '[' or ']') |
| 30 |
// |
| 31 |
// A JSON token is represented by the [Token] type in Go. Technically, |
| 32 |
// there are two additional structural characters (i.e., ':' and ','), |
| 33 |
// but there is no [Token] representation for them since their presence |
| 34 |
// can be inferred by the structure of the JSON grammar itself. |
| 35 |
// For example, there must always be an implicit colon between |
| 36 |
// the name and value of a JSON object member. |
| 37 |
// |
| 38 |
// A JSON value refers to a complete unit of JSON data: |
| 39 |
// |
| 40 |
// - a JSON literal, string, or number |
| 41 |
// - a JSON object (e.g., `{"name":"value"}`) |
| 42 |
// - a JSON array (e.g., `[1,2,3,]`) |
| 43 |
// |
| 44 |
// A JSON value is represented by the [Value] type in Go and is a []byte |
| 45 |
// containing the raw textual representation of the value. There is some overlap |
| 46 |
// between tokens and values as both contain literals, strings, and numbers. |
| 47 |
// However, only a value can represent the entirety of a JSON object or array. |
| 48 |
// |
| 49 |
// The [Encoder] and [Decoder] types contain methods to read or write the next |
| 50 |
// [Token] or [Value] in a sequence. They maintain a state machine to validate |
| 51 |
// whether the sequence of JSON tokens and/or values produces a valid JSON. |
| 52 |
// [Options] may be passed to the [NewEncoder] or [NewDecoder] constructors |
| 53 |
// to configure the syntactic behavior of encoding and decoding. |
| 54 |
// |
| 55 |
// # Terminology |
| 56 |
// |
| 57 |
// The terms "encode" and "decode" are used for syntactic functionality |
| 58 |
// that is concerned with processing JSON based on its grammar, and |
| 59 |
// the terms "marshal" and "unmarshal" are used for semantic functionality |
| 60 |
// that determines the meaning of JSON values as Go values and vice-versa. |
| 61 |
// This package (i.e., [jsontext]) deals with JSON at a syntactic layer, |
| 62 |
// while [encoding/json/v2] deals with JSON at a semantic layer. |
| 63 |
// The goal is to provide a clear distinction between functionality that |
| 64 |
// is purely concerned with encoding versus that of marshaling. |
| 65 |
// For example, one can directly encode a stream of JSON tokens without |
| 66 |
// needing to marshal a concrete Go value representing them. |
| 67 |
// Similarly, one can decode a stream of JSON tokens without |
| 68 |
// needing to unmarshal them into a concrete Go value. |
| 69 |
// |
| 70 |
// This package uses JSON terminology when discussing JSON, which may differ |
| 71 |
// from related concepts in Go or elsewhere in computing literature. |
| 72 |
// |
| 73 |
// - a JSON "object" refers to an unordered collection of name/value members. |
| 74 |
// - a JSON "array" refers to an ordered sequence of elements. |
| 75 |
// - a JSON "value" refers to either a literal (i.e., null, false, or true), |
| 76 |
// string, number, object, or array. |
| 77 |
// |
| 78 |
// See RFC 8259 for more information. |
| 79 |
// |
| 80 |
// # Specifications |
| 81 |
// |
| 82 |
// Relevant specifications include RFC 4627, RFC 7159, RFC 7493, RFC 8259, |
| 83 |
// and RFC 8785. Each RFC is generally a stricter subset of another RFC. |
| 84 |
// In increasing order of strictness: |
| 85 |
// |
| 86 |
// - RFC 4627 and RFC 7159 do not require (but recommend) the use of UTF-8 |
| 87 |
// and also do not require (but recommend) that object names be unique. |
| 88 |
// - RFC 8259 requires the use of UTF-8, |
| 89 |
// but does not require (but recommends) that object names be unique. |
| 90 |
// - RFC 7493 requires the use of UTF-8 |
| 91 |
// and also requires that object names be unique. |
| 92 |
// - RFC 8785 defines a canonical representation. It requires the use of UTF-8 |
| 93 |
// and also requires that object names be unique and in a specific ordering. |
| 94 |
// It specifies exactly how strings and numbers must be formatted. |
| 95 |
// |
| 96 |
// The primary difference between RFC 4627 and RFC 7159 is that the former |
| 97 |
// restricted top-level values to only JSON objects and arrays, while |
| 98 |
// RFC 7159 and subsequent RFCs permit top-level values to additionally be |
| 99 |
// JSON nulls, booleans, strings, or numbers. |
| 100 |
// |
| 101 |
// By default, this package operates on RFC 7493, but can be configured |
| 102 |
// to operate according to the other RFC specifications. |
| 103 |
// RFC 7493 is a stricter subset of RFC 8259 and fully compliant with it. |
| 104 |
// In particular, it makes specific choices about behavior that RFC 8259 |
| 105 |
// leaves as undefined in order to ensure greater interoperability. |
| 106 |
// |
| 107 |
// # Security Considerations |
| 108 |
// |
| 109 |
// See the "Security Considerations" section in [encoding/json/v2]. |
| 110 |
package jsontext |
| 111 |
|
| 112 |
// requireKeyedLiterals can be embedded in a struct to require keyed literals. |
| 113 |
type requireKeyedLiterals struct{} |
| 114 |
|
| 115 |
// nonComparable can be embedded in a struct to prevent comparability. |
| 116 |
type nonComparable [0]func() |