decode.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package json
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. "unsafe"
  8. "github.com/goccy/go-json/internal/decoder"
  9. "github.com/goccy/go-json/internal/errors"
  10. "github.com/goccy/go-json/internal/runtime"
  11. )
  12. type Decoder struct {
  13. s *decoder.Stream
  14. }
  15. const (
  16. nul = '\000'
  17. )
  18. type emptyInterface struct {
  19. typ *runtime.Type
  20. ptr unsafe.Pointer
  21. }
  22. func unmarshal(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error {
  23. src := make([]byte, len(data)+1) // append nul byte to the end
  24. copy(src, data)
  25. header := (*emptyInterface)(unsafe.Pointer(&v))
  26. if err := validateType(header.typ, uintptr(header.ptr)); err != nil {
  27. return err
  28. }
  29. dec, err := decoder.CompileToGetDecoder(header.typ)
  30. if err != nil {
  31. return err
  32. }
  33. ctx := decoder.TakeRuntimeContext()
  34. ctx.Buf = src
  35. ctx.Option.Flags = 0
  36. for _, optFunc := range optFuncs {
  37. optFunc(ctx.Option)
  38. }
  39. cursor, err := dec.Decode(ctx, 0, 0, header.ptr)
  40. if err != nil {
  41. decoder.ReleaseRuntimeContext(ctx)
  42. return err
  43. }
  44. decoder.ReleaseRuntimeContext(ctx)
  45. return validateEndBuf(src, cursor)
  46. }
  47. func unmarshalContext(ctx context.Context, data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error {
  48. src := make([]byte, len(data)+1) // append nul byte to the end
  49. copy(src, data)
  50. header := (*emptyInterface)(unsafe.Pointer(&v))
  51. if err := validateType(header.typ, uintptr(header.ptr)); err != nil {
  52. return err
  53. }
  54. dec, err := decoder.CompileToGetDecoder(header.typ)
  55. if err != nil {
  56. return err
  57. }
  58. rctx := decoder.TakeRuntimeContext()
  59. rctx.Buf = src
  60. rctx.Option.Flags = 0
  61. rctx.Option.Flags |= decoder.ContextOption
  62. rctx.Option.Context = ctx
  63. for _, optFunc := range optFuncs {
  64. optFunc(rctx.Option)
  65. }
  66. cursor, err := dec.Decode(rctx, 0, 0, header.ptr)
  67. if err != nil {
  68. decoder.ReleaseRuntimeContext(rctx)
  69. return err
  70. }
  71. decoder.ReleaseRuntimeContext(rctx)
  72. return validateEndBuf(src, cursor)
  73. }
  74. var (
  75. pathDecoder = decoder.NewPathDecoder()
  76. )
  77. func extractFromPath(path *Path, data []byte, optFuncs ...DecodeOptionFunc) ([][]byte, error) {
  78. if path.path.RootSelectorOnly {
  79. return [][]byte{data}, nil
  80. }
  81. src := make([]byte, len(data)+1) // append nul byte to the end
  82. copy(src, data)
  83. ctx := decoder.TakeRuntimeContext()
  84. ctx.Buf = src
  85. ctx.Option.Flags = 0
  86. ctx.Option.Flags |= decoder.PathOption
  87. ctx.Option.Path = path.path
  88. for _, optFunc := range optFuncs {
  89. optFunc(ctx.Option)
  90. }
  91. paths, cursor, err := pathDecoder.DecodePath(ctx, 0, 0)
  92. if err != nil {
  93. decoder.ReleaseRuntimeContext(ctx)
  94. return nil, err
  95. }
  96. decoder.ReleaseRuntimeContext(ctx)
  97. if err := validateEndBuf(src, cursor); err != nil {
  98. return nil, err
  99. }
  100. return paths, nil
  101. }
  102. func unmarshalNoEscape(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error {
  103. src := make([]byte, len(data)+1) // append nul byte to the end
  104. copy(src, data)
  105. header := (*emptyInterface)(unsafe.Pointer(&v))
  106. if err := validateType(header.typ, uintptr(header.ptr)); err != nil {
  107. return err
  108. }
  109. dec, err := decoder.CompileToGetDecoder(header.typ)
  110. if err != nil {
  111. return err
  112. }
  113. ctx := decoder.TakeRuntimeContext()
  114. ctx.Buf = src
  115. ctx.Option.Flags = 0
  116. for _, optFunc := range optFuncs {
  117. optFunc(ctx.Option)
  118. }
  119. cursor, err := dec.Decode(ctx, 0, 0, noescape(header.ptr))
  120. if err != nil {
  121. decoder.ReleaseRuntimeContext(ctx)
  122. return err
  123. }
  124. decoder.ReleaseRuntimeContext(ctx)
  125. return validateEndBuf(src, cursor)
  126. }
  127. func validateEndBuf(src []byte, cursor int64) error {
  128. for {
  129. switch src[cursor] {
  130. case ' ', '\t', '\n', '\r':
  131. cursor++
  132. continue
  133. case nul:
  134. return nil
  135. }
  136. return errors.ErrSyntax(
  137. fmt.Sprintf("invalid character '%c' after top-level value", src[cursor]),
  138. cursor+1,
  139. )
  140. }
  141. }
  142. //nolint:staticcheck
  143. //go:nosplit
  144. func noescape(p unsafe.Pointer) unsafe.Pointer {
  145. x := uintptr(p)
  146. return unsafe.Pointer(x ^ 0)
  147. }
  148. func validateType(typ *runtime.Type, p uintptr) error {
  149. if typ == nil || typ.Kind() != reflect.Ptr || p == 0 {
  150. return &InvalidUnmarshalError{Type: runtime.RType2Type(typ)}
  151. }
  152. return nil
  153. }
  154. // NewDecoder returns a new decoder that reads from r.
  155. //
  156. // The decoder introduces its own buffering and may
  157. // read data from r beyond the JSON values requested.
  158. func NewDecoder(r io.Reader) *Decoder {
  159. s := decoder.NewStream(r)
  160. return &Decoder{
  161. s: s,
  162. }
  163. }
  164. // Buffered returns a reader of the data remaining in the Decoder's
  165. // buffer. The reader is valid until the next call to Decode.
  166. func (d *Decoder) Buffered() io.Reader {
  167. return d.s.Buffered()
  168. }
  169. // Decode reads the next JSON-encoded value from its
  170. // input and stores it in the value pointed to by v.
  171. //
  172. // See the documentation for Unmarshal for details about
  173. // the conversion of JSON into a Go value.
  174. func (d *Decoder) Decode(v interface{}) error {
  175. return d.DecodeWithOption(v)
  176. }
  177. // DecodeContext reads the next JSON-encoded value from its
  178. // input and stores it in the value pointed to by v with context.Context.
  179. func (d *Decoder) DecodeContext(ctx context.Context, v interface{}) error {
  180. d.s.Option.Flags |= decoder.ContextOption
  181. d.s.Option.Context = ctx
  182. return d.DecodeWithOption(v)
  183. }
  184. func (d *Decoder) DecodeWithOption(v interface{}, optFuncs ...DecodeOptionFunc) error {
  185. header := (*emptyInterface)(unsafe.Pointer(&v))
  186. typ := header.typ
  187. ptr := uintptr(header.ptr)
  188. typeptr := uintptr(unsafe.Pointer(typ))
  189. // noescape trick for header.typ ( reflect.*rtype )
  190. copiedType := *(**runtime.Type)(unsafe.Pointer(&typeptr))
  191. if err := validateType(copiedType, ptr); err != nil {
  192. return err
  193. }
  194. dec, err := decoder.CompileToGetDecoder(typ)
  195. if err != nil {
  196. return err
  197. }
  198. if err := d.s.PrepareForDecode(); err != nil {
  199. return err
  200. }
  201. s := d.s
  202. for _, optFunc := range optFuncs {
  203. optFunc(s.Option)
  204. }
  205. if err := dec.DecodeStream(s, 0, header.ptr); err != nil {
  206. return err
  207. }
  208. s.Reset()
  209. return nil
  210. }
  211. func (d *Decoder) More() bool {
  212. return d.s.More()
  213. }
  214. func (d *Decoder) Token() (Token, error) {
  215. return d.s.Token()
  216. }
  217. // DisallowUnknownFields causes the Decoder to return an error when the destination
  218. // is a struct and the input contains object keys which do not match any
  219. // non-ignored, exported fields in the destination.
  220. func (d *Decoder) DisallowUnknownFields() {
  221. d.s.DisallowUnknownFields = true
  222. }
  223. func (d *Decoder) InputOffset() int64 {
  224. return d.s.TotalOffset()
  225. }
  226. // UseNumber causes the Decoder to unmarshal a number into an interface{} as a
  227. // Number instead of as a float64.
  228. func (d *Decoder) UseNumber() {
  229. d.s.UseNumber = true
  230. }