Debian Codegraph @

decode_of.go

raw
1 package json
2
3 import (
4 "reflect"
5 "unsafe"
6
7 "github.com/goccy/go-json/internal/decoder"
8 "github.com/goccy/go-json/internal/runtime"
9 )
10
11 // UnmarshalOf parses the JSON-encoded data and stores the result in the value pointed to by v, as Unmarshal does.
12 //
13 // Unmarshal takes its argument as an interface value, and the decoder refers to the value by its address,
14 // so the value always escapes to the heap. UnmarshalOf takes the pointer by its type and never gives it to
15 // the decoder: it copies *v to a value in the heap which is reused, decodes into that value, and copies the
16 // result back to *v. So v may point to a variable on the stack of the caller, and a value which needs no
17 // allocation of its own ( no pointer, slice or map to fill ) is decoded without an allocation: the strings
18 // are copied into a buffer shared by the calls, or refer to data with DecodeNoCopyString.
19 //
20 // A value is copied twice, so a large value is faster with Unmarshal when it is in the heap anyway. As the
21 // result is a copy, an UnmarshalJSON or UnmarshalText method of a type in the value must not keep the address
22 // of its receiver, which is the address of the copy.
23 func UnmarshalOf[T any](data []byte, v *T, optFuncs ...DecodeOptionFunc) error {
24 ptrType := reflect.TypeOf((*T)(nil))
25 if v == nil {
26 return &InvalidUnmarshalError{Type: ptrType}
27 }
28 ctx := decoder.TakeRuntimeContext()
29 dec, err := ctx.DecoderOf(runtime.TypePtr(ptrType))
30 if err != nil {
31 decoder.ReleaseRuntimeContext(ctx)
32 return err
33 }
34 var p unsafe.Pointer
35 if unsafe.Sizeof(*v) > maxReusedValueSize {
36 // every context of the pool would keep such a value: it is left to the GC.
37 p = reflect.New(ptrType.Elem()).UnsafePointer()
38 } else {
39 p = ctx.TakeValue(ptrType.Elem(), runtime.TypePtr(ptrType.Elem()))
40 }
41 *(*T)(p) = *v
42
43 src := ctx.SetInput(data)
44 ctx.Option.Flags = 0
45 for _, optFunc := range optFuncs {
46 optFunc(ctx.Option)
47 }
48 cursor, err := dec.Decode(ctx, 0, 0, p)
49 if err == nil {
50 // a type error is returned only when the whole input is valid, as encoding/json does
51 if err = validateEndBuf(src, cursor); err == nil && ctx.HasTypeError() {
52 err = typeErrorOf(ctx, runtime.TypePtr(reflect.TypeOf((*T)(nil))))
53 }
54 }
55 if err != nil {
56 err = inputSyntaxError(ctx, err)
57 }
58 ctx.DiscardTypeError()
59 // What was decoded before an error is stored too, as Unmarshal does.
60 *v = *(*T)(p)
61 // the pool must not keep what the value refers to alive.
62 var zero T
63 *(*T)(p) = zero
64 decoder.ReleaseRuntimeContext(ctx)
65 return err
66 }
67
68 // typeErrorOf returns the type error of the decoding into a value of the pointer type typ. It takes the decoder of
69 // typ again, so that UnmarshalOf keeps nothing more across its call of the decoder: a type error is rare.
70 //
71 //go:noinline
72 func typeErrorOf(ctx *decoder.RuntimeContext, typ unsafe.Pointer) error {
73 dec, err := ctx.DecoderOf(typ)
74 if err != nil {
75 return err
76 }
77 return ctx.TypeError(dec, runtime.TypeOfPtr(typ), 0, 0)
78 }