json.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package json
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "github.com/goccy/go-json/internal/encoder"
  7. )
  8. // Marshaler is the interface implemented by types that
  9. // can marshal themselves into valid JSON.
  10. type Marshaler interface {
  11. MarshalJSON() ([]byte, error)
  12. }
  13. // MarshalerContext is the interface implemented by types that
  14. // can marshal themselves into valid JSON with context.Context.
  15. type MarshalerContext interface {
  16. MarshalJSON(context.Context) ([]byte, error)
  17. }
  18. // Unmarshaler is the interface implemented by types
  19. // that can unmarshal a JSON description of themselves.
  20. // The input can be assumed to be a valid encoding of
  21. // a JSON value. UnmarshalJSON must copy the JSON data
  22. // if it wishes to retain the data after returning.
  23. //
  24. // By convention, to approximate the behavior of Unmarshal itself,
  25. // Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op.
  26. type Unmarshaler interface {
  27. UnmarshalJSON([]byte) error
  28. }
  29. // UnmarshalerContext is the interface implemented by types
  30. // that can unmarshal with context.Context a JSON description of themselves.
  31. type UnmarshalerContext interface {
  32. UnmarshalJSON(context.Context, []byte) error
  33. }
  34. // Marshal returns the JSON encoding of v.
  35. //
  36. // Marshal traverses the value v recursively.
  37. // If an encountered value implements the Marshaler interface
  38. // and is not a nil pointer, Marshal calls its MarshalJSON method
  39. // to produce JSON. If no MarshalJSON method is present but the
  40. // value implements encoding.TextMarshaler instead, Marshal calls
  41. // its MarshalText method and encodes the result as a JSON string.
  42. // The nil pointer exception is not strictly necessary
  43. // but mimics a similar, necessary exception in the behavior of
  44. // UnmarshalJSON.
  45. //
  46. // Otherwise, Marshal uses the following type-dependent default encodings:
  47. //
  48. // Boolean values encode as JSON booleans.
  49. //
  50. // Floating point, integer, and Number values encode as JSON numbers.
  51. //
  52. // String values encode as JSON strings coerced to valid UTF-8,
  53. // replacing invalid bytes with the Unicode replacement rune.
  54. // The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e"
  55. // to keep some browsers from misinterpreting JSON output as HTML.
  56. // Ampersand "&" is also escaped to "\u0026" for the same reason.
  57. // This escaping can be disabled using an Encoder that had SetEscapeHTML(false)
  58. // called on it.
  59. //
  60. // Array and slice values encode as JSON arrays, except that
  61. // []byte encodes as a base64-encoded string, and a nil slice
  62. // encodes as the null JSON value.
  63. //
  64. // Struct values encode as JSON objects.
  65. // Each exported struct field becomes a member of the object, using the
  66. // field name as the object key, unless the field is omitted for one of the
  67. // reasons given below.
  68. //
  69. // The encoding of each struct field can be customized by the format string
  70. // stored under the "json" key in the struct field's tag.
  71. // The format string gives the name of the field, possibly followed by a
  72. // comma-separated list of options. The name may be empty in order to
  73. // specify options without overriding the default field name.
  74. //
  75. // The "omitempty" option specifies that the field should be omitted
  76. // from the encoding if the field has an empty value, defined as
  77. // false, 0, a nil pointer, a nil interface value, and any empty array,
  78. // slice, map, or string.
  79. //
  80. // As a special case, if the field tag is "-", the field is always omitted.
  81. // Note that a field with name "-" can still be generated using the tag "-,".
  82. //
  83. // Examples of struct field tags and their meanings:
  84. //
  85. // // Field appears in JSON as key "myName".
  86. // Field int `json:"myName"`
  87. //
  88. // // Field appears in JSON as key "myName" and
  89. // // the field is omitted from the object if its value is empty,
  90. // // as defined above.
  91. // Field int `json:"myName,omitempty"`
  92. //
  93. // // Field appears in JSON as key "Field" (the default), but
  94. // // the field is skipped if empty.
  95. // // Note the leading comma.
  96. // Field int `json:",omitempty"`
  97. //
  98. // // Field is ignored by this package.
  99. // Field int `json:"-"`
  100. //
  101. // // Field appears in JSON as key "-".
  102. // Field int `json:"-,"`
  103. //
  104. // The "string" option signals that a field is stored as JSON inside a
  105. // JSON-encoded string. It applies only to fields of string, floating point,
  106. // integer, or boolean types. This extra level of encoding is sometimes used
  107. // when communicating with JavaScript programs:
  108. //
  109. // Int64String int64 `json:",string"`
  110. //
  111. // The key name will be used if it's a non-empty string consisting of
  112. // only Unicode letters, digits, and ASCII punctuation except quotation
  113. // marks, backslash, and comma.
  114. //
  115. // Anonymous struct fields are usually marshaled as if their inner exported fields
  116. // were fields in the outer struct, subject to the usual Go visibility rules amended
  117. // as described in the next paragraph.
  118. // An anonymous struct field with a name given in its JSON tag is treated as
  119. // having that name, rather than being anonymous.
  120. // An anonymous struct field of interface type is treated the same as having
  121. // that type as its name, rather than being anonymous.
  122. //
  123. // The Go visibility rules for struct fields are amended for JSON when
  124. // deciding which field to marshal or unmarshal. If there are
  125. // multiple fields at the same level, and that level is the least
  126. // nested (and would therefore be the nesting level selected by the
  127. // usual Go rules), the following extra rules apply:
  128. //
  129. // 1) Of those fields, if any are JSON-tagged, only tagged fields are considered,
  130. // even if there are multiple untagged fields that would otherwise conflict.
  131. //
  132. // 2) If there is exactly one field (tagged or not according to the first rule), that is selected.
  133. //
  134. // 3) Otherwise there are multiple fields, and all are ignored; no error occurs.
  135. //
  136. // Handling of anonymous struct fields is new in Go 1.1.
  137. // Prior to Go 1.1, anonymous struct fields were ignored. To force ignoring of
  138. // an anonymous struct field in both current and earlier versions, give the field
  139. // a JSON tag of "-".
  140. //
  141. // Map values encode as JSON objects. The map's key type must either be a
  142. // string, an integer type, or implement encoding.TextMarshaler. The map keys
  143. // are sorted and used as JSON object keys by applying the following rules,
  144. // subject to the UTF-8 coercion described for string values above:
  145. // - string keys are used directly
  146. // - encoding.TextMarshalers are marshaled
  147. // - integer keys are converted to strings
  148. //
  149. // Pointer values encode as the value pointed to.
  150. // A nil pointer encodes as the null JSON value.
  151. //
  152. // Interface values encode as the value contained in the interface.
  153. // A nil interface value encodes as the null JSON value.
  154. //
  155. // Channel, complex, and function values cannot be encoded in JSON.
  156. // Attempting to encode such a value causes Marshal to return
  157. // an UnsupportedTypeError.
  158. //
  159. // JSON cannot represent cyclic data structures and Marshal does not
  160. // handle them. Passing cyclic structures to Marshal will result in
  161. // an infinite recursion.
  162. //
  163. func Marshal(v interface{}) ([]byte, error) {
  164. return MarshalWithOption(v)
  165. }
  166. // MarshalNoEscape returns the JSON encoding of v and doesn't escape v.
  167. func MarshalNoEscape(v interface{}) ([]byte, error) {
  168. return marshalNoEscape(v)
  169. }
  170. // MarshalContext returns the JSON encoding of v with context.Context and EncodeOption.
  171. func MarshalContext(ctx context.Context, v interface{}, optFuncs ...EncodeOptionFunc) ([]byte, error) {
  172. return marshalContext(ctx, v, optFuncs...)
  173. }
  174. // MarshalWithOption returns the JSON encoding of v with EncodeOption.
  175. func MarshalWithOption(v interface{}, optFuncs ...EncodeOptionFunc) ([]byte, error) {
  176. return marshal(v, optFuncs...)
  177. }
  178. // MarshalIndent is like Marshal but applies Indent to format the output.
  179. // Each JSON element in the output will begin on a new line beginning with prefix
  180. // followed by one or more copies of indent according to the indentation nesting.
  181. func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
  182. return MarshalIndentWithOption(v, prefix, indent)
  183. }
  184. // MarshalIndentWithOption is like Marshal but applies Indent to format the output with EncodeOption.
  185. func MarshalIndentWithOption(v interface{}, prefix, indent string, optFuncs ...EncodeOptionFunc) ([]byte, error) {
  186. return marshalIndent(v, prefix, indent, optFuncs...)
  187. }
  188. // Unmarshal parses the JSON-encoded data and stores the result
  189. // in the value pointed to by v. If v is nil or not a pointer,
  190. // Unmarshal returns an InvalidUnmarshalError.
  191. //
  192. // Unmarshal uses the inverse of the encodings that
  193. // Marshal uses, allocating maps, slices, and pointers as necessary,
  194. // with the following additional rules:
  195. //
  196. // To unmarshal JSON into a pointer, Unmarshal first handles the case of
  197. // the JSON being the JSON literal null. In that case, Unmarshal sets
  198. // the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into
  199. // the value pointed at by the pointer. If the pointer is nil, Unmarshal
  200. // allocates a new value for it to point to.
  201. //
  202. // To unmarshal JSON into a value implementing the Unmarshaler interface,
  203. // Unmarshal calls that value's UnmarshalJSON method, including
  204. // when the input is a JSON null.
  205. // Otherwise, if the value implements encoding.TextUnmarshaler
  206. // and the input is a JSON quoted string, Unmarshal calls that value's
  207. // UnmarshalText method with the unquoted form of the string.
  208. //
  209. // To unmarshal JSON into a struct, Unmarshal matches incoming object
  210. // keys to the keys used by Marshal (either the struct field name or its tag),
  211. // preferring an exact match but also accepting a case-insensitive match. By
  212. // default, object keys which don't have a corresponding struct field are
  213. // ignored (see Decoder.DisallowUnknownFields for an alternative).
  214. //
  215. // To unmarshal JSON into an interface value,
  216. // Unmarshal stores one of these in the interface value:
  217. //
  218. // bool, for JSON booleans
  219. // float64, for JSON numbers
  220. // string, for JSON strings
  221. // []interface{}, for JSON arrays
  222. // map[string]interface{}, for JSON objects
  223. // nil for JSON null
  224. //
  225. // To unmarshal a JSON array into a slice, Unmarshal resets the slice length
  226. // to zero and then appends each element to the slice.
  227. // As a special case, to unmarshal an empty JSON array into a slice,
  228. // Unmarshal replaces the slice with a new empty slice.
  229. //
  230. // To unmarshal a JSON array into a Go array, Unmarshal decodes
  231. // JSON array elements into corresponding Go array elements.
  232. // If the Go array is smaller than the JSON array,
  233. // the additional JSON array elements are discarded.
  234. // If the JSON array is smaller than the Go array,
  235. // the additional Go array elements are set to zero values.
  236. //
  237. // To unmarshal a JSON object into a map, Unmarshal first establishes a map to
  238. // use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal
  239. // reuses the existing map, keeping existing entries. Unmarshal then stores
  240. // key-value pairs from the JSON object into the map. The map's key type must
  241. // either be any string type, an integer, implement json.Unmarshaler, or
  242. // implement encoding.TextUnmarshaler.
  243. //
  244. // If a JSON value is not appropriate for a given target type,
  245. // or if a JSON number overflows the target type, Unmarshal
  246. // skips that field and completes the unmarshaling as best it can.
  247. // If no more serious errors are encountered, Unmarshal returns
  248. // an UnmarshalTypeError describing the earliest such error. In any
  249. // case, it's not guaranteed that all the remaining fields following
  250. // the problematic one will be unmarshaled into the target object.
  251. //
  252. // The JSON null value unmarshals into an interface, map, pointer, or slice
  253. // by setting that Go value to nil. Because null is often used in JSON to mean
  254. // ``not present,'' unmarshaling a JSON null into any other Go type has no effect
  255. // on the value and produces no error.
  256. //
  257. // When unmarshaling quoted strings, invalid UTF-8 or
  258. // invalid UTF-16 surrogate pairs are not treated as an error.
  259. // Instead, they are replaced by the Unicode replacement
  260. // character U+FFFD.
  261. //
  262. func Unmarshal(data []byte, v interface{}) error {
  263. return unmarshal(data, v)
  264. }
  265. // UnmarshalContext parses the JSON-encoded data and stores the result
  266. // in the value pointed to by v. If you implement the UnmarshalerContext interface,
  267. // call it with ctx as an argument.
  268. func UnmarshalContext(ctx context.Context, data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error {
  269. return unmarshalContext(ctx, data, v)
  270. }
  271. func UnmarshalWithOption(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error {
  272. return unmarshal(data, v, optFuncs...)
  273. }
  274. func UnmarshalNoEscape(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error {
  275. return unmarshalNoEscape(data, v, optFuncs...)
  276. }
  277. // A Token holds a value of one of these types:
  278. //
  279. // Delim, for the four JSON delimiters [ ] { }
  280. // bool, for JSON booleans
  281. // float64, for JSON numbers
  282. // Number, for JSON numbers
  283. // string, for JSON string literals
  284. // nil, for JSON null
  285. //
  286. type Token = json.Token
  287. // A Number represents a JSON number literal.
  288. type Number = json.Number
  289. // RawMessage is a raw encoded JSON value.
  290. // It implements Marshaler and Unmarshaler and can
  291. // be used to delay JSON decoding or precompute a JSON encoding.
  292. type RawMessage = json.RawMessage
  293. // A Delim is a JSON array or object delimiter, one of [ ] { or }.
  294. type Delim = json.Delim
  295. // Compact appends to dst the JSON-encoded src with
  296. // insignificant space characters elided.
  297. func Compact(dst *bytes.Buffer, src []byte) error {
  298. return encoder.Compact(dst, src, false)
  299. }
  300. // Indent appends to dst an indented form of the JSON-encoded src.
  301. // Each element in a JSON object or array begins on a new,
  302. // indented line beginning with prefix followed by one or more
  303. // copies of indent according to the indentation nesting.
  304. // The data appended to dst does not begin with the prefix nor
  305. // any indentation, to make it easier to embed inside other formatted JSON data.
  306. // Although leading space characters (space, tab, carriage return, newline)
  307. // at the beginning of src are dropped, trailing space characters
  308. // at the end of src are preserved and copied to dst.
  309. // For example, if src has no trailing spaces, neither will dst;
  310. // if src ends in a trailing newline, so will dst.
  311. func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
  312. return encoder.Indent(dst, src, prefix, indent)
  313. }
  314. // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
  315. // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
  316. // so that the JSON will be safe to embed inside HTML <script> tags.
  317. // For historical reasons, web browsers don't honor standard HTML
  318. // escaping within <script> tags, so an alternative JSON encoding must
  319. // be used.
  320. func HTMLEscape(dst *bytes.Buffer, src []byte) {
  321. var v interface{}
  322. dec := NewDecoder(bytes.NewBuffer(src))
  323. dec.UseNumber()
  324. if err := dec.Decode(&v); err != nil {
  325. return
  326. }
  327. buf, _ := marshal(v)
  328. dst.Write(buf)
  329. }
  330. // Valid reports whether data is a valid JSON encoding.
  331. func Valid(data []byte) bool {
  332. var v interface{}
  333. decoder := NewDecoder(bytes.NewReader(data))
  334. err := decoder.Decode(&v)
  335. if err != nil {
  336. return false
  337. }
  338. if !decoder.More() {
  339. return true
  340. }
  341. return decoder.InputOffset() >= int64(len(data))
  342. }
  343. func init() {
  344. encoder.Marshal = Marshal
  345. encoder.Unmarshal = Unmarshal
  346. }