arshal_any.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright 2022 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. package json
  5. import (
  6. "cmp"
  7. "reflect"
  8. "strconv"
  9. "github.com/go-json-experiment/json/internal"
  10. "github.com/go-json-experiment/json/internal/jsonflags"
  11. "github.com/go-json-experiment/json/internal/jsonopts"
  12. "github.com/go-json-experiment/json/internal/jsonwire"
  13. "github.com/go-json-experiment/json/jsontext"
  14. )
  15. // This file contains an optimized marshal and unmarshal implementation
  16. // for the any type. This type is often used when the Go program has
  17. // no knowledge of the JSON schema. This is a common enough occurrence
  18. // to justify the complexity of adding logic for this.
  19. // marshalValueAny marshals a Go any as a JSON value.
  20. // This assumes that there are no special formatting directives
  21. // for any possible nested value.
  22. func marshalValueAny(enc *jsontext.Encoder, val any, mo *jsonopts.Struct) error {
  23. switch val := val.(type) {
  24. case nil:
  25. return enc.WriteToken(jsontext.Null)
  26. case bool:
  27. return enc.WriteToken(jsontext.Bool(val))
  28. case string:
  29. return enc.WriteToken(jsontext.String(val))
  30. case float64:
  31. return enc.WriteToken(jsontext.Float(val))
  32. case map[string]any:
  33. return marshalObjectAny(enc, val, mo)
  34. case []any:
  35. return marshalArrayAny(enc, val, mo)
  36. default:
  37. v := newAddressableValue(reflect.TypeOf(val))
  38. v.Set(reflect.ValueOf(val))
  39. marshal := lookupArshaler(v.Type()).marshal
  40. if mo.Marshalers != nil {
  41. marshal, _ = mo.Marshalers.(*Marshalers).lookup(marshal, v.Type())
  42. }
  43. return marshal(enc, v, mo)
  44. }
  45. }
  46. // unmarshalValueAny unmarshals a JSON value as a Go any.
  47. // This assumes that there are no special formatting directives
  48. // for any possible nested value.
  49. // Duplicate names must be rejected since this does not implement merging.
  50. func unmarshalValueAny(dec *jsontext.Decoder, uo *jsonopts.Struct) (any, error) {
  51. switch k := dec.PeekKind(); k {
  52. case '{':
  53. return unmarshalObjectAny(dec, uo)
  54. case '[':
  55. return unmarshalArrayAny(dec, uo)
  56. default:
  57. xd := export.Decoder(dec)
  58. var flags jsonwire.ValueFlags
  59. val, err := xd.ReadValue(&flags)
  60. if err != nil {
  61. return nil, err
  62. }
  63. switch val.Kind() {
  64. case 'n':
  65. return nil, nil
  66. case 'f':
  67. return false, nil
  68. case 't':
  69. return true, nil
  70. case '"':
  71. val = jsonwire.UnquoteMayCopy(val, flags.IsVerbatim())
  72. if xd.StringCache == nil {
  73. xd.StringCache = new(stringCache)
  74. }
  75. return makeString(xd.StringCache, val), nil
  76. case '0':
  77. if uo.Flags.Get(jsonflags.UnmarshalAnyWithRawNumber) {
  78. return internal.RawNumberOf(val), nil
  79. }
  80. fv, ok := jsonwire.ParseFloat(val, 64)
  81. if !ok {
  82. return fv, newUnmarshalErrorAfterWithValue(dec, float64Type, strconv.ErrRange)
  83. }
  84. return fv, nil
  85. default:
  86. panic("BUG: invalid kind: " + k.String())
  87. }
  88. }
  89. }
  90. // marshalObjectAny marshals a Go map[string]any as a JSON object
  91. // (or as a JSON null if nil and [jsonflags.FormatNilMapAsNull]).
  92. func marshalObjectAny(enc *jsontext.Encoder, obj map[string]any, mo *jsonopts.Struct) error {
  93. // Check for cycles.
  94. xe := export.Encoder(enc)
  95. if xe.Tokens.Depth() > startDetectingCyclesAfter {
  96. v := reflect.ValueOf(obj)
  97. if err := visitPointer(&xe.SeenPointers, v); err != nil {
  98. return newMarshalErrorBefore(enc, anyType, err)
  99. }
  100. defer leavePointer(&xe.SeenPointers, v)
  101. }
  102. // Handle empty maps.
  103. if len(obj) == 0 {
  104. if mo.Flags.Get(jsonflags.FormatNilMapAsNull) && obj == nil {
  105. return enc.WriteToken(jsontext.Null)
  106. }
  107. // Optimize for marshaling an empty map without any preceding whitespace.
  108. if !mo.Flags.Get(jsonflags.AnyWhitespace) && !xe.Tokens.Last.NeedObjectName() {
  109. xe.Buf = append(xe.Tokens.MayAppendDelim(xe.Buf, '{'), "{}"...)
  110. xe.Tokens.Last.Increment()
  111. if xe.NeedFlush() {
  112. return xe.Flush()
  113. }
  114. return nil
  115. }
  116. }
  117. if err := enc.WriteToken(jsontext.ObjectStart); err != nil {
  118. return err
  119. }
  120. // A Go map guarantees that each entry has a unique key
  121. // The only possibility of duplicates is due to invalid UTF-8.
  122. if !mo.Flags.Get(jsonflags.AllowInvalidUTF8) {
  123. xe.Tokens.Last.DisableNamespace()
  124. }
  125. if !mo.Flags.Get(jsonflags.Deterministic) || len(obj) <= 1 {
  126. for name, val := range obj {
  127. if err := enc.WriteToken(jsontext.String(name)); err != nil {
  128. return err
  129. }
  130. if err := marshalValueAny(enc, val, mo); err != nil {
  131. return err
  132. }
  133. }
  134. } else {
  135. names := getStrings(len(obj))
  136. var i int
  137. for name := range obj {
  138. (*names)[i] = name
  139. i++
  140. }
  141. names.Sort()
  142. for _, name := range *names {
  143. if err := enc.WriteToken(jsontext.String(name)); err != nil {
  144. return err
  145. }
  146. if err := marshalValueAny(enc, obj[name], mo); err != nil {
  147. return err
  148. }
  149. }
  150. putStrings(names)
  151. }
  152. if err := enc.WriteToken(jsontext.ObjectEnd); err != nil {
  153. return err
  154. }
  155. return nil
  156. }
  157. // unmarshalObjectAny unmarshals a JSON object as a Go map[string]any.
  158. // It panics if not decoding a JSON object.
  159. func unmarshalObjectAny(dec *jsontext.Decoder, uo *jsonopts.Struct) (map[string]any, error) {
  160. switch tok, err := dec.ReadToken(); {
  161. case err != nil:
  162. return nil, err
  163. case tok.Kind() != '{':
  164. panic("BUG: invalid kind: " + tok.Kind().String())
  165. }
  166. obj := make(map[string]any)
  167. // A Go map guarantees that each entry has a unique key
  168. // The only possibility of duplicates is due to invalid UTF-8.
  169. if !uo.Flags.Get(jsonflags.AllowInvalidUTF8) {
  170. export.Decoder(dec).Tokens.Last.DisableNamespace()
  171. }
  172. var errUnmarshal error
  173. for dec.PeekKind() != '}' {
  174. tok, err := dec.ReadToken()
  175. if err != nil {
  176. return obj, err
  177. }
  178. name := tok.String()
  179. // Manually check for duplicate names.
  180. if _, ok := obj[name]; ok {
  181. // TODO: Unread the object name.
  182. name := export.Decoder(dec).PreviousTokenOrValue()
  183. err := newDuplicateNameError(dec.StackPointer(), nil, dec.InputOffset()-len64(name))
  184. return obj, err
  185. }
  186. val, err := unmarshalValueAny(dec, uo)
  187. obj[name] = val
  188. if err != nil {
  189. if isFatalError(err, uo.Flags) {
  190. return obj, err
  191. }
  192. errUnmarshal = cmp.Or(err, errUnmarshal)
  193. }
  194. }
  195. if _, err := dec.ReadToken(); err != nil {
  196. return obj, err
  197. }
  198. return obj, errUnmarshal
  199. }
  200. // marshalArrayAny marshals a Go []any as a JSON array
  201. // (or as a JSON null if nil and [jsonflags.FormatNilSliceAsNull]).
  202. func marshalArrayAny(enc *jsontext.Encoder, arr []any, mo *jsonopts.Struct) error {
  203. // Check for cycles.
  204. xe := export.Encoder(enc)
  205. if xe.Tokens.Depth() > startDetectingCyclesAfter {
  206. v := reflect.ValueOf(arr)
  207. if err := visitPointer(&xe.SeenPointers, v); err != nil {
  208. return newMarshalErrorBefore(enc, sliceAnyType, err)
  209. }
  210. defer leavePointer(&xe.SeenPointers, v)
  211. }
  212. // Handle empty slices.
  213. if len(arr) == 0 {
  214. if mo.Flags.Get(jsonflags.FormatNilSliceAsNull) && arr == nil {
  215. return enc.WriteToken(jsontext.Null)
  216. }
  217. // Optimize for marshaling an empty slice without any preceding whitespace.
  218. if !mo.Flags.Get(jsonflags.AnyWhitespace) && !xe.Tokens.Last.NeedObjectName() {
  219. xe.Buf = append(xe.Tokens.MayAppendDelim(xe.Buf, '['), "[]"...)
  220. xe.Tokens.Last.Increment()
  221. if xe.NeedFlush() {
  222. return xe.Flush()
  223. }
  224. return nil
  225. }
  226. }
  227. if err := enc.WriteToken(jsontext.ArrayStart); err != nil {
  228. return err
  229. }
  230. for _, val := range arr {
  231. if err := marshalValueAny(enc, val, mo); err != nil {
  232. return err
  233. }
  234. }
  235. if err := enc.WriteToken(jsontext.ArrayEnd); err != nil {
  236. return err
  237. }
  238. return nil
  239. }
  240. // unmarshalArrayAny unmarshals a JSON array as a Go []any.
  241. // It panics if not decoding a JSON array.
  242. func unmarshalArrayAny(dec *jsontext.Decoder, uo *jsonopts.Struct) ([]any, error) {
  243. switch tok, err := dec.ReadToken(); {
  244. case err != nil:
  245. return nil, err
  246. case tok.Kind() != '[':
  247. panic("BUG: invalid kind: " + tok.Kind().String())
  248. }
  249. arr := []any{}
  250. var errUnmarshal error
  251. for dec.PeekKind() != ']' {
  252. val, err := unmarshalValueAny(dec, uo)
  253. arr = append(arr, val)
  254. if err != nil {
  255. if isFatalError(err, uo.Flags) {
  256. return arr, err
  257. }
  258. errUnmarshal = cmp.Or(errUnmarshal, err)
  259. }
  260. }
  261. if _, err := dec.ReadToken(); err != nil {
  262. return arr, err
  263. }
  264. return arr, errUnmarshal
  265. }