arshal_inlined.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2020 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. "bytes"
  7. "errors"
  8. "io"
  9. "reflect"
  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 package supports "inlining" a Go struct field, where the contents
  16. // of the serialized field (which must be a JSON object) are treated as if
  17. // they are part of the parent Go struct (which represents a JSON object).
  18. //
  19. // Generally, inlined fields are of a Go struct type, where the fields of the
  20. // nested struct are virtually hoisted up to the parent struct using rules
  21. // similar to how Go embedding works (but operating within the JSON namespace).
  22. //
  23. // However, inlined fields may also be of a Go map type with a string key or
  24. // a jsontext.Value. Such inlined fields are called "fallback" fields since they
  25. // represent any arbitrary JSON object member. Explicitly named fields take
  26. // precedence over the inlined fallback. Only one inlined fallback is allowed.
  27. var errRawInlinedNotObject = errors.New("inlined raw value must be a JSON object")
  28. var jsontextValueType = reflect.TypeFor[jsontext.Value]()
  29. // marshalInlinedFallbackAll marshals all the members in an inlined fallback.
  30. func marshalInlinedFallbackAll(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct, f *structField, insertUnquotedName func([]byte) bool) error {
  31. v := addressableValue{va.Field(f.index0), va.forcedAddr} // addressable if struct value is addressable
  32. if len(f.index) > 0 {
  33. v = v.fieldByIndex(f.index, false)
  34. if !v.IsValid() {
  35. return nil // implies a nil inlined field
  36. }
  37. }
  38. v = v.indirect(false)
  39. if !v.IsValid() {
  40. return nil
  41. }
  42. if v.Type() == jsontextValueType {
  43. // TODO(https://go.dev/issue/62121): Use reflect.Value.AssertTo.
  44. b := *v.Addr().Interface().(*jsontext.Value)
  45. if len(b) == 0 { // TODO: Should this be nil? What if it were all whitespace?
  46. return nil
  47. }
  48. dec := export.GetBufferedDecoder(b)
  49. defer export.PutBufferedDecoder(dec)
  50. xd := export.Decoder(dec)
  51. xd.Flags.Set(jsonflags.AllowDuplicateNames | jsonflags.AllowInvalidUTF8 | 1)
  52. tok, err := dec.ReadToken()
  53. if err != nil {
  54. if err == io.EOF {
  55. err = io.ErrUnexpectedEOF
  56. }
  57. return newMarshalErrorBefore(enc, v.Type(), err)
  58. }
  59. if tok.Kind() != '{' {
  60. return newMarshalErrorBefore(enc, v.Type(), errRawInlinedNotObject)
  61. }
  62. for dec.PeekKind() != '}' {
  63. // Parse the JSON object name.
  64. var flags jsonwire.ValueFlags
  65. val, err := xd.ReadValue(&flags)
  66. if err != nil {
  67. return newMarshalErrorBefore(enc, v.Type(), err)
  68. }
  69. if insertUnquotedName != nil {
  70. name := jsonwire.UnquoteMayCopy(val, flags.IsVerbatim())
  71. if !insertUnquotedName(name) {
  72. return newDuplicateNameError(enc.StackPointer().Parent(), val, enc.OutputOffset())
  73. }
  74. }
  75. if err := enc.WriteValue(val); err != nil {
  76. return err
  77. }
  78. // Parse the JSON object value.
  79. val, err = xd.ReadValue(&flags)
  80. if err != nil {
  81. return newMarshalErrorBefore(enc, v.Type(), err)
  82. }
  83. if err := enc.WriteValue(val); err != nil {
  84. return err
  85. }
  86. }
  87. if _, err := dec.ReadToken(); err != nil {
  88. return newMarshalErrorBefore(enc, v.Type(), err)
  89. }
  90. if err := xd.CheckEOF(); err != nil {
  91. return newMarshalErrorBefore(enc, v.Type(), err)
  92. }
  93. return nil
  94. } else {
  95. m := v // must be a map[~string]V
  96. n := m.Len()
  97. if n == 0 {
  98. return nil
  99. }
  100. mk := newAddressableValue(m.Type().Key())
  101. mv := newAddressableValue(m.Type().Elem())
  102. marshalKey := func(mk addressableValue) error {
  103. b, err := jsonwire.AppendQuote(enc.UnusedBuffer(), mk.String(), &mo.Flags)
  104. if err != nil {
  105. return newMarshalErrorBefore(enc, m.Type().Key(), err)
  106. }
  107. if insertUnquotedName != nil {
  108. isVerbatim := bytes.IndexByte(b, '\\') < 0
  109. name := jsonwire.UnquoteMayCopy(b, isVerbatim)
  110. if !insertUnquotedName(name) {
  111. return newDuplicateNameError(enc.StackPointer().Parent(), b, enc.OutputOffset())
  112. }
  113. }
  114. return enc.WriteValue(b)
  115. }
  116. marshalVal := f.fncs.marshal
  117. if mo.Marshalers != nil {
  118. marshalVal, _ = mo.Marshalers.(*Marshalers).lookup(marshalVal, mv.Type())
  119. }
  120. if !mo.Flags.Get(jsonflags.Deterministic) || n <= 1 {
  121. for iter := m.MapRange(); iter.Next(); {
  122. mk.SetIterKey(iter)
  123. if err := marshalKey(mk); err != nil {
  124. return err
  125. }
  126. mv.Set(iter.Value())
  127. if err := marshalVal(enc, mv, mo); err != nil {
  128. return err
  129. }
  130. }
  131. } else {
  132. names := getStrings(n)
  133. for i, iter := 0, m.Value.MapRange(); i < n && iter.Next(); i++ {
  134. mk.SetIterKey(iter)
  135. (*names)[i] = mk.String()
  136. }
  137. names.Sort()
  138. for _, name := range *names {
  139. mk.SetString(name)
  140. if err := marshalKey(mk); err != nil {
  141. return err
  142. }
  143. // TODO(https://go.dev/issue/57061): Use mv.SetMapIndexOf.
  144. mv.Set(m.MapIndex(mk.Value))
  145. if err := marshalVal(enc, mv, mo); err != nil {
  146. return err
  147. }
  148. }
  149. putStrings(names)
  150. }
  151. return nil
  152. }
  153. }
  154. // unmarshalInlinedFallbackNext unmarshals only the next member in an inlined fallback.
  155. func unmarshalInlinedFallbackNext(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct, f *structField, quotedName, unquotedName []byte) error {
  156. v := addressableValue{va.Field(f.index0), va.forcedAddr} // addressable if struct value is addressable
  157. if len(f.index) > 0 {
  158. v = v.fieldByIndex(f.index, true)
  159. }
  160. v = v.indirect(true)
  161. if v.Type() == jsontextValueType {
  162. b := v.Addr().Interface().(*jsontext.Value)
  163. if len(*b) == 0 { // TODO: Should this be nil? What if it were all whitespace?
  164. *b = append(*b, '{')
  165. } else {
  166. *b = jsonwire.TrimSuffixWhitespace(*b)
  167. if jsonwire.HasSuffixByte(*b, '}') {
  168. // TODO: When merging into an object for the first time,
  169. // should we verify that it is valid?
  170. *b = jsonwire.TrimSuffixByte(*b, '}')
  171. *b = jsonwire.TrimSuffixWhitespace(*b)
  172. if !jsonwire.HasSuffixByte(*b, ',') && !jsonwire.HasSuffixByte(*b, '{') {
  173. *b = append(*b, ',')
  174. }
  175. } else {
  176. return newUnmarshalErrorAfterWithSkipping(dec, uo, v.Type(), errRawInlinedNotObject)
  177. }
  178. }
  179. *b = append(*b, quotedName...)
  180. *b = append(*b, ':')
  181. val, err := dec.ReadValue()
  182. if err != nil {
  183. return err
  184. }
  185. *b = append(*b, val...)
  186. *b = append(*b, '}')
  187. return nil
  188. } else {
  189. name := string(unquotedName) // TODO: Intern this?
  190. m := v // must be a map[~string]V
  191. if m.IsNil() {
  192. m.Set(reflect.MakeMap(m.Type()))
  193. }
  194. mk := reflect.ValueOf(name)
  195. if mkt := m.Type().Key(); mkt != stringType {
  196. mk = mk.Convert(mkt)
  197. }
  198. mv := newAddressableValue(m.Type().Elem()) // TODO: Cache across calls?
  199. if v2 := m.MapIndex(mk); v2.IsValid() {
  200. mv.Set(v2)
  201. }
  202. unmarshal := f.fncs.unmarshal
  203. if uo.Unmarshalers != nil {
  204. unmarshal, _ = uo.Unmarshalers.(*Unmarshalers).lookup(unmarshal, mv.Type())
  205. }
  206. err := unmarshal(dec, mv, uo)
  207. m.SetMapIndex(mk, mv.Value)
  208. if err != nil {
  209. return err
  210. }
  211. return nil
  212. }
  213. }