decode.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. func unmarshalNoEscape(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error {
  75. src := make([]byte, len(data)+1) // append nul byte to the end
  76. copy(src, data)
  77. header := (*emptyInterface)(unsafe.Pointer(&v))
  78. if err := validateType(header.typ, uintptr(header.ptr)); err != nil {
  79. return err
  80. }
  81. dec, err := decoder.CompileToGetDecoder(header.typ)
  82. if err != nil {
  83. return err
  84. }
  85. ctx := decoder.TakeRuntimeContext()
  86. ctx.Buf = src
  87. ctx.Option.Flags = 0
  88. for _, optFunc := range optFuncs {
  89. optFunc(ctx.Option)
  90. }
  91. cursor, err := dec.Decode(ctx, 0, 0, noescape(header.ptr))
  92. if err != nil {
  93. decoder.ReleaseRuntimeContext(ctx)
  94. return err
  95. }
  96. decoder.ReleaseRuntimeContext(ctx)
  97. return validateEndBuf(src, cursor)
  98. }
  99. func validateEndBuf(src []byte, cursor int64) error {
  100. for {
  101. switch src[cursor] {
  102. case ' ', '\t', '\n', '\r':
  103. cursor++
  104. continue
  105. case nul:
  106. return nil
  107. }
  108. return errors.ErrSyntax(
  109. fmt.Sprintf("invalid character '%c' after top-level value", src[cursor]),
  110. cursor+1,
  111. )
  112. }
  113. }
  114. //nolint:staticcheck
  115. //go:nosplit
  116. func noescape(p unsafe.Pointer) unsafe.Pointer {
  117. x := uintptr(p)
  118. return unsafe.Pointer(x ^ 0)
  119. }
  120. func validateType(typ *runtime.Type, p uintptr) error {
  121. if typ == nil || typ.Kind() != reflect.Ptr || p == 0 {
  122. return &InvalidUnmarshalError{Type: runtime.RType2Type(typ)}
  123. }
  124. return nil
  125. }
  126. // NewDecoder returns a new decoder that reads from r.
  127. //
  128. // The decoder introduces its own buffering and may
  129. // read data from r beyond the JSON values requested.
  130. func NewDecoder(r io.Reader) *Decoder {
  131. s := decoder.NewStream(r)
  132. return &Decoder{
  133. s: s,
  134. }
  135. }
  136. // Buffered returns a reader of the data remaining in the Decoder's
  137. // buffer. The reader is valid until the next call to Decode.
  138. func (d *Decoder) Buffered() io.Reader {
  139. return d.s.Buffered()
  140. }
  141. // Decode reads the next JSON-encoded value from its
  142. // input and stores it in the value pointed to by v.
  143. //
  144. // See the documentation for Unmarshal for details about
  145. // the conversion of JSON into a Go value.
  146. func (d *Decoder) Decode(v interface{}) error {
  147. return d.DecodeWithOption(v)
  148. }
  149. // DecodeContext reads the next JSON-encoded value from its
  150. // input and stores it in the value pointed to by v with context.Context.
  151. func (d *Decoder) DecodeContext(ctx context.Context, v interface{}) error {
  152. d.s.Option.Flags |= decoder.ContextOption
  153. d.s.Option.Context = ctx
  154. return d.DecodeWithOption(v)
  155. }
  156. func (d *Decoder) DecodeWithOption(v interface{}, optFuncs ...DecodeOptionFunc) error {
  157. header := (*emptyInterface)(unsafe.Pointer(&v))
  158. typ := header.typ
  159. ptr := uintptr(header.ptr)
  160. typeptr := uintptr(unsafe.Pointer(typ))
  161. // noescape trick for header.typ ( reflect.*rtype )
  162. copiedType := *(**runtime.Type)(unsafe.Pointer(&typeptr))
  163. if err := validateType(copiedType, ptr); err != nil {
  164. return err
  165. }
  166. dec, err := decoder.CompileToGetDecoder(typ)
  167. if err != nil {
  168. return err
  169. }
  170. if err := d.s.PrepareForDecode(); err != nil {
  171. return err
  172. }
  173. s := d.s
  174. for _, optFunc := range optFuncs {
  175. optFunc(s.Option)
  176. }
  177. if err := dec.DecodeStream(s, 0, header.ptr); err != nil {
  178. return err
  179. }
  180. s.Reset()
  181. return nil
  182. }
  183. func (d *Decoder) More() bool {
  184. return d.s.More()
  185. }
  186. func (d *Decoder) Token() (Token, error) {
  187. return d.s.Token()
  188. }
  189. // DisallowUnknownFields causes the Decoder to return an error when the destination
  190. // is a struct and the input contains object keys which do not match any
  191. // non-ignored, exported fields in the destination.
  192. func (d *Decoder) DisallowUnknownFields() {
  193. d.s.DisallowUnknownFields = true
  194. }
  195. func (d *Decoder) InputOffset() int64 {
  196. return d.s.TotalOffset()
  197. }
  198. // UseNumber causes the Decoder to unmarshal a number into an interface{} as a
  199. // Number instead of as a float64.
  200. func (d *Decoder) UseNumber() {
  201. d.s.UseNumber = true
  202. }