| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"sync" |
| 6 |
"sync/atomic" |
| 7 |
"unsafe" |
| 8 |
|
| 9 |
"github.com/goccy/go-json/internal/errors" |
| 10 |
"github.com/goccy/go-json/internal/runtime" |
| 11 |
) |
| 12 |
|
| 13 |
type mapDecoder struct { |
| 14 |
mapType reflect.Type |
| 15 |
keyType reflect.Type |
| 16 |
valueType reflect.Type |
| 17 |
// mapTypePtr, keyPtrType and valuePtrType are the type descriptors of the map, of *K and of *V. |
| 18 |
mapTypePtr unsafe.Pointer |
| 19 |
keyPtrType unsafe.Pointer |
| 20 |
valuePtrType unsafe.Pointer |
| 21 |
// isStringAnyMap is whether the map is a map[string]interface{}, which is decoded without reflect. |
| 22 |
isStringAnyMap bool |
| 23 |
keyDecoder Decoder |
| 24 |
valueDecoder Decoder |
| 25 |
// keyUnsupported is set for a map whose keys encoding/json of the Go version doesn't decode ( see |
| 26 |
// mapKeySupported ): an object is a type error. |
| 27 |
keyUnsupported bool |
| 28 |
structName string |
| 29 |
fieldName string |
| 30 |
// temps holds the zero values which the key and the value of an entry are decoded into ( see mapTemps ). |
| 31 |
temps sync.Pool |
| 32 |
// lastLen is the number of the entries of the object which the decoder decoded last, up to maxMapSizeHint, |
| 33 |
// which a new map is made for: the objects of a kind often have a number of entries. It is only a hint, for |
| 34 |
// any goroutine. |
| 35 |
lastLen atomic.Int32 |
| 36 |
} |
| 37 |
|
| 38 |
// maxMapSizeHint is the largest number of the entries which a new map is made for ( see lastLen ). |
| 39 |
const maxMapSizeHint = 64 |
| 40 |
|
| 41 |
// mapTemps are the zero values of the key and of the value of a map, into which the key and the value of every |
| 42 |
// entry are decoded, which reflect.Value.SetMapIndex copies into the map, and which are zeroed after it: a |
| 43 |
// decoder keeps them in a pool, so that a map is decoded without allocating them. |
| 44 |
type mapTemps struct { |
| 45 |
k, v unsafe.Pointer |
| 46 |
kv, vv reflect.Value |
| 47 |
} |
| 48 |
|
| 49 |
func newMapDecoder(mapType reflect.Type, keyType reflect.Type, keyDec Decoder, valueType reflect.Type, valueDec Decoder, structName, fieldName string) *mapDecoder { |
| 50 |
_, isIfaceValue := valueDec.(*interfaceDecoder) |
| 51 |
d := &mapDecoder{ |
| 52 |
mapType: mapType, |
| 53 |
keyDecoder: keyDec, |
| 54 |
keyType: keyType, |
| 55 |
valueType: valueType, |
| 56 |
mapTypePtr: runtime.TypePtr(mapType), |
| 57 |
keyPtrType: ptrTypeOf(keyType), |
| 58 |
valuePtrType: ptrTypeOf(valueType), |
| 59 |
isStringAnyMap: mapType == interfaceMapType && isIfaceValue, |
| 60 |
valueDecoder: valueDec, |
| 61 |
keyUnsupported: !mapKeySupported(keyType, keyDec), |
| 62 |
structName: structName, |
| 63 |
fieldName: fieldName, |
| 64 |
} |
| 65 |
d.temps.New = func() any { |
| 66 |
t := &mapTemps{k: newValue(keyType), v: newValue(valueType)} |
| 67 |
t.kv, t.vv = valueAt(d.keyPtrType, t.k), valueAt(d.valuePtrType, t.v) |
| 68 |
return t |
| 69 |
} |
| 70 |
return d |
| 71 |
} |
| 72 |
|
| 73 |
// mapValue returns the reflect.Value of the map m. |
| 74 |
func (d *mapDecoder) mapValue(m unsafe.Pointer) reflect.Value { |
| 75 |
// A map is a pointer in an interface value. |
| 76 |
return reflect.ValueOf(*(*any)(unsafe.Pointer(&emptyInterface{typ: d.mapTypePtr, ptr: m}))) |
| 77 |
} |
| 78 |
|
| 79 |
// decodeEntries decodes the entries of the object from cursor into the map, whose keys and values are decoded into |
| 80 |
// k and v, as Decode does, but the entries whose keys are of another type ( see errMapKeyType ), which are dropped. |
| 81 |
// If afterKey is set, cursor is after the key of such an entry, whose value is decoded first. It is a function of |
| 82 |
// its own, so that Decode keeps the size it had: such a key is rare. |
| 83 |
// |
| 84 |
//go:noinline |
| 85 |
func (d *mapDecoder) decodeEntries(ctx *RuntimeContext, cursor, depth int64, p, mapValue, k, v unsafe.Pointer, afterKey bool) (int64, error) { |
| 86 |
buf := ctx.Buf |
| 87 |
mv := d.mapValue(mapValue) |
| 88 |
kv := valueAt(d.keyPtrType, k) |
| 89 |
vv := valueAt(d.valuePtrType, v) |
| 90 |
for { |
| 91 |
drop := afterKey |
| 92 |
if !afterKey { |
| 93 |
keyCursor, err := d.keyDecoder.Decode(ctx, cursor, depth, k) |
| 94 |
if err != nil && err != errMapKeyType { |
| 95 |
return 0, err |
| 96 |
} |
| 97 |
drop = err != nil |
| 98 |
cursor = keyCursor |
| 99 |
} |
| 100 |
afterKey = false |
| 101 |
cursor = skipWhiteSpace(buf, cursor) |
| 102 |
if buf[cursor] != ':' { |
| 103 |
return 0, errors.ErrExpected("colon after object key", cursor) |
| 104 |
} |
| 105 |
valueCursor, err := d.valueDecoder.Decode(ctx, cursor+1, depth, v) |
| 106 |
if err != nil { |
| 107 |
return 0, err |
| 108 |
} |
| 109 |
if !drop { |
| 110 |
mv.SetMapIndex(kv, vv) |
| 111 |
} |
| 112 |
kv.SetZero() |
| 113 |
vv.SetZero() |
| 114 |
cursor = skipWhiteSpace(buf, valueCursor) |
| 115 |
if buf[cursor] == '}' { |
| 116 |
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue |
| 117 |
return cursor + 1, nil |
| 118 |
} |
| 119 |
if buf[cursor] != ',' { |
| 120 |
return 0, errors.ErrExpected("comma after object value", cursor) |
| 121 |
} |
| 122 |
cursor++ |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// decodeOther skips the value at cursor, which is not an object: a value of another kind is a type error, and |
| 127 |
// anything else a syntax error. It is a function of its own, so that Decode keeps the size it had. |
| 128 |
// |
| 129 |
//go:noinline |
| 130 |
func (d *mapDecoder) decodeOther(ctx *RuntimeContext, cursor, depth int64) (int64, error) { |
| 131 |
if isOtherValue(ctx.Buf[cursor], objectValue) { |
| 132 |
return ctx.skipTypeError(cursor, depth, d.mapType) |
| 133 |
} |
| 134 |
return 0, errors.ErrExpected("{ character for map value", cursor) |
| 135 |
} |
| 136 |
|
| 137 |
func (d *mapDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { |
| 138 |
buf := ctx.Buf |
| 139 |
depth++ |
| 140 |
if depth > maxDecodeNestingDepth { |
| 141 |
return 0, errors.ErrExceededMaxDepth(buf[cursor], cursor) |
| 142 |
} |
| 143 |
|
| 144 |
cursor = skipWhiteSpace(buf, cursor) |
| 145 |
buflen := int64(len(buf)) |
| 146 |
if buflen < 2 { |
| 147 |
return 0, errors.ErrExpected("{} for map", cursor) |
| 148 |
} |
| 149 |
switch buf[cursor] { |
| 150 |
case 'n': |
| 151 |
if err := validateNull(buf, cursor); err != nil { |
| 152 |
return 0, err |
| 153 |
} |
| 154 |
cursor += 4 |
| 155 |
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = nil |
| 156 |
return cursor, nil |
| 157 |
case '{': |
| 158 |
default: |
| 159 |
return d.decodeOther(ctx, cursor, depth-1) |
| 160 |
} |
| 161 |
if d.keyUnsupported { |
| 162 |
return ctx.unsupportedMapKeys(d, cursor, depth-1, p) |
| 163 |
} |
| 164 |
if d.isStringAnyMap { |
| 165 |
m := *(*map[string]any)(p) |
| 166 |
if m == nil { |
| 167 |
m, c, err := decodeNewStringAnyMap(ctx, d.valueDecoder.(*interfaceDecoder), cursor, depth) |
| 168 |
if err != nil { |
| 169 |
return 0, err |
| 170 |
} |
| 171 |
*(*map[string]any)(p) = m |
| 172 |
return c, nil |
| 173 |
} |
| 174 |
c, err := decodeStringAnyMap(ctx, d.valueDecoder.(*interfaceDecoder), m, cursor, depth) |
| 175 |
if err != nil { |
| 176 |
return 0, err |
| 177 |
} |
| 178 |
return c, nil |
| 179 |
} |
| 180 |
cursor++ |
| 181 |
cursor = skipWhiteSpace(buf, cursor) |
| 182 |
mapValue := *(*unsafe.Pointer)(p) |
| 183 |
if mapValue == nil { |
| 184 |
// made for the number of the entries of the last object, which an object of the kind is likely to have |
| 185 |
mapValue = reflect.MakeMapWithSize(d.mapType, int(d.lastLen.Load())).UnsafePointer() |
| 186 |
} |
| 187 |
if buf[cursor] == '}' { |
| 188 |
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue |
| 189 |
cursor++ |
| 190 |
return cursor, nil |
| 191 |
} |
| 192 |
// The key and the value of every entry are decoded into the same zero values ( see mapTemps ), which go back |
| 193 |
// to the pool when the object ends: after an error, they may have values, and are left to the collector. |
| 194 |
t := d.temps.Get().(*mapTemps) |
| 195 |
k, v, kv, vv := t.k, t.v, t.kv, t.vv |
| 196 |
mv := d.mapValue(mapValue) |
| 197 |
n := int32(0) |
| 198 |
for { |
| 199 |
keyCursor, err := d.keyDecoder.Decode(ctx, cursor, depth, k) |
| 200 |
if err != nil { |
| 201 |
if err == errMapKeyType { |
| 202 |
// a key of another type: the rest of the object is decoded by decodeEntries |
| 203 |
return d.decodeEntries(ctx, keyCursor, depth, p, mapValue, k, v, true) |
| 204 |
} |
| 205 |
return 0, err |
| 206 |
} |
| 207 |
cursor = skipWhiteSpace(buf, keyCursor) |
| 208 |
if buf[cursor] != ':' { |
| 209 |
return 0, errors.ErrExpected("colon after object key", cursor) |
| 210 |
} |
| 211 |
cursor++ |
| 212 |
valueCursor, err := d.valueDecoder.Decode(ctx, cursor, depth, v) |
| 213 |
if err != nil { |
| 214 |
return 0, err |
| 215 |
} |
| 216 |
mv.SetMapIndex(kv, vv) |
| 217 |
kv.SetZero() |
| 218 |
vv.SetZero() |
| 219 |
n++ |
| 220 |
cursor = skipWhiteSpace(buf, valueCursor) |
| 221 |
if buf[cursor] == '}' { |
| 222 |
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue |
| 223 |
d.temps.Put(t) |
| 224 |
d.lastLen.Store(min(n, maxMapSizeHint)) |
| 225 |
cursor++ |
| 226 |
return cursor, nil |
| 227 |
} |
| 228 |
if buf[cursor] != ',' { |
| 229 |
return 0, errors.ErrExpected("comma after object value", cursor) |
| 230 |
} |
| 231 |
cursor++ |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
func (d *mapDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) { |
| 236 |
buf := ctx.Buf |
| 237 |
depth++ |
| 238 |
if depth > maxDecodeNestingDepth { |
| 239 |
return nil, 0, errors.ErrExceededMaxDepth(buf[cursor], cursor) |
| 240 |
} |
| 241 |
|
| 242 |
cursor = skipWhiteSpace(buf, cursor) |
| 243 |
buflen := int64(len(buf)) |
| 244 |
if buflen < 2 { |
| 245 |
return nil, 0, errors.ErrExpected("{} for map", cursor) |
| 246 |
} |
| 247 |
switch buf[cursor] { |
| 248 |
case 'n': |
| 249 |
if err := validateNull(buf, cursor); err != nil { |
| 250 |
return nil, 0, err |
| 251 |
} |
| 252 |
cursor += 4 |
| 253 |
return [][]byte{nullbytes}, cursor, nil |
| 254 |
case '{': |
| 255 |
default: |
| 256 |
return nil, 0, errors.ErrExpected("{ character for map value", cursor) |
| 257 |
} |
| 258 |
cursor++ |
| 259 |
cursor = skipWhiteSpace(buf, cursor) |
| 260 |
if buf[cursor] == '}' { |
| 261 |
cursor++ |
| 262 |
return nil, cursor, nil |
| 263 |
} |
| 264 |
keyDecoder, ok := d.keyDecoder.(*stringDecoder) |
| 265 |
if !ok { |
| 266 |
return nil, 0, &errors.UnmarshalTypeError{ |
| 267 |
Value: "string", |
| 268 |
Type: reflect.TypeOf(""), |
| 269 |
Offset: cursor, |
| 270 |
Struct: d.structName, |
| 271 |
Field: d.fieldName, |
| 272 |
} |
| 273 |
} |
| 274 |
ret := [][]byte{} |
| 275 |
for { |
| 276 |
key, keyCursor, err := keyDecoder.decodeByte(buf, cursor) |
| 277 |
if err != nil { |
| 278 |
return nil, 0, err |
| 279 |
} |
| 280 |
cursor = skipWhiteSpace(buf, keyCursor) |
| 281 |
if buf[cursor] != ':' { |
| 282 |
return nil, 0, errors.ErrExpected("colon after object key", cursor) |
| 283 |
} |
| 284 |
cursor++ |
| 285 |
child, found, err := ctx.Option.Path.Field(string(key)) |
| 286 |
if err != nil { |
| 287 |
return nil, 0, err |
| 288 |
} |
| 289 |
if found { |
| 290 |
if child != nil { |
| 291 |
oldPath := ctx.Option.Path.node |
| 292 |
ctx.Option.Path.node = child |
| 293 |
paths, c, err := d.valueDecoder.DecodePath(ctx, cursor, depth) |
| 294 |
if err != nil { |
| 295 |
return nil, 0, err |
| 296 |
} |
| 297 |
ctx.Option.Path.node = oldPath |
| 298 |
ret = append(ret, paths...) |
| 299 |
cursor = c |
| 300 |
} else { |
| 301 |
start := cursor |
| 302 |
end, err := skipValue(buf, cursor, depth) |
| 303 |
if err != nil { |
| 304 |
return nil, 0, err |
| 305 |
} |
| 306 |
ret = append(ret, buf[start:end]) |
| 307 |
cursor = end |
| 308 |
} |
| 309 |
} else { |
| 310 |
c, err := skipValue(buf, cursor, depth) |
| 311 |
if err != nil { |
| 312 |
return nil, 0, err |
| 313 |
} |
| 314 |
cursor = c |
| 315 |
} |
| 316 |
cursor = skipWhiteSpace(buf, cursor) |
| 317 |
if buf[cursor] == '}' { |
| 318 |
cursor++ |
| 319 |
return ret, cursor, nil |
| 320 |
} |
| 321 |
if buf[cursor] != ',' { |
| 322 |
return nil, 0, errors.ErrExpected("comma after object value", cursor) |
| 323 |
} |
| 324 |
cursor++ |
| 325 |
} |
| 326 |
} |