| 1 |
package json |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"io" |
| 6 |
"unsafe" |
| 7 |
|
| 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 |
|
| 13 |
type Decoder struct { |
| 14 |
s *decoder.Stream |
| 15 |
} |
| 16 |
|
| 17 |
type emptyInterface struct { |
| 18 |
typ unsafe.Pointer |
| 19 |
ptr unsafe.Pointer |
| 20 |
} |
| 21 |
|
| 22 |
func unmarshal(data []byte, v any, optFuncs ...DecodeOptionFunc) error { |
| 23 |
header := (*emptyInterface)(unsafe.Pointer(&v)) |
| 24 |
|
| 25 |
if err := validateType(header.typ, uintptr(header.ptr)); err != nil { |
| 26 |
return err |
| 27 |
} |
| 28 |
ctx := decoder.TakeRuntimeContext() |
| 29 |
dec, err := ctx.DecoderOf(header.typ) |
| 30 |
if err != nil { |
| 31 |
decoder.ReleaseRuntimeContext(ctx) |
| 32 |
return err |
| 33 |
} |
| 34 |
src := ctx.SetInput(data) |
| 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 |
// src is the buffer of the context: it is validated before the context is released |
| 42 |
err = validateEndBuf(src, cursor) |
| 43 |
} |
| 44 |
if err != nil || ctx.HasTypeError() { |
| 45 |
return endWithError(ctx, header.typ, err) |
| 46 |
} |
| 47 |
decoder.ReleaseRuntimeContext(ctx) |
| 48 |
return nil |
| 49 |
} |
| 50 |
|
| 51 |
func unmarshalContext(ctx context.Context, data []byte, v any, optFuncs ...DecodeOptionFunc) error { |
| 52 |
header := (*emptyInterface)(unsafe.Pointer(&v)) |
| 53 |
|
| 54 |
if err := validateType(header.typ, uintptr(header.ptr)); err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
rctx := decoder.TakeRuntimeContext() |
| 58 |
dec, err := rctx.DecoderOf(header.typ) |
| 59 |
if err != nil { |
| 60 |
decoder.ReleaseRuntimeContext(rctx) |
| 61 |
return err |
| 62 |
} |
| 63 |
src := rctx.SetInput(data) |
| 64 |
rctx.Option.Flags = 0 |
| 65 |
rctx.Option.Flags |= decoder.ContextOption |
| 66 |
rctx.Option.Context = ctx |
| 67 |
for _, optFunc := range optFuncs { |
| 68 |
optFunc(rctx.Option) |
| 69 |
} |
| 70 |
cursor, err := dec.Decode(rctx, 0, 0, header.ptr) |
| 71 |
if err == nil { |
| 72 |
// src is the buffer of the context: it is validated before the context is released |
| 73 |
err = validateEndBuf(src, cursor) |
| 74 |
} |
| 75 |
if err != nil || rctx.HasTypeError() { |
| 76 |
return endWithError(rctx, header.typ, err) |
| 77 |
} |
| 78 |
decoder.ReleaseRuntimeContext(rctx) |
| 79 |
return nil |
| 80 |
} |
| 81 |
|
| 82 |
var ( |
| 83 |
pathDecoder = decoder.NewPathDecoder() |
| 84 |
) |
| 85 |
|
| 86 |
func extractFromPath(path *Path, data []byte, optFuncs ...DecodeOptionFunc) ([][]byte, error) { |
| 87 |
if path.path.RootSelectorOnly { |
| 88 |
return [][]byte{data}, nil |
| 89 |
} |
| 90 |
src := decoder.NewInput(data) |
| 91 |
|
| 92 |
ctx := decoder.TakeRuntimeContext() |
| 93 |
ctx.Buf = src |
| 94 |
ctx.Option.Flags = 0 |
| 95 |
ctx.Option.Flags |= decoder.PathOption |
| 96 |
ctx.Option.Path = path.path |
| 97 |
for _, optFunc := range optFuncs { |
| 98 |
optFunc(ctx.Option) |
| 99 |
} |
| 100 |
paths, cursor, err := pathDecoder.DecodePath(ctx, 0, 0) |
| 101 |
if err != nil { |
| 102 |
decoder.ReleaseRuntimeContext(ctx) |
| 103 |
return nil, err |
| 104 |
} |
| 105 |
decoder.ReleaseRuntimeContext(ctx) |
| 106 |
if err := validateEndBuf(src, cursor); err != nil { |
| 107 |
return nil, err |
| 108 |
} |
| 109 |
return paths, nil |
| 110 |
} |
| 111 |
|
| 112 |
// endWithError releases the context of a decoding into a value of the pointer type typ, which failed with err or |
| 113 |
// else has a type error, and returns the error: the type error is returned only when the whole input is valid, as |
| 114 |
// encoding/json does. It takes the decoder of typ again, so that the functions which decode keep nothing more |
| 115 |
// across their call of it: an error is rare. |
| 116 |
// |
| 117 |
//go:noinline |
| 118 |
func endWithError(ctx *decoder.RuntimeContext, typ unsafe.Pointer, err error) error { |
| 119 |
err = inputSyntaxError(ctx, err) |
| 120 |
if err == nil { |
| 121 |
var dec decoder.Decoder |
| 122 |
if dec, err = ctx.DecoderOf(typ); err == nil { |
| 123 |
err = ctx.TypeError(dec, runtime.TypeOfPtr(typ), 0, 0) |
| 124 |
} |
| 125 |
} |
| 126 |
ctx.DiscardTypeError() |
| 127 |
decoder.ReleaseRuntimeContext(ctx) |
| 128 |
return err |
| 129 |
} |
| 130 |
|
| 131 |
// inputSyntaxError returns err, the error of the decoding of the input of ctx, or, for a syntax error, the first |
| 132 |
// syntax error of the input as encoding/json reports it: it checks the whole input before it decodes it. |
| 133 |
func inputSyntaxError(ctx *decoder.RuntimeContext, err error) error { |
| 134 |
if _, ok := err.(*errors.SyntaxError); ok { |
| 135 |
if serr := ctx.InputSyntaxError(); serr != nil { |
| 136 |
return serr |
| 137 |
} |
| 138 |
} |
| 139 |
return err |
| 140 |
} |
| 141 |
|
| 142 |
// validateEndBuf returns the syntax error of what follows the value, which ends at cursor, in src, the input |
| 143 |
// followed by the nul byte: only white spaces may follow it. The value is most often at the end of the input, which |
| 144 |
// is checked without a call. |
| 145 |
func validateEndBuf(src []byte, cursor int64) error { |
| 146 |
if cursor == int64(len(src))-1 { |
| 147 |
return nil |
| 148 |
} |
| 149 |
return decoder.ValidateEnd(src, cursor) |
| 150 |
} |
| 151 |
|
| 152 |
// validateType validates that the value is not nil. |
| 153 |
// Whether the type is a pointer is validated when its decoder is compiled, once per type. |
| 154 |
func validateType(typ unsafe.Pointer, p uintptr) error { |
| 155 |
if typ == nil || p == 0 { |
| 156 |
return &InvalidUnmarshalError{Type: runtime.TypeOfPtr(typ)} |
| 157 |
} |
| 158 |
return nil |
| 159 |
} |
| 160 |
|
| 161 |
// NewDecoder returns a new decoder that reads from r. |
| 162 |
// |
| 163 |
// The decoder introduces its own buffering and may |
| 164 |
// read data from r beyond the JSON values requested. |
| 165 |
func NewDecoder(r io.Reader) *Decoder { |
| 166 |
s := decoder.NewStream(r) |
| 167 |
return &Decoder{ |
| 168 |
s: s, |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
// Buffered returns a reader of the data remaining in the Decoder's |
| 173 |
// buffer. The reader is valid until the next call to Decode. |
| 174 |
func (d *Decoder) Buffered() io.Reader { |
| 175 |
return d.s.Buffered() |
| 176 |
} |
| 177 |
|
| 178 |
// Decode reads the next JSON-encoded value from its |
| 179 |
// input and stores it in the value pointed to by v. |
| 180 |
// |
| 181 |
// See the documentation for Unmarshal for details about |
| 182 |
// the conversion of JSON into a Go value. |
| 183 |
func (d *Decoder) Decode(v any) error { |
| 184 |
return d.DecodeWithOption(v) |
| 185 |
} |
| 186 |
|
| 187 |
// DecodeContext reads the next JSON-encoded value from its |
| 188 |
// input and stores it in the value pointed to by v with context.Context. |
| 189 |
func (d *Decoder) DecodeContext(ctx context.Context, v any) error { |
| 190 |
d.s.Option.Flags |= decoder.ContextOption |
| 191 |
d.s.Option.Context = ctx |
| 192 |
return d.DecodeWithOption(v) |
| 193 |
} |
| 194 |
|
| 195 |
func (d *Decoder) DecodeWithOption(v any, optFuncs ...DecodeOptionFunc) error { |
| 196 |
header := (*emptyInterface)(unsafe.Pointer(&v)) |
| 197 |
if err := validateType(header.typ, uintptr(header.ptr)); err != nil { |
| 198 |
return err |
| 199 |
} |
| 200 |
|
| 201 |
dec, err := d.s.DecoderOf(header.typ) |
| 202 |
if err != nil { |
| 203 |
return err |
| 204 |
} |
| 205 |
s := d.s |
| 206 |
for _, optFunc := range optFuncs { |
| 207 |
optFunc(s.Option) |
| 208 |
} |
| 209 |
return s.Decode(dec, header.typ, header.ptr) |
| 210 |
} |
| 211 |
|
| 212 |
func (d *Decoder) More() bool { |
| 213 |
return d.s.More() |
| 214 |
} |
| 215 |
|
| 216 |
func (d *Decoder) Token() (Token, error) { |
| 217 |
return d.s.Token() |
| 218 |
} |
| 219 |
|
| 220 |
// DisallowUnknownFields causes the Decoder to return an error when the destination |
| 221 |
// is a struct and the input contains object keys which do not match any |
| 222 |
// non-ignored, exported fields in the destination. |
| 223 |
func (d *Decoder) DisallowUnknownFields() { |
| 224 |
d.s.Option.Flags |= decoder.DisallowUnknownFieldsOption |
| 225 |
} |
| 226 |
|
| 227 |
func (d *Decoder) InputOffset() int64 { |
| 228 |
return d.s.InputOffset() |
| 229 |
} |
| 230 |
|
| 231 |
// UseNumber causes the Decoder to unmarshal a number into an interface{} as a |
| 232 |
// Number instead of as a float64. |
| 233 |
func (d *Decoder) UseNumber() { |
| 234 |
d.s.Option.Flags |= decoder.UseNumberOption |
| 235 |
} |