Debian Codegraph @

internal/decoder/unmarshal_json.go

raw
1 package decoder
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "reflect"
8 "time"
9 "unsafe"
10
11 "github.com/goccy/go-json/internal/runtime"
12 )
13
14 type unmarshalJSONDecoder struct {
15 typ reflect.Type
16 structName string
17 fieldName string
18 // retainsNothing is set for a type whose UnmarshalJSON keeps nothing of the bytes it is given, which are then
19 // the ones of the buffer: a type of the standard library ( see runtime.IsStdMarshalerType ), a type which has
20 // the UnmarshalJSON of one only by embedding it, and json.RawMessage, which copies them. The bytes of any other type are copied: its UnmarshalJSON may keep them,
21 // and the buffer is written again by the next call.
22 retainsNothing bool
23 }
24
25 // timePtrType is the type of *time.Time, which is decoded from a string only. The decoder is made for the pointer
26 // type, whose method set has UnmarshalJSON.
27 var timePtrType = reflect.TypeOf(&time.Time{})
28
29 // rawMessagePtrType is the type of *json.RawMessage, whose UnmarshalJSON copies the bytes it is given: a copy of
30 // them made here would be copied again.
31 var rawMessagePtrType = reflect.TypeOf(&json.RawMessage{})
32
33 // timeType is the type of time.Time.
34 var timeType = timePtrType.Elem()
35
36 func newUnmarshalJSONDecoder(typ reflect.Type, structName, fieldName string) *unmarshalJSONDecoder {
37 return &unmarshalJSONDecoder{
38 typ: typ,
39 structName: structName,
40 fieldName: fieldName,
41 retainsNothing: typ == rawMessagePtrType || runtime.IsStdMarshalerType(typ) || promotesStdUnmarshalJSON(typ),
42 }
43 }
44
45 // promotesStdUnmarshalJSON is whether the UnmarshalJSON of the type is the one of an embedded type of the standard
46 // library, which the type doesn't declare itself ( see runtime.PromotedStdMethod ).
47 func promotesStdUnmarshalJSON(typ reflect.Type) bool {
48 _, _, _, ok := runtime.PromotedStdMethod(typ, runtime.UnmarshalJSONMethod)
49 return ok
50 }
51
52 func (d *unmarshalJSONDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
53 buf := ctx.Buf
54 cursor = skipWhiteSpace(buf, cursor)
55 if timeKindTypeErrors && d.typ == timePtrType {
56 if c := buf[cursor]; c != '"' && c != 'n' {
57 // a time.Time is decoded from a string only: any other value is a type error
58 return ctx.skipTypeError(cursor, depth, timeType)
59 }
60 }
61 start := cursor
62 end, err := skipValue(buf, cursor, depth)
63 if err != nil {
64 return 0, err
65 }
66 dst := buf[start:end:end]
67 if !d.retainsNothing {
68 // a copy of the exact length: append would round its capacity up, which costs more
69 copied := make([]byte, len(dst))
70 copy(copied, dst)
71 dst = copied
72 }
73
74 v := *(*any)(unsafe.Pointer(&emptyInterface{
75 typ: runtime.TypePtr(d.typ),
76 ptr: p,
77 }))
78 // The method is chosen by what the type implements, not by the entry point:
79 // json.Unmarshal may meet a type with the context method and UnmarshalContext one without it.
80 switch v := v.(type) {
81 case unmarshalerContext:
82 c := ctx.Option.Context
83 if (ctx.Option.Flags&ContextOption) == 0 || c == nil {
84 c = context.Background()
85 }
86 if err := v.UnmarshalJSON(c, dst); err != nil {
87 return ctx.methodError(cursor, end, err, d.structName, d.fieldName)
88 }
89 case json.Unmarshaler:
90 if err := v.UnmarshalJSON(dst); err != nil {
91 return ctx.methodError(cursor, end, err, d.structName, d.fieldName)
92 }
93 }
94 return end, nil
95 }
96
97 func (d *unmarshalJSONDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) {
98 return nil, 0, fmt.Errorf("json: unmarshal json decoder does not support decode path")
99 }