| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"encoding" |
| 7 |
stdjson "encoding/json" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"image" |
| 11 |
"math" |
| 12 |
"math/big" |
| 13 |
"net" |
| 14 |
"reflect" |
| 15 |
"strconv" |
| 16 |
"strings" |
| 17 |
"sync" |
| 18 |
"testing" |
| 19 |
"time" |
| 20 |
"unsafe" |
| 21 |
|
| 22 |
"github.com/goccy/go-json" |
| 23 |
) |
| 24 |
|
| 25 |
func Test_Decoder(t *testing.T) { |
| 26 |
t.Run("int", func(t *testing.T) { |
| 27 |
var v int |
| 28 |
assertErr(t, json.Unmarshal([]byte(`-1`), &v)) |
| 29 |
assertEq(t, "int", int(-1), v) |
| 30 |
}) |
| 31 |
t.Run("int8", func(t *testing.T) { |
| 32 |
var v int8 |
| 33 |
assertErr(t, json.Unmarshal([]byte(`-2`), &v)) |
| 34 |
assertEq(t, "int8", int8(-2), v) |
| 35 |
}) |
| 36 |
t.Run("int16", func(t *testing.T) { |
| 37 |
var v int16 |
| 38 |
assertErr(t, json.Unmarshal([]byte(`-3`), &v)) |
| 39 |
assertEq(t, "int16", int16(-3), v) |
| 40 |
}) |
| 41 |
t.Run("int32", func(t *testing.T) { |
| 42 |
var v int32 |
| 43 |
assertErr(t, json.Unmarshal([]byte(`-4`), &v)) |
| 44 |
assertEq(t, "int32", int32(-4), v) |
| 45 |
}) |
| 46 |
t.Run("int64", func(t *testing.T) { |
| 47 |
var v int64 |
| 48 |
assertErr(t, json.Unmarshal([]byte(`-5`), &v)) |
| 49 |
assertEq(t, "int64", int64(-5), v) |
| 50 |
}) |
| 51 |
t.Run("uint", func(t *testing.T) { |
| 52 |
var v uint |
| 53 |
assertErr(t, json.Unmarshal([]byte(`1`), &v)) |
| 54 |
assertEq(t, "uint", uint(1), v) |
| 55 |
}) |
| 56 |
t.Run("uint8", func(t *testing.T) { |
| 57 |
var v uint8 |
| 58 |
assertErr(t, json.Unmarshal([]byte(`2`), &v)) |
| 59 |
assertEq(t, "uint8", uint8(2), v) |
| 60 |
}) |
| 61 |
t.Run("uint16", func(t *testing.T) { |
| 62 |
var v uint16 |
| 63 |
assertErr(t, json.Unmarshal([]byte(`3`), &v)) |
| 64 |
assertEq(t, "uint16", uint16(3), v) |
| 65 |
}) |
| 66 |
t.Run("uint32", func(t *testing.T) { |
| 67 |
var v uint32 |
| 68 |
assertErr(t, json.Unmarshal([]byte(`4`), &v)) |
| 69 |
assertEq(t, "uint32", uint32(4), v) |
| 70 |
}) |
| 71 |
t.Run("uint64", func(t *testing.T) { |
| 72 |
var v uint64 |
| 73 |
assertErr(t, json.Unmarshal([]byte(`5`), &v)) |
| 74 |
assertEq(t, "uint64", uint64(5), v) |
| 75 |
}) |
| 76 |
t.Run("bool", func(t *testing.T) { |
| 77 |
t.Run("true", func(t *testing.T) { |
| 78 |
var v bool |
| 79 |
assertErr(t, json.Unmarshal([]byte(`true`), &v)) |
| 80 |
assertEq(t, "bool", true, v) |
| 81 |
}) |
| 82 |
t.Run("false", func(t *testing.T) { |
| 83 |
v := true |
| 84 |
assertErr(t, json.Unmarshal([]byte(`false`), &v)) |
| 85 |
assertEq(t, "bool", false, v) |
| 86 |
}) |
| 87 |
}) |
| 88 |
t.Run("string", func(t *testing.T) { |
| 89 |
var v string |
| 90 |
assertErr(t, json.Unmarshal([]byte(`"hello"`), &v)) |
| 91 |
assertEq(t, "string", "hello", v) |
| 92 |
}) |
| 93 |
t.Run("float32", func(t *testing.T) { |
| 94 |
var v float32 |
| 95 |
assertErr(t, json.Unmarshal([]byte(`3.14`), &v)) |
| 96 |
assertEq(t, "float32", float32(3.14), v) |
| 97 |
}) |
| 98 |
t.Run("float64", func(t *testing.T) { |
| 99 |
var v float64 |
| 100 |
assertErr(t, json.Unmarshal([]byte(`3.14`), &v)) |
| 101 |
assertEq(t, "float64", float64(3.14), v) |
| 102 |
}) |
| 103 |
t.Run("slice", func(t *testing.T) { |
| 104 |
var v []int |
| 105 |
assertErr(t, json.Unmarshal([]byte(` [ 1 , 2 , 3 , 4 ] `), &v)) |
| 106 |
assertEq(t, "slice", fmt.Sprint([]int{1, 2, 3, 4}), fmt.Sprint(v)) |
| 107 |
}) |
| 108 |
t.Run("slice_reuse_data", func(t *testing.T) { |
| 109 |
v := make([]int, 0, 10) |
| 110 |
assertErr(t, json.Unmarshal([]byte(` [ 1 , 2 , 3 , 4 ] `), &v)) |
| 111 |
assertEq(t, "slice", fmt.Sprint([]int{1, 2, 3, 4}), fmt.Sprint(v)) |
| 112 |
assertEq(t, "cap", 10, cap(v)) |
| 113 |
}) |
| 114 |
t.Run("array", func(t *testing.T) { |
| 115 |
var v [4]int |
| 116 |
assertErr(t, json.Unmarshal([]byte(` [ 1 , 2 , 3 , 4 ] `), &v)) |
| 117 |
assertEq(t, "array", fmt.Sprint([4]int{1, 2, 3, 4}), fmt.Sprint(v)) |
| 118 |
}) |
| 119 |
t.Run("map", func(t *testing.T) { |
| 120 |
var v map[string]int |
| 121 |
assertErr(t, json.Unmarshal([]byte(` { "a": 1, "b": 2, "c": 3, "d": 4 } `), &v)) |
| 122 |
assertEq(t, "map.a", v["a"], 1) |
| 123 |
assertEq(t, "map.b", v["b"], 2) |
| 124 |
assertEq(t, "map.c", v["c"], 3) |
| 125 |
assertEq(t, "map.d", v["d"], 4) |
| 126 |
t.Run("nested map", func(t *testing.T) { |
| 127 |
// https://github.com/goccy/go-json/issues/8 |
| 128 |
content := ` |
| 129 |
{ |
| 130 |
"a": { |
| 131 |
"nestedA": "value of nested a" |
| 132 |
}, |
| 133 |
"b": { |
| 134 |
"nestedB": "value of nested b" |
| 135 |
}, |
| 136 |
"c": { |
| 137 |
"nestedC": "value of nested c" |
| 138 |
} |
| 139 |
}` |
| 140 |
var v map[string]any |
| 141 |
assertErr(t, json.Unmarshal([]byte(content), &v)) |
| 142 |
assertEq(t, "length", 3, len(v)) |
| 143 |
}) |
| 144 |
}) |
| 145 |
t.Run("struct", func(t *testing.T) { |
| 146 |
type T struct { |
| 147 |
AA int `json:"aa"` |
| 148 |
BB string `json:"bb"` |
| 149 |
CC bool `json:"cc"` |
| 150 |
} |
| 151 |
var v struct { |
| 152 |
A int `json:"abcd"` |
| 153 |
B string `json:"str"` |
| 154 |
C bool |
| 155 |
D *T |
| 156 |
E func() |
| 157 |
} |
| 158 |
content := []byte(` |
| 159 |
{ |
| 160 |
"abcd": 123, |
| 161 |
"str" : "hello", |
| 162 |
"c" : true, |
| 163 |
"d" : { |
| 164 |
"aa": 2, |
| 165 |
"bb": "world", |
| 166 |
"cc": true |
| 167 |
}, |
| 168 |
"e" : null |
| 169 |
}`) |
| 170 |
assertErr(t, json.Unmarshal(content, &v)) |
| 171 |
assertEq(t, "struct.A", 123, v.A) |
| 172 |
assertEq(t, "struct.B", "hello", v.B) |
| 173 |
assertEq(t, "struct.C", true, v.C) |
| 174 |
assertEq(t, "struct.D.AA", 2, v.D.AA) |
| 175 |
assertEq(t, "struct.D.BB", "world", v.D.BB) |
| 176 |
assertEq(t, "struct.D.CC", true, v.D.CC) |
| 177 |
assertEq(t, "struct.E", true, v.E == nil) |
| 178 |
t.Run("struct.field null", func(t *testing.T) { |
| 179 |
var v struct { |
| 180 |
A string |
| 181 |
B []string |
| 182 |
C []int |
| 183 |
D map[string]any |
| 184 |
E [2]string |
| 185 |
F any |
| 186 |
G func() |
| 187 |
} |
| 188 |
assertErr(t, json.Unmarshal([]byte(`{"a":null,"b":null,"c":null,"d":null,"e":null,"f":null,"g":null}`), &v)) |
| 189 |
assertEq(t, "string", v.A, "") |
| 190 |
assertNeq(t, "[]string", v.B, nil) |
| 191 |
assertEq(t, "[]string", len(v.B), 0) |
| 192 |
assertNeq(t, "[]int", v.C, nil) |
| 193 |
assertEq(t, "[]int", len(v.C), 0) |
| 194 |
assertNeq(t, "map", v.D, nil) |
| 195 |
assertEq(t, "map", len(v.D), 0) |
| 196 |
assertNeq(t, "array", v.E, nil) |
| 197 |
assertEq(t, "array", len(v.E), 2) |
| 198 |
assertEq(t, "any", v.F, nil) |
| 199 |
assertEq(t, "nilfunc", true, v.G == nil) |
| 200 |
}) |
| 201 |
t.Run("struct.pointer as encoding/json", func(t *testing.T) { |
| 202 |
// the pointer is set to a zero value before the type error of the value, as encoding/json does |
| 203 |
var v, want struct { |
| 204 |
A *int |
| 205 |
} |
| 206 |
json.Unmarshal([]byte(`{"a": "alpha"}`), &v) |
| 207 |
stdjson.Unmarshal([]byte(`{"a": "alpha"}`), &want) |
| 208 |
assertEq(t, "struct.A", want.A == nil, v.A == nil) |
| 209 |
}) |
| 210 |
}) |
| 211 |
t.Run("interface", func(t *testing.T) { |
| 212 |
t.Run("number", func(t *testing.T) { |
| 213 |
var v any |
| 214 |
assertErr(t, json.Unmarshal([]byte(`10`), &v)) |
| 215 |
assertEq(t, "interface.kind", "float64", reflect.TypeOf(v).Kind().String()) |
| 216 |
assertEq(t, "interface", `10`, fmt.Sprint(v)) |
| 217 |
}) |
| 218 |
t.Run("string", func(t *testing.T) { |
| 219 |
var v any |
| 220 |
assertErr(t, json.Unmarshal([]byte(`"hello"`), &v)) |
| 221 |
assertEq(t, "interface.kind", "string", reflect.TypeOf(v).Kind().String()) |
| 222 |
assertEq(t, "interface", `hello`, fmt.Sprint(v)) |
| 223 |
}) |
| 224 |
t.Run("escaped string", func(t *testing.T) { |
| 225 |
var v any |
| 226 |
assertErr(t, json.Unmarshal([]byte(`"he\"llo"`), &v)) |
| 227 |
assertEq(t, "interface.kind", "string", reflect.TypeOf(v).Kind().String()) |
| 228 |
assertEq(t, "interface", `he"llo`, fmt.Sprint(v)) |
| 229 |
}) |
| 230 |
t.Run("bool", func(t *testing.T) { |
| 231 |
var v any |
| 232 |
assertErr(t, json.Unmarshal([]byte(`true`), &v)) |
| 233 |
assertEq(t, "interface.kind", "bool", reflect.TypeOf(v).Kind().String()) |
| 234 |
assertEq(t, "interface", `true`, fmt.Sprint(v)) |
| 235 |
}) |
| 236 |
t.Run("slice", func(t *testing.T) { |
| 237 |
var v any |
| 238 |
assertErr(t, json.Unmarshal([]byte(`[1,2,3,4]`), &v)) |
| 239 |
assertEq(t, "interface.kind", "slice", reflect.TypeOf(v).Kind().String()) |
| 240 |
assertEq(t, "interface", `[1 2 3 4]`, fmt.Sprint(v)) |
| 241 |
}) |
| 242 |
t.Run("map", func(t *testing.T) { |
| 243 |
var v any |
| 244 |
assertErr(t, json.Unmarshal([]byte(`{"a": 1, "b": "c"}`), &v)) |
| 245 |
assertEq(t, "interface.kind", "map", reflect.TypeOf(v).Kind().String()) |
| 246 |
m := v.(map[string]any) |
| 247 |
assertEq(t, "interface", `1`, fmt.Sprint(m["a"])) |
| 248 |
assertEq(t, "interface", `c`, fmt.Sprint(m["b"])) |
| 249 |
}) |
| 250 |
t.Run("null", func(t *testing.T) { |
| 251 |
var v any |
| 252 |
v = 1 |
| 253 |
assertErr(t, json.Unmarshal([]byte(`null`), &v)) |
| 254 |
assertEq(t, "interface", nil, v) |
| 255 |
}) |
| 256 |
}) |
| 257 |
t.Run("func", func(t *testing.T) { |
| 258 |
var v func() |
| 259 |
assertErr(t, json.Unmarshal([]byte(`null`), &v)) |
| 260 |
assertEq(t, "nilfunc", true, v == nil) |
| 261 |
}) |
| 262 |
} |
| 263 |
|
| 264 |
func TestIssue98(t *testing.T) { |
| 265 |
data := "[\"\\" |
| 266 |
var v any |
| 267 |
if err := json.Unmarshal([]byte(data), &v); err == nil { |
| 268 |
t.Fatal("expected error") |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
func Test_Decoder_UseNumber(t *testing.T) { |
| 273 |
dec := json.NewDecoder(strings.NewReader(`{"a": 3.14}`)) |
| 274 |
dec.UseNumber() |
| 275 |
var v map[string]any |
| 276 |
assertErr(t, dec.Decode(&v)) |
| 277 |
assertEq(t, "json.Number", "json.Number", fmt.Sprintf("%T", v["a"])) |
| 278 |
} |
| 279 |
|
| 280 |
func Test_Decoder_DisallowUnknownFields(t *testing.T) { |
| 281 |
dec := json.NewDecoder(strings.NewReader(`{"x": 1}`)) |
| 282 |
dec.DisallowUnknownFields() |
| 283 |
var v struct { |
| 284 |
x int |
| 285 |
} |
| 286 |
err := dec.Decode(&v) |
| 287 |
if err == nil { |
| 288 |
t.Fatal("expected unknown field error") |
| 289 |
} |
| 290 |
if err.Error() != `json: unknown field "x"` { |
| 291 |
t.Fatal("expected unknown field error") |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
func Test_Decoder_EmptyObjectWithSpace(t *testing.T) { |
| 296 |
dec := json.NewDecoder(strings.NewReader(`{"obj":{ }}`)) |
| 297 |
var v struct { |
| 298 |
Obj map[string]int `json:"obj"` |
| 299 |
} |
| 300 |
assertErr(t, dec.Decode(&v)) |
| 301 |
} |
| 302 |
|
| 303 |
type unmarshalJSON struct { |
| 304 |
v int |
| 305 |
} |
| 306 |
|
| 307 |
func (u *unmarshalJSON) UnmarshalJSON(b []byte) error { |
| 308 |
var v int |
| 309 |
if err := json.Unmarshal(b, &v); err != nil { |
| 310 |
return err |
| 311 |
} |
| 312 |
u.v = v |
| 313 |
return nil |
| 314 |
} |
| 315 |
|
| 316 |
func Test_UnmarshalJSON(t *testing.T) { |
| 317 |
t.Run("*struct", func(t *testing.T) { |
| 318 |
var v unmarshalJSON |
| 319 |
assertErr(t, json.Unmarshal([]byte(`10`), &v)) |
| 320 |
assertEq(t, "unmarshal", 10, v.v) |
| 321 |
}) |
| 322 |
} |
| 323 |
|
| 324 |
type unmarshalText struct { |
| 325 |
v int |
| 326 |
} |
| 327 |
|
| 328 |
func (u *unmarshalText) UnmarshalText(b []byte) error { |
| 329 |
var v int |
| 330 |
if err := json.Unmarshal(b, &v); err != nil { |
| 331 |
return err |
| 332 |
} |
| 333 |
u.v = v |
| 334 |
return nil |
| 335 |
} |
| 336 |
|
| 337 |
func Test_UnmarshalText(t *testing.T) { |
| 338 |
t.Run("*struct", func(t *testing.T) { |
| 339 |
var v unmarshalText |
| 340 |
assertErr(t, json.Unmarshal([]byte(`"11"`), &v)) |
| 341 |
assertEq(t, "unmarshal", v.v, 11) |
| 342 |
}) |
| 343 |
} |
| 344 |
|
| 345 |
func Test_InvalidUnmarshalError(t *testing.T) { |
| 346 |
t.Run("nil", func(t *testing.T) { |
| 347 |
var v *struct{} |
| 348 |
err := fmt.Sprint(json.Unmarshal([]byte(`{}`), v)) |
| 349 |
assertEq(t, "invalid unmarshal error", "json: Unmarshal(nil *struct {})", err) |
| 350 |
}) |
| 351 |
t.Run("non pointer", func(t *testing.T) { |
| 352 |
var v int |
| 353 |
err := fmt.Sprint(json.Unmarshal([]byte(`{}`), v)) |
| 354 |
assertEq(t, "invalid unmarshal error", "json: Unmarshal(non-pointer int)", err) |
| 355 |
}) |
| 356 |
} |
| 357 |
|
| 358 |
func Test_Token(t *testing.T) { |
| 359 |
dec := json.NewDecoder(strings.NewReader(`{"a": 1, "b": true, "c": [1, "two", null]}`)) |
| 360 |
cnt := 0 |
| 361 |
for { |
| 362 |
if _, err := dec.Token(); err != nil { |
| 363 |
break |
| 364 |
} |
| 365 |
cnt++ |
| 366 |
} |
| 367 |
if cnt != 12 { |
| 368 |
t.Fatal("failed to parse token") |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
func Test_DecodeStream(t *testing.T) { |
| 373 |
const stream = ` |
| 374 |
[ |
| 375 |
{"Name": "Ed", "Text": "Knock knock."}, |
| 376 |
{"Name": "Sam", "Text": "Who's there?"}, |
| 377 |
{"Name": "Ed", "Text": "Go fmt."}, |
| 378 |
{"Name": "Sam", "Text": "Go fmt who?"}, |
| 379 |
{"Name": "Ed", "Text": "Go fmt yourself!"} |
| 380 |
] |
| 381 |
` |
| 382 |
type Message struct { |
| 383 |
Name, Text string |
| 384 |
} |
| 385 |
dec := json.NewDecoder(strings.NewReader(stream)) |
| 386 |
|
| 387 |
tk, err := dec.Token() |
| 388 |
assertErr(t, err) |
| 389 |
assertEq(t, "[", fmt.Sprint(tk), "[") |
| 390 |
|
| 391 |
elem := 0 |
| 392 |
// while the array contains values |
| 393 |
for dec.More() { |
| 394 |
var m Message |
| 395 |
// decode an array value (Message) |
| 396 |
assertErr(t, dec.Decode(&m)) |
| 397 |
if m.Name == "" || m.Text == "" { |
| 398 |
t.Fatal("failed to assign value to struct field") |
| 399 |
} |
| 400 |
elem++ |
| 401 |
} |
| 402 |
assertEq(t, "decode count", elem, 5) |
| 403 |
|
| 404 |
tk, err = dec.Token() |
| 405 |
assertErr(t, err) |
| 406 |
assertEq(t, "]", fmt.Sprint(tk), "]") |
| 407 |
} |
| 408 |
|
| 409 |
type T struct { |
| 410 |
X string |
| 411 |
Y int |
| 412 |
Z int `json:"-"` |
| 413 |
} |
| 414 |
|
| 415 |
type U struct { |
| 416 |
Alphabet string `json:"alpha"` |
| 417 |
} |
| 418 |
|
| 419 |
type V struct { |
| 420 |
F1 any |
| 421 |
F2 int32 |
| 422 |
F3 json.Number |
| 423 |
F4 *VOuter |
| 424 |
} |
| 425 |
|
| 426 |
type VOuter struct { |
| 427 |
V V |
| 428 |
} |
| 429 |
|
| 430 |
type W struct { |
| 431 |
S SS |
| 432 |
} |
| 433 |
|
| 434 |
type P struct { |
| 435 |
PP PP |
| 436 |
} |
| 437 |
|
| 438 |
type PP struct { |
| 439 |
T T |
| 440 |
Ts []T |
| 441 |
} |
| 442 |
|
| 443 |
type SS string |
| 444 |
|
| 445 |
func (*SS) UnmarshalJSON(data []byte) error { |
| 446 |
return &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(SS(""))} |
| 447 |
} |
| 448 |
|
| 449 |
// ifaceNumAsFloat64/ifaceNumAsNumber are used to test unmarshaling with and |
| 450 |
// without UseNumber |
| 451 |
var ifaceNumAsFloat64 = map[string]any{ |
| 452 |
"k1": float64(1), |
| 453 |
"k2": "s", |
| 454 |
"k3": []any{float64(1), float64(2.0), float64(3e-3)}, |
| 455 |
"k4": map[string]any{"kk1": "s", "kk2": float64(2)}, |
| 456 |
} |
| 457 |
|
| 458 |
var ifaceNumAsNumber = map[string]any{ |
| 459 |
"k1": json.Number("1"), |
| 460 |
"k2": "s", |
| 461 |
"k3": []any{json.Number("1"), json.Number("2.0"), json.Number("3e-3")}, |
| 462 |
"k4": map[string]any{"kk1": "s", "kk2": json.Number("2")}, |
| 463 |
} |
| 464 |
|
| 465 |
type tx struct { |
| 466 |
x int |
| 467 |
} |
| 468 |
|
| 469 |
type u8 uint8 |
| 470 |
|
| 471 |
// A type that can unmarshal itself. |
| 472 |
|
| 473 |
type unmarshaler struct { |
| 474 |
T bool |
| 475 |
} |
| 476 |
|
| 477 |
func (u *unmarshaler) UnmarshalJSON(b []byte) error { |
| 478 |
*u = unmarshaler{true} // All we need to see that UnmarshalJSON is called. |
| 479 |
return nil |
| 480 |
} |
| 481 |
|
| 482 |
type ustruct struct { |
| 483 |
M unmarshaler |
| 484 |
} |
| 485 |
|
| 486 |
var _ encoding.TextUnmarshaler = (*unmarshalerText)(nil) |
| 487 |
|
| 488 |
type ustructText struct { |
| 489 |
M unmarshalerText |
| 490 |
} |
| 491 |
|
| 492 |
// u8marshal is an integer type that can marshal/unmarshal itself. |
| 493 |
type u8marshal uint8 |
| 494 |
|
| 495 |
func (u8 u8marshal) MarshalText() ([]byte, error) { |
| 496 |
return []byte(fmt.Sprintf("u%d", u8)), nil |
| 497 |
} |
| 498 |
|
| 499 |
var errMissingU8Prefix = errors.New("missing 'u' prefix") |
| 500 |
|
| 501 |
func (u8 *u8marshal) UnmarshalText(b []byte) error { |
| 502 |
if !bytes.HasPrefix(b, []byte{'u'}) { |
| 503 |
return errMissingU8Prefix |
| 504 |
} |
| 505 |
n, err := strconv.Atoi(string(b[1:])) |
| 506 |
if err != nil { |
| 507 |
return err |
| 508 |
} |
| 509 |
*u8 = u8marshal(n) |
| 510 |
return nil |
| 511 |
} |
| 512 |
|
| 513 |
var _ encoding.TextUnmarshaler = (*u8marshal)(nil) |
| 514 |
|
| 515 |
var ( |
| 516 |
umtrue = unmarshaler{true} |
| 517 |
umslice = []unmarshaler{{true}} |
| 518 |
umstruct = ustruct{unmarshaler{true}} |
| 519 |
|
| 520 |
umtrueXY = unmarshalerText{"x", "y"} |
| 521 |
umsliceXY = []unmarshalerText{{"x", "y"}} |
| 522 |
umstructXY = ustructText{unmarshalerText{"x", "y"}} |
| 523 |
|
| 524 |
ummapXY = map[unmarshalerText]bool{{"x", "y"}: true} |
| 525 |
) |
| 526 |
|
| 527 |
// Test data structures for anonymous fields. |
| 528 |
|
| 529 |
type Point struct { |
| 530 |
Z int |
| 531 |
} |
| 532 |
|
| 533 |
type Top struct { |
| 534 |
Level0 int |
| 535 |
Embed0 |
| 536 |
*Embed0a |
| 537 |
*Embed0b `json:"e,omitempty"` // treated as named |
| 538 |
Embed0c `json:"-"` // ignored |
| 539 |
Loop |
| 540 |
Embed0p // has Point with X, Y, used |
| 541 |
Embed0q // has Point with Z, used |
| 542 |
embed // contains exported field |
| 543 |
} |
| 544 |
|
| 545 |
type Embed0 struct { |
| 546 |
Level1a int // overridden by Embed0a's Level1a with json tag |
| 547 |
Level1b int // used because Embed0a's Level1b is renamed |
| 548 |
Level1c int // used because Embed0a's Level1c is ignored |
| 549 |
Level1d int // annihilated by Embed0a's Level1d |
| 550 |
Level1e int `json:"x"` // annihilated by Embed0a.Level1e |
| 551 |
} |
| 552 |
|
| 553 |
type Embed0a struct { |
| 554 |
Level1a int `json:"Level1a,omitempty"` |
| 555 |
Level1b int `json:"LEVEL1B,omitempty"` |
| 556 |
Level1c int `json:"-"` |
| 557 |
Level1d int // annihilated by Embed0's Level1d |
| 558 |
Level1f int `json:"x"` // annihilated by Embed0's Level1e |
| 559 |
} |
| 560 |
|
| 561 |
type Embed0b Embed0 |
| 562 |
|
| 563 |
type Embed0c Embed0 |
| 564 |
|
| 565 |
type Embed0p struct { |
| 566 |
image.Point |
| 567 |
} |
| 568 |
|
| 569 |
type Embed0q struct { |
| 570 |
Point |
| 571 |
} |
| 572 |
|
| 573 |
type embed struct { |
| 574 |
Q int |
| 575 |
} |
| 576 |
|
| 577 |
type Loop struct { |
| 578 |
Loop1 int `json:",omitempty"` |
| 579 |
Loop2 int `json:",omitempty"` |
| 580 |
*Loop |
| 581 |
} |
| 582 |
|
| 583 |
// From reflect test: |
| 584 |
// The X in S6 and S7 annihilate, but they also block the X in S8.S9. |
| 585 |
type S5 struct { |
| 586 |
S6 |
| 587 |
S7 |
| 588 |
S8 |
| 589 |
} |
| 590 |
|
| 591 |
type S6 struct { |
| 592 |
X int |
| 593 |
} |
| 594 |
|
| 595 |
type S7 S6 |
| 596 |
|
| 597 |
type S8 struct { |
| 598 |
S9 |
| 599 |
} |
| 600 |
|
| 601 |
type S9 struct { |
| 602 |
X int |
| 603 |
Y int |
| 604 |
} |
| 605 |
|
| 606 |
// From reflect test: |
| 607 |
// The X in S11.S6 and S12.S6 annihilate, but they also block the X in S13.S8.S9. |
| 608 |
type S10 struct { |
| 609 |
S11 |
| 610 |
S12 |
| 611 |
S13 |
| 612 |
} |
| 613 |
|
| 614 |
type S11 struct { |
| 615 |
S6 |
| 616 |
} |
| 617 |
|
| 618 |
type S12 struct { |
| 619 |
S6 |
| 620 |
} |
| 621 |
|
| 622 |
type S13 struct { |
| 623 |
S8 |
| 624 |
} |
| 625 |
|
| 626 |
type Ambig struct { |
| 627 |
// Given "hello", the first match should win. |
| 628 |
First int `json:"HELLO"` |
| 629 |
Second int `json:"Hello"` |
| 630 |
} |
| 631 |
|
| 632 |
type XYZ struct { |
| 633 |
X any |
| 634 |
Y any |
| 635 |
Z any |
| 636 |
} |
| 637 |
|
| 638 |
type unexportedWithMethods struct{} |
| 639 |
|
| 640 |
func (unexportedWithMethods) F() {} |
| 641 |
|
| 642 |
type byteWithMarshalJSON byte |
| 643 |
|
| 644 |
func (b byteWithMarshalJSON) MarshalJSON() ([]byte, error) { |
| 645 |
return []byte(fmt.Sprintf(`"Z%.2x"`, byte(b))), nil |
| 646 |
} |
| 647 |
|
| 648 |
func (b *byteWithMarshalJSON) UnmarshalJSON(data []byte) error { |
| 649 |
if len(data) != 5 || data[0] != '"' || data[1] != 'Z' || data[4] != '"' { |
| 650 |
return fmt.Errorf("bad quoted string") |
| 651 |
} |
| 652 |
i, err := strconv.ParseInt(string(data[2:4]), 16, 8) |
| 653 |
if err != nil { |
| 654 |
return fmt.Errorf("bad hex") |
| 655 |
} |
| 656 |
*b = byteWithMarshalJSON(i) |
| 657 |
return nil |
| 658 |
} |
| 659 |
|
| 660 |
type byteWithPtrMarshalJSON byte |
| 661 |
|
| 662 |
func (b *byteWithPtrMarshalJSON) MarshalJSON() ([]byte, error) { |
| 663 |
return byteWithMarshalJSON(*b).MarshalJSON() |
| 664 |
} |
| 665 |
|
| 666 |
func (b *byteWithPtrMarshalJSON) UnmarshalJSON(data []byte) error { |
| 667 |
return (*byteWithMarshalJSON)(b).UnmarshalJSON(data) |
| 668 |
} |
| 669 |
|
| 670 |
type byteWithMarshalText byte |
| 671 |
|
| 672 |
func (b byteWithMarshalText) MarshalText() ([]byte, error) { |
| 673 |
return []byte(fmt.Sprintf(`Z%.2x`, byte(b))), nil |
| 674 |
} |
| 675 |
|
| 676 |
func (b *byteWithMarshalText) UnmarshalText(data []byte) error { |
| 677 |
if len(data) != 3 || data[0] != 'Z' { |
| 678 |
return fmt.Errorf("bad quoted string") |
| 679 |
} |
| 680 |
i, err := strconv.ParseInt(string(data[1:3]), 16, 8) |
| 681 |
if err != nil { |
| 682 |
return fmt.Errorf("bad hex") |
| 683 |
} |
| 684 |
*b = byteWithMarshalText(i) |
| 685 |
return nil |
| 686 |
} |
| 687 |
|
| 688 |
type byteWithPtrMarshalText byte |
| 689 |
|
| 690 |
func (b *byteWithPtrMarshalText) MarshalText() ([]byte, error) { |
| 691 |
return byteWithMarshalText(*b).MarshalText() |
| 692 |
} |
| 693 |
|
| 694 |
func (b *byteWithPtrMarshalText) UnmarshalText(data []byte) error { |
| 695 |
return (*byteWithMarshalText)(b).UnmarshalText(data) |
| 696 |
} |
| 697 |
|
| 698 |
type intWithMarshalJSON int |
| 699 |
|
| 700 |
func (b intWithMarshalJSON) MarshalJSON() ([]byte, error) { |
| 701 |
return []byte(fmt.Sprintf(`"Z%.2x"`, int(b))), nil |
| 702 |
} |
| 703 |
|
| 704 |
func (b *intWithMarshalJSON) UnmarshalJSON(data []byte) error { |
| 705 |
if len(data) != 5 || data[0] != '"' || data[1] != 'Z' || data[4] != '"' { |
| 706 |
return fmt.Errorf("bad quoted string") |
| 707 |
} |
| 708 |
i, err := strconv.ParseInt(string(data[2:4]), 16, 8) |
| 709 |
if err != nil { |
| 710 |
return fmt.Errorf("bad hex") |
| 711 |
} |
| 712 |
*b = intWithMarshalJSON(i) |
| 713 |
return nil |
| 714 |
} |
| 715 |
|
| 716 |
type intWithPtrMarshalJSON int |
| 717 |
|
| 718 |
func (b *intWithPtrMarshalJSON) MarshalJSON() ([]byte, error) { |
| 719 |
return intWithMarshalJSON(*b).MarshalJSON() |
| 720 |
} |
| 721 |
|
| 722 |
func (b *intWithPtrMarshalJSON) UnmarshalJSON(data []byte) error { |
| 723 |
return (*intWithMarshalJSON)(b).UnmarshalJSON(data) |
| 724 |
} |
| 725 |
|
| 726 |
type intWithMarshalText int |
| 727 |
|
| 728 |
func (b intWithMarshalText) MarshalText() ([]byte, error) { |
| 729 |
return []byte(fmt.Sprintf(`Z%.2x`, int(b))), nil |
| 730 |
} |
| 731 |
|
| 732 |
func (b *intWithMarshalText) UnmarshalText(data []byte) error { |
| 733 |
if len(data) != 3 || data[0] != 'Z' { |
| 734 |
return fmt.Errorf("bad quoted string") |
| 735 |
} |
| 736 |
i, err := strconv.ParseInt(string(data[1:3]), 16, 8) |
| 737 |
if err != nil { |
| 738 |
return fmt.Errorf("bad hex") |
| 739 |
} |
| 740 |
*b = intWithMarshalText(i) |
| 741 |
return nil |
| 742 |
} |
| 743 |
|
| 744 |
type intWithPtrMarshalText int |
| 745 |
|
| 746 |
func (b *intWithPtrMarshalText) MarshalText() ([]byte, error) { |
| 747 |
return intWithMarshalText(*b).MarshalText() |
| 748 |
} |
| 749 |
|
| 750 |
func (b *intWithPtrMarshalText) UnmarshalText(data []byte) error { |
| 751 |
return (*intWithMarshalText)(b).UnmarshalText(data) |
| 752 |
} |
| 753 |
|
| 754 |
type mapStringToStringData struct { |
| 755 |
Data map[string]string `json:"data"` |
| 756 |
} |
| 757 |
|
| 758 |
type unmarshalTest struct { |
| 759 |
in string |
| 760 |
ptr any // new(type) |
| 761 |
out any |
| 762 |
err error |
| 763 |
useNumber bool |
| 764 |
golden bool |
| 765 |
disallowUnknownFields bool |
| 766 |
} |
| 767 |
|
| 768 |
type B struct { |
| 769 |
B bool `json:",string"` |
| 770 |
} |
| 771 |
|
| 772 |
type DoublePtr struct { |
| 773 |
I **int |
| 774 |
J **int |
| 775 |
} |
| 776 |
|
| 777 |
var unmarshalTests = []unmarshalTest{ |
| 778 |
// basic types |
| 779 |
{in: `true`, ptr: new(bool), out: true}, // 0 |
| 780 |
{in: `1`, ptr: new(int), out: 1}, // 1 |
| 781 |
{in: `1.2`, ptr: new(float64), out: 1.2}, // 2 |
| 782 |
{in: `-5`, ptr: new(int16), out: int16(-5)}, // 3 |
| 783 |
{in: `2`, ptr: new(json.Number), out: json.Number("2"), useNumber: true}, // 4 |
| 784 |
{in: `2`, ptr: new(json.Number), out: json.Number("2")}, // 5 |
| 785 |
{in: `2`, ptr: new(any), out: float64(2.0)}, // 6 |
| 786 |
{in: `2`, ptr: new(any), out: json.Number("2"), useNumber: true}, // 7 |
| 787 |
{in: `"a\u1234"`, ptr: new(string), out: "a\u1234"}, // 8 |
| 788 |
{in: `"http:\/\/"`, ptr: new(string), out: "http://"}, // 9 |
| 789 |
{in: `"g-clef: \uD834\uDD1E"`, ptr: new(string), out: "g-clef: \U0001D11E"}, // 10 |
| 790 |
{in: `"invalid: \uD834x\uDD1E"`, ptr: new(string), out: "invalid: \uFFFDx\uFFFD"}, // 11 |
| 791 |
{in: "null", ptr: new(any), out: nil}, // 12 |
| 792 |
{in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &json.UnmarshalTypeError{Value: "array", Type: reflect.TypeOf(""), Offset: 7, Struct: "T", Field: "X"}}, // 13 |
| 793 |
{in: `{"X": 23}`, ptr: new(T), out: T{}, err: &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 8, Struct: "T", Field: "X"}}, {in: `{"x": 1}`, ptr: new(tx), out: tx{}}, // 14 |
| 794 |
{in: `{"x": 1}`, ptr: new(tx), out: tx{}}, // 15, 16 |
| 795 |
{in: `{"x": 1}`, ptr: new(tx), err: fmt.Errorf("json: unknown field \"x\""), disallowUnknownFields: true}, // 17 |
| 796 |
{in: `{"S": 23}`, ptr: new(W), out: W{}, err: &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(SS("")), Offset: 0, Struct: "W", Field: "S"}}, // 18 |
| 797 |
{in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: json.Number("3")}}, // 19 |
| 798 |
{in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: json.Number("1"), F2: int32(2), F3: json.Number("3")}, useNumber: true}, // 20 |
| 799 |
{in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(any), out: ifaceNumAsFloat64}, // 21 |
| 800 |
{in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(any), out: ifaceNumAsNumber, useNumber: true}, // 22 |
| 801 |
|
| 802 |
// raw values with whitespace |
| 803 |
{in: "\n true ", ptr: new(bool), out: true}, // 23 |
| 804 |
{in: "\t 1 ", ptr: new(int), out: 1}, // 24 |
| 805 |
{in: "\r 1.2 ", ptr: new(float64), out: 1.2}, // 25 |
| 806 |
{in: "\t -5 \n", ptr: new(int16), out: int16(-5)}, // 26 |
| 807 |
{in: "\t \"a\\u1234\" \n", ptr: new(string), out: "a\u1234"}, // 27 |
| 808 |
|
| 809 |
// Z has a "-" tag. |
| 810 |
{in: `{"Y": 1, "Z": 2}`, ptr: new(T), out: T{Y: 1}}, // 28 |
| 811 |
{in: `{"Y": 1, "Z": 2}`, ptr: new(T), err: fmt.Errorf("json: unknown field \"Z\""), disallowUnknownFields: true}, // 29 |
| 812 |
|
| 813 |
{in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), out: U{Alphabet: "abc"}}, // 30 |
| 814 |
{in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), err: fmt.Errorf("json: unknown field \"alphabet\""), disallowUnknownFields: true}, // 31 |
| 815 |
{in: `{"alpha": "abc"}`, ptr: new(U), out: U{Alphabet: "abc"}}, // 32 |
| 816 |
{in: `{"alphabet": "xyz"}`, ptr: new(U), out: U{}}, // 33 |
| 817 |
{in: `{"alphabet": "xyz"}`, ptr: new(U), err: fmt.Errorf("json: unknown field \"alphabet\""), disallowUnknownFields: true}, // 34 |
| 818 |
|
| 819 |
// syntax errors |
| 820 |
{in: `{"X": "foo", "Y"}`, err: json.NewSyntaxError("invalid character '}' after object key", 17)}, // 35 |
| 821 |
{in: `[1, 2, 3+]`, err: json.NewSyntaxError("invalid character '+' after array element", 9)}, // 36 |
| 822 |
{in: `{"X":12x}`, err: json.NewSyntaxError("invalid character 'x' after object key:value pair", 8), useNumber: true}, // 37 |
| 823 |
{in: `[2, 3`, err: json.NewSyntaxError("unexpected end of JSON input", 5)}, // 38 |
| 824 |
{in: `{"F3": -}`, ptr: new(V), out: V{F3: json.Number("-")}, err: json.NewSyntaxError("invalid character '}' in numeric literal", 9)}, // 39 |
| 825 |
|
| 826 |
// raw value errors |
| 827 |
{in: "\x01 42", err: json.NewSyntaxError("invalid character '\\x01' looking for beginning of value", 1)}, // 40 |
| 828 |
{in: " 42 \x01", err: json.NewSyntaxError("invalid character '\\x01' after top-level value", 5)}, // 41 |
| 829 |
{in: "\x01 true", err: json.NewSyntaxError("invalid character '\\x01' looking for beginning of value", 1)}, // 42 |
| 830 |
{in: " false \x01", err: json.NewSyntaxError("invalid character '\\x01' after top-level value", 8)}, // 43 |
| 831 |
{in: "\x01 1.2", err: json.NewSyntaxError("invalid character '\\x01' looking for beginning of value", 1)}, // 44 |
| 832 |
{in: " 3.4 \x01", err: json.NewSyntaxError("invalid character '\\x01' after top-level value", 6)}, // 45 |
| 833 |
{in: "\x01 \"string\"", err: json.NewSyntaxError("invalid character '\\x01' looking for beginning of value", 1)}, // 46 |
| 834 |
{in: " \"string\" \x01", err: json.NewSyntaxError("invalid character '\\x01' after top-level value", 11)}, // 47 |
| 835 |
|
| 836 |
// array tests |
| 837 |
{in: `[1, 2, 3]`, ptr: new([3]int), out: [3]int{1, 2, 3}}, // 48 |
| 838 |
{in: `[1, 2, 3]`, ptr: new([1]int), out: [1]int{1}}, // 49 |
| 839 |
{in: `[1, 2, 3]`, ptr: new([5]int), out: [5]int{1, 2, 3, 0, 0}}, // 50 |
| 840 |
{in: `[1, 2, 3]`, ptr: new(MustNotUnmarshalJSON), err: errors.New("MustNotUnmarshalJSON was used")}, // 51 |
| 841 |
|
| 842 |
// empty array to interface test |
| 843 |
{in: `[]`, ptr: new([]any), out: []any{}}, //52 |
| 844 |
{in: `null`, ptr: new([]any), out: []any(nil)}, //53 |
| 845 |
{in: `{"T":[]}`, ptr: new(map[string]any), out: map[string]any{"T": []any{}}}, //54 |
| 846 |
{in: `{"T":null}`, ptr: new(map[string]any), out: map[string]any{"T": any(nil)}}, // 55 |
| 847 |
|
| 848 |
// composite tests |
| 849 |
{in: allValueIndent, ptr: new(All), out: allValue}, // 56 |
| 850 |
{in: allValueCompact, ptr: new(All), out: allValue}, // 57 |
| 851 |
{in: allValueIndent, ptr: new(*All), out: &allValue}, // 58 |
| 852 |
{in: allValueCompact, ptr: new(*All), out: &allValue}, // 59 |
| 853 |
{in: pallValueIndent, ptr: new(All), out: pallValue}, // 60 |
| 854 |
{in: pallValueCompact, ptr: new(All), out: pallValue}, // 61 |
| 855 |
{in: pallValueIndent, ptr: new(*All), out: &pallValue}, // 62 |
| 856 |
{in: pallValueCompact, ptr: new(*All), out: &pallValue}, // 63 |
| 857 |
|
| 858 |
// unmarshal interface test |
| 859 |
{in: `{"T":false}`, ptr: new(unmarshaler), out: umtrue}, // use "false" so test will fail if custom unmarshaler is not called |
| 860 |
{in: `{"T":false}`, ptr: new(*unmarshaler), out: &umtrue}, // 65 |
| 861 |
{in: `[{"T":false}]`, ptr: new([]unmarshaler), out: umslice}, // 66 |
| 862 |
{in: `[{"T":false}]`, ptr: new(*[]unmarshaler), out: &umslice}, // 67 |
| 863 |
{in: `{"M":{"T":"x:y"}}`, ptr: new(ustruct), out: umstruct}, // 68 |
| 864 |
|
| 865 |
// UnmarshalText interface test |
| 866 |
{in: `"x:y"`, ptr: new(unmarshalerText), out: umtrueXY}, // 69 |
| 867 |
{in: `"x:y"`, ptr: new(*unmarshalerText), out: &umtrueXY}, // 70 |
| 868 |
{in: `["x:y"]`, ptr: new([]unmarshalerText), out: umsliceXY}, // 71 |
| 869 |
{in: `["x:y"]`, ptr: new(*[]unmarshalerText), out: &umsliceXY}, // 72 |
| 870 |
{in: `{"M":"x:y"}`, ptr: new(ustructText), out: umstructXY}, // 73 |
| 871 |
// integer-keyed map test |
| 872 |
{ |
| 873 |
in: `{"-1":"a","0":"b","1":"c"}`, // 74 |
| 874 |
ptr: new(map[int]string), |
| 875 |
out: map[int]string{-1: "a", 0: "b", 1: "c"}, |
| 876 |
}, |
| 877 |
{ |
| 878 |
in: `{"0":"a","10":"c","9":"b"}`, // 75 |
| 879 |
ptr: new(map[u8]string), |
| 880 |
out: map[u8]string{0: "a", 9: "b", 10: "c"}, |
| 881 |
}, |
| 882 |
{ |
| 883 |
in: `{"-9223372036854775808":"min","9223372036854775807":"max"}`, // 76 |
| 884 |
ptr: new(map[int64]string), |
| 885 |
out: map[int64]string{math.MinInt64: "min", math.MaxInt64: "max"}, |
| 886 |
}, |
| 887 |
{ |
| 888 |
in: `{"18446744073709551615":"max"}`, // 77 |
| 889 |
ptr: new(map[uint64]string), |
| 890 |
out: map[uint64]string{math.MaxUint64: "max"}, |
| 891 |
}, |
| 892 |
{ |
| 893 |
in: `{"0":false,"10":true}`, // 78 |
| 894 |
ptr: new(map[uintptr]bool), |
| 895 |
out: map[uintptr]bool{0: false, 10: true}, |
| 896 |
}, |
| 897 |
// Check that MarshalText and UnmarshalText take precedence |
| 898 |
// over default integer handling in map keys. |
| 899 |
{ |
| 900 |
in: `{"u2":4}`, // 79 |
| 901 |
ptr: new(map[u8marshal]int), |
| 902 |
out: map[u8marshal]int{2: 4}, |
| 903 |
}, |
| 904 |
{ |
| 905 |
in: `{"2":4}`, // 80 |
| 906 |
ptr: new(map[u8marshal]int), |
| 907 |
err: errMissingU8Prefix, |
| 908 |
}, |
| 909 |
// integer-keyed map errors |
| 910 |
{ |
| 911 |
in: `{"abc":"abc"}`, // 81 |
| 912 |
ptr: new(map[int]string), |
| 913 |
err: &json.UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(0), Offset: 2}, |
| 914 |
}, |
| 915 |
{ |
| 916 |
in: `{"256":"abc"}`, // 82 |
| 917 |
ptr: new(map[uint8]string), |
| 918 |
err: &json.UnmarshalTypeError{Value: "number 256", Type: reflect.TypeOf(uint8(0)), Offset: 2}, |
| 919 |
}, |
| 920 |
{ |
| 921 |
in: `{"128":"abc"}`, // 83 |
| 922 |
ptr: new(map[int8]string), |
| 923 |
err: &json.UnmarshalTypeError{Value: "number 128", Type: reflect.TypeOf(int8(0)), Offset: 2}, |
| 924 |
}, |
| 925 |
{ |
| 926 |
in: `{"-1":"abc"}`, // 84 |
| 927 |
ptr: new(map[uint8]string), |
| 928 |
err: &json.UnmarshalTypeError{Value: "number -", Type: reflect.TypeOf(uint8(0)), Offset: 2}, |
| 929 |
}, |
| 930 |
{ |
| 931 |
in: `{"F":{"a":2,"3":4}}`, // 85 |
| 932 |
ptr: new(map[string]map[int]int), |
| 933 |
err: &json.UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(int(0)), Offset: 7}, |
| 934 |
}, |
| 935 |
{ |
| 936 |
in: `{"F":{"a":2,"3":4}}`, // 86 |
| 937 |
ptr: new(map[string]map[uint]int), |
| 938 |
err: &json.UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(uint(0)), Offset: 7}, |
| 939 |
}, |
| 940 |
// Map keys can be encoding.TextUnmarshalers. |
| 941 |
{in: `{"x:y":true}`, ptr: new(map[unmarshalerText]bool), out: ummapXY}, // 87 |
| 942 |
// If multiple values for the same key exists, only the most recent value is used. |
| 943 |
{in: `{"x:y":false,"x:y":true}`, ptr: new(map[unmarshalerText]bool), out: ummapXY}, // 88 |
| 944 |
{ // 89 |
| 945 |
in: `{ |
| 946 |
"Level0": 1, |
| 947 |
"Level1b": 2, |
| 948 |
"Level1c": 3, |
| 949 |
"x": 4, |
| 950 |
"Level1a": 5, |
| 951 |
"LEVEL1B": 6, |
| 952 |
"e": { |
| 953 |
"Level1a": 8, |
| 954 |
"Level1b": 9, |
| 955 |
"Level1c": 10, |
| 956 |
"Level1d": 11, |
| 957 |
"x": 12 |
| 958 |
}, |
| 959 |
"Loop1": 13, |
| 960 |
"Loop2": 14, |
| 961 |
"X": 15, |
| 962 |
"Y": 16, |
| 963 |
"Z": 17, |
| 964 |
"Q": 18 |
| 965 |
}`, |
| 966 |
ptr: new(Top), |
| 967 |
out: Top{ |
| 968 |
Level0: 1, |
| 969 |
Embed0: Embed0{ |
| 970 |
Level1b: 2, |
| 971 |
Level1c: 3, |
| 972 |
}, |
| 973 |
Embed0a: &Embed0a{ |
| 974 |
Level1a: 5, |
| 975 |
Level1b: 6, |
| 976 |
}, |
| 977 |
Embed0b: &Embed0b{ |
| 978 |
Level1a: 8, |
| 979 |
Level1b: 9, |
| 980 |
Level1c: 10, |
| 981 |
Level1d: 11, |
| 982 |
Level1e: 12, |
| 983 |
}, |
| 984 |
Loop: Loop{ |
| 985 |
Loop1: 13, |
| 986 |
Loop2: 14, |
| 987 |
}, |
| 988 |
Embed0p: Embed0p{ |
| 989 |
Point: image.Point{X: 15, Y: 16}, |
| 990 |
}, |
| 991 |
Embed0q: Embed0q{ |
| 992 |
Point: Point{Z: 17}, |
| 993 |
}, |
| 994 |
embed: embed{ |
| 995 |
Q: 18, |
| 996 |
}, |
| 997 |
}, |
| 998 |
}, |
| 999 |
{ |
| 1000 |
in: `{"hello": 1}`, // 90 |
| 1001 |
ptr: new(Ambig), |
| 1002 |
out: Ambig{First: 1}, |
| 1003 |
}, |
| 1004 |
{ |
| 1005 |
in: `{"X": 1,"Y":2}`, // 91 |
| 1006 |
ptr: new(S5), |
| 1007 |
out: S5{S8: S8{S9: S9{Y: 2}}}, |
| 1008 |
}, |
| 1009 |
{ |
| 1010 |
in: `{"X": 1,"Y":2}`, // 92 |
| 1011 |
ptr: new(S5), |
| 1012 |
err: fmt.Errorf("json: unknown field \"X\""), |
| 1013 |
disallowUnknownFields: true, |
| 1014 |
}, |
| 1015 |
{ |
| 1016 |
in: `{"X": 1,"Y":2}`, // 93 |
| 1017 |
ptr: new(S10), |
| 1018 |
out: S10{S13: S13{S8: S8{S9: S9{Y: 2}}}}, |
| 1019 |
}, |
| 1020 |
{ |
| 1021 |
in: `{"X": 1,"Y":2}`, // 94 |
| 1022 |
ptr: new(S10), |
| 1023 |
err: fmt.Errorf("json: unknown field \"X\""), |
| 1024 |
disallowUnknownFields: true, |
| 1025 |
}, |
| 1026 |
{ |
| 1027 |
in: `{"I": 0, "I": null, "J": null}`, // 95 |
| 1028 |
ptr: new(DoublePtr), |
| 1029 |
out: DoublePtr{I: nil, J: nil}, |
| 1030 |
}, |
| 1031 |
{ |
| 1032 |
in: "\"hello\\ud800world\"", // 96 |
| 1033 |
ptr: new(string), |
| 1034 |
out: "hello\ufffdworld", |
| 1035 |
}, |
| 1036 |
{ |
| 1037 |
in: "\"hello\\ud800\\ud800world\"", // 97 |
| 1038 |
ptr: new(string), |
| 1039 |
out: "hello\ufffd\ufffdworld", |
| 1040 |
}, |
| 1041 |
{ |
| 1042 |
in: "\"hello\\ud800\\ud800world\"", // 98 |
| 1043 |
ptr: new(string), |
| 1044 |
out: "hello\ufffd\ufffdworld", |
| 1045 |
}, |
| 1046 |
// Used to be issue 8305, but time.Time implements encoding.TextUnmarshaler so this works now. |
| 1047 |
{ |
| 1048 |
in: `{"2009-11-10T23:00:00Z": "hello world"}`, // 99 |
| 1049 |
ptr: new(map[time.Time]string), |
| 1050 |
out: map[time.Time]string{time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC): "hello world"}, |
| 1051 |
}, |
| 1052 |
// issue 8305 |
| 1053 |
{ |
| 1054 |
in: `{"2009-11-10T23:00:00Z": "hello world"}`, // 100 |
| 1055 |
ptr: new(map[Point]string), |
| 1056 |
err: &json.UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(Point{}), Offset: 0}, |
| 1057 |
}, |
| 1058 |
{ |
| 1059 |
in: `{"asdf": "hello world"}`, // 101 |
| 1060 |
ptr: new(map[unmarshaler]string), |
| 1061 |
err: &json.UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(unmarshaler{}), Offset: 1}, |
| 1062 |
}, |
| 1063 |
// related to issue 13783. |
| 1064 |
// Go 1.7 changed marshaling a slice of typed byte to use the methods on the byte type, |
| 1065 |
// similar to marshaling a slice of typed int. |
| 1066 |
// These tests check that, assuming the byte type also has valid decoding methods, |
| 1067 |
// either the old base64 string encoding or the new per-element encoding can be |
| 1068 |
// successfully unmarshaled. The custom unmarshalers were accessible in earlier |
| 1069 |
// versions of Go, even though the custom marshaler was not. |
| 1070 |
{ |
| 1071 |
in: `"AQID"`, // 102 |
| 1072 |
ptr: new([]byteWithMarshalJSON), |
| 1073 |
out: []byteWithMarshalJSON{1, 2, 3}, |
| 1074 |
}, |
| 1075 |
{ |
| 1076 |
in: `["Z01","Z02","Z03"]`, // 103 |
| 1077 |
ptr: new([]byteWithMarshalJSON), |
| 1078 |
out: []byteWithMarshalJSON{1, 2, 3}, |
| 1079 |
golden: true, |
| 1080 |
}, |
| 1081 |
{ |
| 1082 |
in: `"AQID"`, // 104 |
| 1083 |
ptr: new([]byteWithMarshalText), |
| 1084 |
out: []byteWithMarshalText{1, 2, 3}, |
| 1085 |
}, |
| 1086 |
{ |
| 1087 |
in: `["Z01","Z02","Z03"]`, // 105 |
| 1088 |
ptr: new([]byteWithMarshalText), |
| 1089 |
out: []byteWithMarshalText{1, 2, 3}, |
| 1090 |
golden: true, |
| 1091 |
}, |
| 1092 |
{ |
| 1093 |
in: `"AQID"`, // 106 |
| 1094 |
ptr: new([]byteWithPtrMarshalJSON), |
| 1095 |
out: []byteWithPtrMarshalJSON{1, 2, 3}, |
| 1096 |
}, |
| 1097 |
{ |
| 1098 |
in: `["Z01","Z02","Z03"]`, // 107 |
| 1099 |
ptr: new([]byteWithPtrMarshalJSON), |
| 1100 |
out: []byteWithPtrMarshalJSON{1, 2, 3}, |
| 1101 |
golden: true, |
| 1102 |
}, |
| 1103 |
{ |
| 1104 |
in: `"AQID"`, // 108 |
| 1105 |
ptr: new([]byteWithPtrMarshalText), |
| 1106 |
out: []byteWithPtrMarshalText{1, 2, 3}, |
| 1107 |
}, |
| 1108 |
{ |
| 1109 |
in: `["Z01","Z02","Z03"]`, // 109 |
| 1110 |
ptr: new([]byteWithPtrMarshalText), |
| 1111 |
out: []byteWithPtrMarshalText{1, 2, 3}, |
| 1112 |
golden: true, |
| 1113 |
}, |
| 1114 |
|
| 1115 |
// ints work with the marshaler but not the base64 []byte case |
| 1116 |
{ |
| 1117 |
in: `["Z01","Z02","Z03"]`, // 110 |
| 1118 |
ptr: new([]intWithMarshalJSON), |
| 1119 |
out: []intWithMarshalJSON{1, 2, 3}, |
| 1120 |
golden: true, |
| 1121 |
}, |
| 1122 |
{ |
| 1123 |
in: `["Z01","Z02","Z03"]`, // 111 |
| 1124 |
ptr: new([]intWithMarshalText), |
| 1125 |
out: []intWithMarshalText{1, 2, 3}, |
| 1126 |
golden: true, |
| 1127 |
}, |
| 1128 |
{ |
| 1129 |
in: `["Z01","Z02","Z03"]`, // 112 |
| 1130 |
ptr: new([]intWithPtrMarshalJSON), |
| 1131 |
out: []intWithPtrMarshalJSON{1, 2, 3}, |
| 1132 |
golden: true, |
| 1133 |
}, |
| 1134 |
{ |
| 1135 |
in: `["Z01","Z02","Z03"]`, // 113 |
| 1136 |
ptr: new([]intWithPtrMarshalText), |
| 1137 |
out: []intWithPtrMarshalText{1, 2, 3}, |
| 1138 |
golden: true, |
| 1139 |
}, |
| 1140 |
|
| 1141 |
{in: `0.000001`, ptr: new(float64), out: 0.000001, golden: true}, // 114 |
| 1142 |
{in: `1e-7`, ptr: new(float64), out: 1e-7, golden: true}, // 115 |
| 1143 |
{in: `100000000000000000000`, ptr: new(float64), out: 100000000000000000000.0, golden: true}, // 116 |
| 1144 |
{in: `1e+21`, ptr: new(float64), out: 1e21, golden: true}, // 117 |
| 1145 |
{in: `-0.000001`, ptr: new(float64), out: -0.000001, golden: true}, // 118 |
| 1146 |
{in: `-1e-7`, ptr: new(float64), out: -1e-7, golden: true}, // 119 |
| 1147 |
{in: `-100000000000000000000`, ptr: new(float64), out: -100000000000000000000.0, golden: true}, // 120 |
| 1148 |
{in: `-1e+21`, ptr: new(float64), out: -1e21, golden: true}, // 121 |
| 1149 |
{in: `999999999999999900000`, ptr: new(float64), out: 999999999999999900000.0, golden: true}, // 122 |
| 1150 |
{in: `9007199254740992`, ptr: new(float64), out: 9007199254740992.0, golden: true}, // 123 |
| 1151 |
{in: `9007199254740993`, ptr: new(float64), out: 9007199254740992.0, golden: false}, // 124 |
| 1152 |
{ |
| 1153 |
in: `{"V": {"F2": "hello"}}`, // 125 |
| 1154 |
ptr: new(VOuter), |
| 1155 |
err: &json.UnmarshalTypeError{ |
| 1156 |
Value: `number "`, |
| 1157 |
Struct: "V", |
| 1158 |
Field: "F2", |
| 1159 |
Type: reflect.TypeOf(int32(0)), |
| 1160 |
Offset: 20, |
| 1161 |
}, |
| 1162 |
}, |
| 1163 |
{ |
| 1164 |
in: `{"V": {"F4": {}, "F2": "hello"}}`, // 126 |
| 1165 |
ptr: new(VOuter), |
| 1166 |
err: &json.UnmarshalTypeError{ |
| 1167 |
Value: `number "`, |
| 1168 |
Struct: "V", |
| 1169 |
Field: "F2", |
| 1170 |
Type: reflect.TypeOf(int32(0)), |
| 1171 |
Offset: 30, |
| 1172 |
}, |
| 1173 |
}, |
| 1174 |
// issue 15146. |
| 1175 |
// invalid inputs in wrongStringTests below. |
| 1176 |
{in: `{"B":"true"}`, ptr: new(B), out: B{true}, golden: true}, // 127 |
| 1177 |
{in: `{"B":"false"}`, ptr: new(B), out: B{false}, golden: true}, // 128 |
| 1178 |
{in: `{"B": "maybe"}`, ptr: new(B), err: errors.New(`json: bool unexpected end of JSON input`)}, // 129 |
| 1179 |
{in: `{"B": "tru"}`, ptr: new(B), err: errors.New(`json: invalid character as true`)}, // 130 |
| 1180 |
{in: `{"B": "False"}`, ptr: new(B), err: errors.New(`json: bool unexpected end of JSON input`)}, // 131 |
| 1181 |
{in: `{"B": "null"}`, ptr: new(B), out: B{false}}, // 132 |
| 1182 |
{in: `{"B": "nul"}`, ptr: new(B), err: errors.New(`json: invalid character as null`)}, // 133 |
| 1183 |
{in: `{"B": [2, 3]}`, ptr: new(B), err: errors.New(`json: cannot unmarshal array into Go struct field B.B of type string`)}, // 134 |
| 1184 |
// additional tests for disallowUnknownFields |
| 1185 |
{ // 135 |
| 1186 |
in: `{ |
| 1187 |
"Level0": 1, |
| 1188 |
"Level1b": 2, |
| 1189 |
"Level1c": 3, |
| 1190 |
"x": 4, |
| 1191 |
"Level1a": 5, |
| 1192 |
"LEVEL1B": 6, |
| 1193 |
"e": { |
| 1194 |
"Level1a": 8, |
| 1195 |
"Level1b": 9, |
| 1196 |
"Level1c": 10, |
| 1197 |
"Level1d": 11, |
| 1198 |
"x": 12 |
| 1199 |
}, |
| 1200 |
"Loop1": 13, |
| 1201 |
"Loop2": 14, |
| 1202 |
"X": 15, |
| 1203 |
"Y": 16, |
| 1204 |
"Z": 17, |
| 1205 |
"Q": 18, |
| 1206 |
"extra": true |
| 1207 |
}`, |
| 1208 |
ptr: new(Top), |
| 1209 |
err: fmt.Errorf("json: unknown field \"extra\""), |
| 1210 |
disallowUnknownFields: true, |
| 1211 |
}, |
| 1212 |
{ // 136 |
| 1213 |
in: `{ |
| 1214 |
"Level0": 1, |
| 1215 |
"Level1b": 2, |
| 1216 |
"Level1c": 3, |
| 1217 |
"x": 4, |
| 1218 |
"Level1a": 5, |
| 1219 |
"LEVEL1B": 6, |
| 1220 |
"e": { |
| 1221 |
"Level1a": 8, |
| 1222 |
"Level1b": 9, |
| 1223 |
"Level1c": 10, |
| 1224 |
"Level1d": 11, |
| 1225 |
"x": 12, |
| 1226 |
"extra": null |
| 1227 |
}, |
| 1228 |
"Loop1": 13, |
| 1229 |
"Loop2": 14, |
| 1230 |
"X": 15, |
| 1231 |
"Y": 16, |
| 1232 |
"Z": 17, |
| 1233 |
"Q": 18 |
| 1234 |
}`, |
| 1235 |
ptr: new(Top), |
| 1236 |
err: fmt.Errorf("json: unknown field \"extra\""), |
| 1237 |
disallowUnknownFields: true, |
| 1238 |
}, |
| 1239 |
// issue 26444 |
| 1240 |
// UnmarshalTypeError without field & struct values |
| 1241 |
{ |
| 1242 |
in: `{"data":{"test1": "bob", "test2": 123}}`, // 137 |
| 1243 |
ptr: new(mapStringToStringData), |
| 1244 |
err: &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 37, Struct: "mapStringToStringData", Field: "Data"}, |
| 1245 |
}, |
| 1246 |
{ |
| 1247 |
in: `{"data":{"test1": 123, "test2": "bob"}}`, // 138 |
| 1248 |
ptr: new(mapStringToStringData), |
| 1249 |
err: &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 21, Struct: "mapStringToStringData", Field: "Data"}, |
| 1250 |
}, |
| 1251 |
|
| 1252 |
// trying to decode JSON arrays or objects via TextUnmarshaler |
| 1253 |
{ |
| 1254 |
in: `[1, 2, 3]`, // 139 |
| 1255 |
ptr: new(MustNotUnmarshalText), |
| 1256 |
err: &json.UnmarshalTypeError{Value: "array", Type: reflect.TypeOf(&MustNotUnmarshalText{}), Offset: 1}, |
| 1257 |
}, |
| 1258 |
{ |
| 1259 |
in: `{"foo": "bar"}`, // 140 |
| 1260 |
ptr: new(MustNotUnmarshalText), |
| 1261 |
err: &json.UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(&MustNotUnmarshalText{}), Offset: 1}, |
| 1262 |
}, |
| 1263 |
// #22369 |
| 1264 |
{ |
| 1265 |
in: `{"PP": {"T": {"Y": "bad-type"}}}`, // 141 |
| 1266 |
ptr: new(P), |
| 1267 |
err: &json.UnmarshalTypeError{ |
| 1268 |
Value: `number "`, |
| 1269 |
Struct: "T", |
| 1270 |
Field: "Y", |
| 1271 |
Type: reflect.TypeOf(int(0)), |
| 1272 |
Offset: 29, |
| 1273 |
}, |
| 1274 |
}, |
| 1275 |
{ |
| 1276 |
in: `{"Ts": [{"Y": 1}, {"Y": 2}, {"Y": "bad-type"}]}`, // 142 |
| 1277 |
ptr: new(PP), |
| 1278 |
err: &json.UnmarshalTypeError{ |
| 1279 |
Value: `number "`, |
| 1280 |
Struct: "T", |
| 1281 |
Field: "Y", |
| 1282 |
Type: reflect.TypeOf(int(0)), |
| 1283 |
Offset: 29, |
| 1284 |
}, |
| 1285 |
}, |
| 1286 |
// #14702 |
| 1287 |
{ |
| 1288 |
in: `invalid`, // 143 |
| 1289 |
ptr: new(json.Number), |
| 1290 |
err: json.NewSyntaxError( |
| 1291 |
`invalid character 'i' looking for beginning of value`, |
| 1292 |
1, |
| 1293 |
), |
| 1294 |
}, |
| 1295 |
{ |
| 1296 |
in: `"invalid"`, // 144 |
| 1297 |
ptr: new(json.Number), |
| 1298 |
err: fmt.Errorf(`strconv.ParseFloat: parsing "invalid": invalid syntax`), |
| 1299 |
}, |
| 1300 |
{ |
| 1301 |
in: `{"A":"invalid"}`, // 145 |
| 1302 |
ptr: new(struct{ A json.Number }), |
| 1303 |
err: fmt.Errorf(`strconv.ParseFloat: parsing "invalid": invalid syntax`), |
| 1304 |
}, |
| 1305 |
{ |
| 1306 |
in: `{"A":"invalid"}`, // 146 |
| 1307 |
ptr: new(struct { |
| 1308 |
A json.Number `json:",string"` |
| 1309 |
}), |
| 1310 |
err: fmt.Errorf(`invalid character 'i' looking for beginning of value`), |
| 1311 |
}, |
| 1312 |
{ |
| 1313 |
in: `{"A":"invalid"}`, // 147 |
| 1314 |
ptr: new(map[string]json.Number), |
| 1315 |
err: fmt.Errorf(`strconv.ParseFloat: parsing "invalid": invalid syntax`), |
| 1316 |
}, |
| 1317 |
// invalid UTF-8 is coerced to valid UTF-8. |
| 1318 |
{ |
| 1319 |
in: "\"hello\xffworld\"", // 148 |
| 1320 |
ptr: new(string), |
| 1321 |
out: "hello\ufffdworld", |
| 1322 |
}, |
| 1323 |
{ |
| 1324 |
in: "\"hello\xc2\xc2world\"", // 149 |
| 1325 |
ptr: new(string), |
| 1326 |
out: "hello\ufffd\ufffdworld", |
| 1327 |
}, |
| 1328 |
{ |
| 1329 |
in: "\"hello\xc2\xffworld\"", // 150 |
| 1330 |
ptr: new(string), |
| 1331 |
out: "hello\ufffd\ufffdworld", |
| 1332 |
}, |
| 1333 |
{ |
| 1334 |
in: "\"hello\xed\xa0\x80\xed\xb0\x80world\"", // 151 |
| 1335 |
ptr: new(string), |
| 1336 |
out: "hello\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdworld", |
| 1337 |
}, |
| 1338 |
{in: "-128", ptr: new(int8), out: int8(-128)}, |
| 1339 |
{in: "127", ptr: new(int8), out: int8(127)}, |
| 1340 |
{in: "-32768", ptr: new(int16), out: int16(-32768)}, |
| 1341 |
{in: "32767", ptr: new(int16), out: int16(32767)}, |
| 1342 |
{in: "-2147483648", ptr: new(int32), out: int32(-2147483648)}, |
| 1343 |
{in: "2147483647", ptr: new(int32), out: int32(2147483647)}, |
| 1344 |
} |
| 1345 |
|
| 1346 |
type All struct { |
| 1347 |
Bool bool |
| 1348 |
Int int |
| 1349 |
Int8 int8 |
| 1350 |
Int16 int16 |
| 1351 |
Int32 int32 |
| 1352 |
Int64 int64 |
| 1353 |
Uint uint |
| 1354 |
Uint8 uint8 |
| 1355 |
Uint16 uint16 |
| 1356 |
Uint32 uint32 |
| 1357 |
Uint64 uint64 |
| 1358 |
Uintptr uintptr |
| 1359 |
Float32 float32 |
| 1360 |
Float64 float64 |
| 1361 |
|
| 1362 |
Foo string `json:"bar"` |
| 1363 |
Foo2 string `json:"bar2,dummyopt"` |
| 1364 |
|
| 1365 |
IntStr int64 `json:",string"` |
| 1366 |
UintptrStr uintptr `json:",string"` |
| 1367 |
|
| 1368 |
PBool *bool |
| 1369 |
PInt *int |
| 1370 |
PInt8 *int8 |
| 1371 |
PInt16 *int16 |
| 1372 |
PInt32 *int32 |
| 1373 |
PInt64 *int64 |
| 1374 |
PUint *uint |
| 1375 |
PUint8 *uint8 |
| 1376 |
PUint16 *uint16 |
| 1377 |
PUint32 *uint32 |
| 1378 |
PUint64 *uint64 |
| 1379 |
PUintptr *uintptr |
| 1380 |
PFloat32 *float32 |
| 1381 |
PFloat64 *float64 |
| 1382 |
|
| 1383 |
String string |
| 1384 |
PString *string |
| 1385 |
|
| 1386 |
Map map[string]Small |
| 1387 |
MapP map[string]*Small |
| 1388 |
PMap *map[string]Small |
| 1389 |
PMapP *map[string]*Small |
| 1390 |
|
| 1391 |
EmptyMap map[string]Small |
| 1392 |
NilMap map[string]Small |
| 1393 |
|
| 1394 |
Slice []Small |
| 1395 |
SliceP []*Small |
| 1396 |
PSlice *[]Small |
| 1397 |
PSliceP *[]*Small |
| 1398 |
|
| 1399 |
EmptySlice []Small |
| 1400 |
NilSlice []Small |
| 1401 |
|
| 1402 |
StringSlice []string |
| 1403 |
ByteSlice []byte |
| 1404 |
|
| 1405 |
Small Small |
| 1406 |
PSmall *Small |
| 1407 |
PPSmall **Small |
| 1408 |
|
| 1409 |
Interface any |
| 1410 |
PInterface *any |
| 1411 |
|
| 1412 |
unexported int |
| 1413 |
} |
| 1414 |
|
| 1415 |
type Small struct { |
| 1416 |
Tag string |
| 1417 |
} |
| 1418 |
|
| 1419 |
var allValue = All{ |
| 1420 |
Bool: true, |
| 1421 |
Int: 2, |
| 1422 |
Int8: 3, |
| 1423 |
Int16: 4, |
| 1424 |
Int32: 5, |
| 1425 |
Int64: 6, |
| 1426 |
Uint: 7, |
| 1427 |
Uint8: 8, |
| 1428 |
Uint16: 9, |
| 1429 |
Uint32: 10, |
| 1430 |
Uint64: 11, |
| 1431 |
Uintptr: 12, |
| 1432 |
Float32: 14.1, |
| 1433 |
Float64: 15.1, |
| 1434 |
Foo: "foo", |
| 1435 |
Foo2: "foo2", |
| 1436 |
IntStr: 42, |
| 1437 |
UintptrStr: 44, |
| 1438 |
String: "16", |
| 1439 |
Map: map[string]Small{ |
| 1440 |
"17": {Tag: "tag17"}, |
| 1441 |
"18": {Tag: "tag18"}, |
| 1442 |
}, |
| 1443 |
MapP: map[string]*Small{ |
| 1444 |
"19": {Tag: "tag19"}, |
| 1445 |
"20": nil, |
| 1446 |
}, |
| 1447 |
EmptyMap: map[string]Small{}, |
| 1448 |
Slice: []Small{{Tag: "tag20"}, {Tag: "tag21"}}, |
| 1449 |
SliceP: []*Small{{Tag: "tag22"}, nil, {Tag: "tag23"}}, |
| 1450 |
EmptySlice: []Small{}, |
| 1451 |
StringSlice: []string{"str24", "str25", "str26"}, |
| 1452 |
ByteSlice: []byte{27, 28, 29}, |
| 1453 |
Small: Small{Tag: "tag30"}, |
| 1454 |
PSmall: &Small{Tag: "tag31"}, |
| 1455 |
Interface: 5.2, |
| 1456 |
} |
| 1457 |
|
| 1458 |
var pallValue = All{ |
| 1459 |
PBool: &allValue.Bool, |
| 1460 |
PInt: &allValue.Int, |
| 1461 |
PInt8: &allValue.Int8, |
| 1462 |
PInt16: &allValue.Int16, |
| 1463 |
PInt32: &allValue.Int32, |
| 1464 |
PInt64: &allValue.Int64, |
| 1465 |
PUint: &allValue.Uint, |
| 1466 |
PUint8: &allValue.Uint8, |
| 1467 |
PUint16: &allValue.Uint16, |
| 1468 |
PUint32: &allValue.Uint32, |
| 1469 |
PUint64: &allValue.Uint64, |
| 1470 |
PUintptr: &allValue.Uintptr, |
| 1471 |
PFloat32: &allValue.Float32, |
| 1472 |
PFloat64: &allValue.Float64, |
| 1473 |
PString: &allValue.String, |
| 1474 |
PMap: &allValue.Map, |
| 1475 |
PMapP: &allValue.MapP, |
| 1476 |
PSlice: &allValue.Slice, |
| 1477 |
PSliceP: &allValue.SliceP, |
| 1478 |
PPSmall: &allValue.PSmall, |
| 1479 |
PInterface: &allValue.Interface, |
| 1480 |
} |
| 1481 |
|
| 1482 |
var allValueIndent = `{ |
| 1483 |
"Bool": true, |
| 1484 |
"Int": 2, |
| 1485 |
"Int8": 3, |
| 1486 |
"Int16": 4, |
| 1487 |
"Int32": 5, |
| 1488 |
"Int64": 6, |
| 1489 |
"Uint": 7, |
| 1490 |
"Uint8": 8, |
| 1491 |
"Uint16": 9, |
| 1492 |
"Uint32": 10, |
| 1493 |
"Uint64": 11, |
| 1494 |
"Uintptr": 12, |
| 1495 |
"Float32": 14.1, |
| 1496 |
"Float64": 15.1, |
| 1497 |
"bar": "foo", |
| 1498 |
"bar2": "foo2", |
| 1499 |
"IntStr": "42", |
| 1500 |
"UintptrStr": "44", |
| 1501 |
"PBool": null, |
| 1502 |
"PInt": null, |
| 1503 |
"PInt8": null, |
| 1504 |
"PInt16": null, |
| 1505 |
"PInt32": null, |
| 1506 |
"PInt64": null, |
| 1507 |
"PUint": null, |
| 1508 |
"PUint8": null, |
| 1509 |
"PUint16": null, |
| 1510 |
"PUint32": null, |
| 1511 |
"PUint64": null, |
| 1512 |
"PUintptr": null, |
| 1513 |
"PFloat32": null, |
| 1514 |
"PFloat64": null, |
| 1515 |
"String": "16", |
| 1516 |
"PString": null, |
| 1517 |
"Map": { |
| 1518 |
"17": { |
| 1519 |
"Tag": "tag17" |
| 1520 |
}, |
| 1521 |
"18": { |
| 1522 |
"Tag": "tag18" |
| 1523 |
} |
| 1524 |
}, |
| 1525 |
"MapP": { |
| 1526 |
"19": { |
| 1527 |
"Tag": "tag19" |
| 1528 |
}, |
| 1529 |
"20": null |
| 1530 |
}, |
| 1531 |
"PMap": null, |
| 1532 |
"PMapP": null, |
| 1533 |
"EmptyMap": {}, |
| 1534 |
"NilMap": null, |
| 1535 |
"Slice": [ |
| 1536 |
{ |
| 1537 |
"Tag": "tag20" |
| 1538 |
}, |
| 1539 |
{ |
| 1540 |
"Tag": "tag21" |
| 1541 |
} |
| 1542 |
], |
| 1543 |
"SliceP": [ |
| 1544 |
{ |
| 1545 |
"Tag": "tag22" |
| 1546 |
}, |
| 1547 |
null, |
| 1548 |
{ |
| 1549 |
"Tag": "tag23" |
| 1550 |
} |
| 1551 |
], |
| 1552 |
"PSlice": null, |
| 1553 |
"PSliceP": null, |
| 1554 |
"EmptySlice": [], |
| 1555 |
"NilSlice": null, |
| 1556 |
"StringSlice": [ |
| 1557 |
"str24", |
| 1558 |
"str25", |
| 1559 |
"str26" |
| 1560 |
], |
| 1561 |
"ByteSlice": "Gxwd", |
| 1562 |
"Small": { |
| 1563 |
"Tag": "tag30" |
| 1564 |
}, |
| 1565 |
"PSmall": { |
| 1566 |
"Tag": "tag31" |
| 1567 |
}, |
| 1568 |
"PPSmall": null, |
| 1569 |
"Interface": 5.2, |
| 1570 |
"PInterface": null |
| 1571 |
}` |
| 1572 |
|
| 1573 |
var allValueCompact = strings.Map(noSpace, allValueIndent) |
| 1574 |
|
| 1575 |
var pallValueIndent = `{ |
| 1576 |
"Bool": false, |
| 1577 |
"Int": 0, |
| 1578 |
"Int8": 0, |
| 1579 |
"Int16": 0, |
| 1580 |
"Int32": 0, |
| 1581 |
"Int64": 0, |
| 1582 |
"Uint": 0, |
| 1583 |
"Uint8": 0, |
| 1584 |
"Uint16": 0, |
| 1585 |
"Uint32": 0, |
| 1586 |
"Uint64": 0, |
| 1587 |
"Uintptr": 0, |
| 1588 |
"Float32": 0, |
| 1589 |
"Float64": 0, |
| 1590 |
"bar": "", |
| 1591 |
"bar2": "", |
| 1592 |
"IntStr": "0", |
| 1593 |
"UintptrStr": "0", |
| 1594 |
"PBool": true, |
| 1595 |
"PInt": 2, |
| 1596 |
"PInt8": 3, |
| 1597 |
"PInt16": 4, |
| 1598 |
"PInt32": 5, |
| 1599 |
"PInt64": 6, |
| 1600 |
"PUint": 7, |
| 1601 |
"PUint8": 8, |
| 1602 |
"PUint16": 9, |
| 1603 |
"PUint32": 10, |
| 1604 |
"PUint64": 11, |
| 1605 |
"PUintptr": 12, |
| 1606 |
"PFloat32": 14.1, |
| 1607 |
"PFloat64": 15.1, |
| 1608 |
"String": "", |
| 1609 |
"PString": "16", |
| 1610 |
"Map": null, |
| 1611 |
"MapP": null, |
| 1612 |
"PMap": { |
| 1613 |
"17": { |
| 1614 |
"Tag": "tag17" |
| 1615 |
}, |
| 1616 |
"18": { |
| 1617 |
"Tag": "tag18" |
| 1618 |
} |
| 1619 |
}, |
| 1620 |
"PMapP": { |
| 1621 |
"19": { |
| 1622 |
"Tag": "tag19" |
| 1623 |
}, |
| 1624 |
"20": null |
| 1625 |
}, |
| 1626 |
"EmptyMap": null, |
| 1627 |
"NilMap": null, |
| 1628 |
"Slice": null, |
| 1629 |
"SliceP": null, |
| 1630 |
"PSlice": [ |
| 1631 |
{ |
| 1632 |
"Tag": "tag20" |
| 1633 |
}, |
| 1634 |
{ |
| 1635 |
"Tag": "tag21" |
| 1636 |
} |
| 1637 |
], |
| 1638 |
"PSliceP": [ |
| 1639 |
{ |
| 1640 |
"Tag": "tag22" |
| 1641 |
}, |
| 1642 |
null, |
| 1643 |
{ |
| 1644 |
"Tag": "tag23" |
| 1645 |
} |
| 1646 |
], |
| 1647 |
"EmptySlice": null, |
| 1648 |
"NilSlice": null, |
| 1649 |
"StringSlice": null, |
| 1650 |
"ByteSlice": null, |
| 1651 |
"Small": { |
| 1652 |
"Tag": "" |
| 1653 |
}, |
| 1654 |
"PSmall": null, |
| 1655 |
"PPSmall": { |
| 1656 |
"Tag": "tag31" |
| 1657 |
}, |
| 1658 |
"Interface": null, |
| 1659 |
"PInterface": 5.2 |
| 1660 |
}` |
| 1661 |
|
| 1662 |
var pallValueCompact = strings.Map(noSpace, pallValueIndent) |
| 1663 |
|
| 1664 |
type NullTest struct { |
| 1665 |
Bool bool |
| 1666 |
Int int |
| 1667 |
Int8 int8 |
| 1668 |
Int16 int16 |
| 1669 |
Int32 int32 |
| 1670 |
Int64 int64 |
| 1671 |
Uint uint |
| 1672 |
Uint8 uint8 |
| 1673 |
Uint16 uint16 |
| 1674 |
Uint32 uint32 |
| 1675 |
Uint64 uint64 |
| 1676 |
Float32 float32 |
| 1677 |
Float64 float64 |
| 1678 |
String string |
| 1679 |
PBool *bool |
| 1680 |
Map map[string]string |
| 1681 |
Slice []string |
| 1682 |
Interface any |
| 1683 |
|
| 1684 |
PRaw *json.RawMessage |
| 1685 |
PTime *time.Time |
| 1686 |
PBigInt *big.Int |
| 1687 |
PText *MustNotUnmarshalText |
| 1688 |
PBuffer *bytes.Buffer // has methods, just not relevant ones |
| 1689 |
PStruct *struct{} |
| 1690 |
|
| 1691 |
Raw json.RawMessage |
| 1692 |
Time time.Time |
| 1693 |
BigInt big.Int |
| 1694 |
Text MustNotUnmarshalText |
| 1695 |
Buffer bytes.Buffer |
| 1696 |
Struct struct{} |
| 1697 |
} |
| 1698 |
|
| 1699 |
type MustNotUnmarshalJSON struct{} |
| 1700 |
|
| 1701 |
func (x MustNotUnmarshalJSON) UnmarshalJSON(data []byte) error { |
| 1702 |
return errors.New("MustNotUnmarshalJSON was used") |
| 1703 |
} |
| 1704 |
|
| 1705 |
type MustNotUnmarshalText struct{} |
| 1706 |
|
| 1707 |
func (x MustNotUnmarshalText) UnmarshalText(text []byte) error { |
| 1708 |
return errors.New("MustNotUnmarshalText was used") |
| 1709 |
} |
| 1710 |
|
| 1711 |
func isSpace(c byte) bool { |
| 1712 |
return c <= ' ' && (c == ' ' || c == '\t' || c == '\r' || c == '\n') |
| 1713 |
} |
| 1714 |
|
| 1715 |
func noSpace(c rune) rune { |
| 1716 |
if isSpace(byte(c)) { //only used for ascii |
| 1717 |
return -1 |
| 1718 |
} |
| 1719 |
return c |
| 1720 |
} |
| 1721 |
|
| 1722 |
// badUTF8 are strings which are not valid UTF-8. Every byte of an invalid sequence is replaced by U+FFFD, which |
| 1723 |
// encoding/json writes escaped before Go 1.27 and as it is by Go 1.27: they are encoded as encoding/json of the |
| 1724 |
// running Go does. |
| 1725 |
var badUTF8 = []string{ |
| 1726 |
"hello\xffworld", |
| 1727 |
"", |
| 1728 |
"\xff", |
| 1729 |
"\xff\xff", |
| 1730 |
"a\xffb", |
| 1731 |
"\xe6\x97\xa5\xe6\x9c\xac\xff\xaa\x9e", |
| 1732 |
} |
| 1733 |
|
| 1734 |
func TestMarshalAllValue(t *testing.T) { |
| 1735 |
b, err := json.Marshal(allValue) |
| 1736 |
if err != nil { |
| 1737 |
t.Fatalf("Marshal allValue: %v", err) |
| 1738 |
} |
| 1739 |
if string(b) != allValueCompact { |
| 1740 |
t.Errorf("Marshal allValueCompact") |
| 1741 |
diff(t, b, []byte(allValueCompact)) |
| 1742 |
return |
| 1743 |
} |
| 1744 |
|
| 1745 |
b, err = json.Marshal(pallValue) |
| 1746 |
if err != nil { |
| 1747 |
t.Fatalf("Marshal pallValue: %v", err) |
| 1748 |
} |
| 1749 |
if string(b) != pallValueCompact { |
| 1750 |
t.Errorf("Marshal pallValueCompact") |
| 1751 |
diff(t, b, []byte(pallValueCompact)) |
| 1752 |
return |
| 1753 |
} |
| 1754 |
} |
| 1755 |
|
| 1756 |
func TestMarshalBadUTF8(t *testing.T) { |
| 1757 |
for _, in := range badUTF8 { |
| 1758 |
want, err := stdjson.Marshal(in) |
| 1759 |
if err != nil { |
| 1760 |
t.Fatal(err) |
| 1761 |
} |
| 1762 |
b, err := json.Marshal(in) |
| 1763 |
if string(b) != string(want) || err != nil { |
| 1764 |
t.Errorf("Marshal(%q) = %#q, %v, want %#q, nil", in, b, err, want) |
| 1765 |
} |
| 1766 |
} |
| 1767 |
} |
| 1768 |
|
| 1769 |
func TestMarshalNumberZeroVal(t *testing.T) { |
| 1770 |
var n json.Number |
| 1771 |
out, err := json.Marshal(n) |
| 1772 |
if err != nil { |
| 1773 |
t.Fatal(err) |
| 1774 |
} |
| 1775 |
outStr := string(out) |
| 1776 |
if outStr != "0" { |
| 1777 |
t.Fatalf("Invalid zero val for Number: %q", outStr) |
| 1778 |
} |
| 1779 |
} |
| 1780 |
|
| 1781 |
func TestMarshalEmbeds(t *testing.T) { |
| 1782 |
top := &Top{ |
| 1783 |
Level0: 1, |
| 1784 |
Embed0: Embed0{ |
| 1785 |
Level1b: 2, |
| 1786 |
Level1c: 3, |
| 1787 |
}, |
| 1788 |
Embed0a: &Embed0a{ |
| 1789 |
Level1a: 5, |
| 1790 |
Level1b: 6, |
| 1791 |
}, |
| 1792 |
Embed0b: &Embed0b{ |
| 1793 |
Level1a: 8, |
| 1794 |
Level1b: 9, |
| 1795 |
Level1c: 10, |
| 1796 |
Level1d: 11, |
| 1797 |
Level1e: 12, |
| 1798 |
}, |
| 1799 |
Loop: Loop{ |
| 1800 |
Loop1: 13, |
| 1801 |
Loop2: 14, |
| 1802 |
}, |
| 1803 |
Embed0p: Embed0p{ |
| 1804 |
Point: image.Point{X: 15, Y: 16}, |
| 1805 |
}, |
| 1806 |
Embed0q: Embed0q{ |
| 1807 |
Point: Point{Z: 17}, |
| 1808 |
}, |
| 1809 |
embed: embed{ |
| 1810 |
Q: 18, |
| 1811 |
}, |
| 1812 |
} |
| 1813 |
b, err := json.Marshal(top) |
| 1814 |
if err != nil { |
| 1815 |
t.Fatal(err) |
| 1816 |
} |
| 1817 |
want := "{\"Level0\":1,\"Level1b\":2,\"Level1c\":3,\"Level1a\":5,\"LEVEL1B\":6,\"e\":{\"Level1a\":8,\"Level1b\":9,\"Level1c\":10,\"Level1d\":11,\"x\":12},\"Loop1\":13,\"Loop2\":14,\"X\":15,\"Y\":16,\"Z\":17,\"Q\":18}" |
| 1818 |
if string(b) != want { |
| 1819 |
t.Errorf("Wrong marshal result.\n got: %q\nwant: %q", b, want) |
| 1820 |
} |
| 1821 |
} |
| 1822 |
|
| 1823 |
func equalError(a, b error) bool { |
| 1824 |
if a == nil { |
| 1825 |
return b == nil |
| 1826 |
} |
| 1827 |
if b == nil { |
| 1828 |
return a == nil |
| 1829 |
} |
| 1830 |
return a.Error() == b.Error() |
| 1831 |
} |
| 1832 |
|
| 1833 |
func TestUnmarshal(t *testing.T) { |
| 1834 |
for i, tt := range unmarshalTests { |
| 1835 |
t.Run(fmt.Sprintf("%d_%q", i, tt.in), func(t *testing.T) { |
| 1836 |
in := []byte(tt.in) |
| 1837 |
if tt.ptr == nil { |
| 1838 |
return |
| 1839 |
} |
| 1840 |
|
| 1841 |
typ := reflect.TypeOf(tt.ptr) |
| 1842 |
if typ.Kind() != reflect.Ptr { |
| 1843 |
t.Errorf("#%d: unmarshalTest.ptr %T is not a pointer type", i, tt.ptr) |
| 1844 |
return |
| 1845 |
} |
| 1846 |
typ = typ.Elem() |
| 1847 |
|
| 1848 |
// v = new(right-type) |
| 1849 |
v := reflect.New(typ) |
| 1850 |
|
| 1851 |
if !reflect.DeepEqual(tt.ptr, v.Interface()) { |
| 1852 |
// There's no reason for ptr to point to non-zero data, |
| 1853 |
// as we decode into new(right-type), so the data is |
| 1854 |
// discarded. |
| 1855 |
// This can easily mean tests that silently don't test |
| 1856 |
// what they should. To test decoding into existing |
| 1857 |
// data, see TestPrefilled. |
| 1858 |
t.Errorf("#%d: unmarshalTest.ptr %#v is not a pointer to a zero value", i, tt.ptr) |
| 1859 |
return |
| 1860 |
} |
| 1861 |
|
| 1862 |
dec := json.NewDecoder(bytes.NewReader(in)) |
| 1863 |
if tt.useNumber { |
| 1864 |
dec.UseNumber() |
| 1865 |
} |
| 1866 |
if tt.disallowUnknownFields { |
| 1867 |
dec.DisallowUnknownFields() |
| 1868 |
} |
| 1869 |
// An error which is not a syntax error is the one of encoding/json of the running Go, whose messages |
| 1870 |
// differ by its version; a syntax error is the one of the test. |
| 1871 |
wantErr := tt.err |
| 1872 |
if e, ok := wantErr.(*json.UnmarshalTypeError); ok && methodErrorsAsIs && e.Type == reflect.TypeOf(SS("")) { |
| 1873 |
// the type error which the method of SS returns, as it is |
| 1874 |
wantErr = &json.UnmarshalTypeError{Value: e.Value, Type: e.Type} |
| 1875 |
} |
| 1876 |
stdDec := stdjson.NewDecoder(bytes.NewReader(in)) |
| 1877 |
if tt.useNumber { |
| 1878 |
stdDec.UseNumber() |
| 1879 |
} |
| 1880 |
if tt.disallowUnknownFields { |
| 1881 |
stdDec.DisallowUnknownFields() |
| 1882 |
} |
| 1883 |
var syntaxErr *stdjson.SyntaxError |
| 1884 |
var ownErr *json.UnmarshalTypeError |
| 1885 |
if err := stdDec.Decode(reflect.New(typ).Interface()); err != nil && !errors.As(err, &syntaxErr) && !errors.As(err, &ownErr) { |
| 1886 |
// an error of go-json which a method returns is not the one of encoding/json |
| 1887 |
wantErr = err |
| 1888 |
} |
| 1889 |
if err := dec.Decode(v.Interface()); !equalError(err, wantErr) { |
| 1890 |
t.Errorf("#%d: %v, want %v", i, err, wantErr) |
| 1891 |
return |
| 1892 |
} else if err != nil { |
| 1893 |
return |
| 1894 |
} |
| 1895 |
if !reflect.DeepEqual(v.Elem().Interface(), tt.out) { |
| 1896 |
t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), tt.out) |
| 1897 |
data, _ := json.Marshal(v.Elem().Interface()) |
| 1898 |
println(string(data)) |
| 1899 |
data, _ = json.Marshal(tt.out) |
| 1900 |
println(string(data)) |
| 1901 |
return |
| 1902 |
} |
| 1903 |
|
| 1904 |
// Check round trip also decodes correctly. |
| 1905 |
if tt.err == nil { |
| 1906 |
enc, err := json.Marshal(v.Interface()) |
| 1907 |
if err != nil { |
| 1908 |
t.Errorf("#%d: error re-marshaling: %v", i, err) |
| 1909 |
return |
| 1910 |
} |
| 1911 |
if tt.golden && !bytes.Equal(enc, in) { |
| 1912 |
t.Errorf("#%d: remarshal mismatch:\nhave: %s\nwant: %s", i, enc, in) |
| 1913 |
} |
| 1914 |
vv := reflect.New(reflect.TypeOf(tt.ptr).Elem()) |
| 1915 |
dec = json.NewDecoder(bytes.NewReader(enc)) |
| 1916 |
if tt.useNumber { |
| 1917 |
dec.UseNumber() |
| 1918 |
} |
| 1919 |
if err := dec.Decode(vv.Interface()); err != nil { |
| 1920 |
t.Errorf("#%d: error re-unmarshaling %#q: %v", i, enc, err) |
| 1921 |
return |
| 1922 |
} |
| 1923 |
if !reflect.DeepEqual(v.Elem().Interface(), vv.Elem().Interface()) { |
| 1924 |
t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), vv.Elem().Interface()) |
| 1925 |
t.Errorf(" In: %q", strings.Map(noSpace, string(in))) |
| 1926 |
t.Errorf("Marshal: %q", strings.Map(noSpace, string(enc))) |
| 1927 |
return |
| 1928 |
} |
| 1929 |
} |
| 1930 |
}) |
| 1931 |
} |
| 1932 |
} |
| 1933 |
|
| 1934 |
func TestUnmarshalMarshal(t *testing.T) { |
| 1935 |
initBig() |
| 1936 |
var v any |
| 1937 |
if err := json.Unmarshal(jsonBig, &v); err != nil { |
| 1938 |
t.Fatalf("Unmarshal: %v", err) |
| 1939 |
} |
| 1940 |
b, err := json.Marshal(v) |
| 1941 |
if err != nil { |
| 1942 |
t.Fatalf("Marshal: %v", err) |
| 1943 |
} |
| 1944 |
if !bytes.Equal(jsonBig, b) { |
| 1945 |
t.Errorf("Marshal jsonBig") |
| 1946 |
diff(t, b, jsonBig) |
| 1947 |
return |
| 1948 |
} |
| 1949 |
} |
| 1950 |
|
| 1951 |
var numberTests = []struct { |
| 1952 |
in string |
| 1953 |
i int64 |
| 1954 |
intErr string |
| 1955 |
f float64 |
| 1956 |
floatErr string |
| 1957 |
}{ |
| 1958 |
{in: "-1.23e1", intErr: "strconv.ParseInt: parsing \"-1.23e1\": invalid syntax", f: -1.23e1}, |
| 1959 |
{in: "-12", i: -12, f: -12.0}, |
| 1960 |
{in: "1e1000", intErr: "strconv.ParseInt: parsing \"1e1000\": invalid syntax", floatErr: "strconv.ParseFloat: parsing \"1e1000\": value out of range"}, |
| 1961 |
} |
| 1962 |
|
| 1963 |
// Independent of Decode, basic coverage of the accessors in Number |
| 1964 |
func TestNumberAccessors(t *testing.T) { |
| 1965 |
for _, tt := range numberTests { |
| 1966 |
n := json.Number(tt.in) |
| 1967 |
if s := n.String(); s != tt.in { |
| 1968 |
t.Errorf("Number(%q).String() is %q", tt.in, s) |
| 1969 |
} |
| 1970 |
if i, err := n.Int64(); err == nil && tt.intErr == "" && i != tt.i { |
| 1971 |
t.Errorf("Number(%q).Int64() is %d", tt.in, i) |
| 1972 |
} else if (err == nil && tt.intErr != "") || (err != nil && err.Error() != tt.intErr) { |
| 1973 |
t.Errorf("Number(%q).Int64() wanted error %q but got: %v", tt.in, tt.intErr, err) |
| 1974 |
} |
| 1975 |
if f, err := n.Float64(); err == nil && tt.floatErr == "" && f != tt.f { |
| 1976 |
t.Errorf("Number(%q).Float64() is %g", tt.in, f) |
| 1977 |
} else if (err == nil && tt.floatErr != "") || (err != nil && err.Error() != tt.floatErr) { |
| 1978 |
t.Errorf("Number(%q).Float64() wanted error %q but got: %v", tt.in, tt.floatErr, err) |
| 1979 |
} |
| 1980 |
} |
| 1981 |
} |
| 1982 |
|
| 1983 |
func TestLargeByteSlice(t *testing.T) { |
| 1984 |
s0 := make([]byte, 2000) |
| 1985 |
for i := range s0 { |
| 1986 |
s0[i] = byte(i) |
| 1987 |
} |
| 1988 |
b, err := json.Marshal(s0) |
| 1989 |
if err != nil { |
| 1990 |
t.Fatalf("Marshal: %v", err) |
| 1991 |
} |
| 1992 |
var s1 []byte |
| 1993 |
if err := json.Unmarshal(b, &s1); err != nil { |
| 1994 |
t.Fatalf("Unmarshal: %v", err) |
| 1995 |
} |
| 1996 |
if !bytes.Equal(s0, s1) { |
| 1997 |
t.Errorf("Marshal large byte slice") |
| 1998 |
diff(t, s0, s1) |
| 1999 |
} |
| 2000 |
} |
| 2001 |
|
| 2002 |
type Xint struct { |
| 2003 |
X int |
| 2004 |
} |
| 2005 |
|
| 2006 |
func TestUnmarshalInterface(t *testing.T) { |
| 2007 |
var xint Xint |
| 2008 |
var i any = &xint |
| 2009 |
if err := json.Unmarshal([]byte(`{"X":1}`), &i); err != nil { |
| 2010 |
t.Fatalf("Unmarshal: %v", err) |
| 2011 |
} |
| 2012 |
if xint.X != 1 { |
| 2013 |
t.Fatalf("Did not write to xint") |
| 2014 |
} |
| 2015 |
} |
| 2016 |
|
| 2017 |
func TestUnmarshalPtrPtr(t *testing.T) { |
| 2018 |
var xint Xint |
| 2019 |
pxint := &xint |
| 2020 |
if err := json.Unmarshal([]byte(`{"X":1}`), &pxint); err != nil { |
| 2021 |
t.Fatalf("Unmarshal: %v", err) |
| 2022 |
} |
| 2023 |
if xint.X != 1 { |
| 2024 |
t.Fatalf("Did not write to xint") |
| 2025 |
} |
| 2026 |
} |
| 2027 |
|
| 2028 |
func TestEscape(t *testing.T) { |
| 2029 |
const input = `"foobar"<html>` + " [\u2028 \u2029]" |
| 2030 |
const expected = `"\"foobar\"\u003chtml\u003e [\u2028 \u2029]"` |
| 2031 |
b, err := json.Marshal(input) |
| 2032 |
if err != nil { |
| 2033 |
t.Fatalf("Marshal error: %v", err) |
| 2034 |
} |
| 2035 |
if s := string(b); s != expected { |
| 2036 |
t.Errorf("Encoding of [%s]:\n got [%s]\nwant [%s]", input, s, expected) |
| 2037 |
} |
| 2038 |
} |
| 2039 |
|
| 2040 |
// WrongString is a struct that's misusing the ,string modifier. |
| 2041 |
type WrongString struct { |
| 2042 |
Message string `json:"result,string"` |
| 2043 |
} |
| 2044 |
|
| 2045 |
type wrongStringTest struct { |
| 2046 |
in, err string |
| 2047 |
} |
| 2048 |
|
| 2049 |
var wrongStringTests = []wrongStringTest{ |
| 2050 |
{`{"result":"x"}`, `invalid character 'x' looking for beginning of value`}, |
| 2051 |
{`{"result":"foo"}`, `invalid character 'f' looking for beginning of value`}, |
| 2052 |
{`{"result":"123"}`, `json: cannot unmarshal number into Go struct field WrongString.Message of type string`}, |
| 2053 |
{`{"result":123}`, `json: cannot unmarshal number into Go struct field WrongString.Message of type string`}, |
| 2054 |
{`{"result":"\""}`, `json: string unexpected end of JSON input`}, |
| 2055 |
{`{"result":"\"foo"}`, `json: string unexpected end of JSON input`}, |
| 2056 |
} |
| 2057 |
|
| 2058 |
// If people misuse the ,string modifier, the error message should be |
| 2059 |
// helpful, telling the user that they're doing it wrong. |
| 2060 |
func TestErrorMessageFromMisusedString(t *testing.T) { |
| 2061 |
// The errors of the misuses of the string option are the ones of encoding/json of the running Go. |
| 2062 |
for n, tt := range wrongStringTests { |
| 2063 |
var s, stdS WrongString |
| 2064 |
err := json.NewDecoder(strings.NewReader(tt.in)).Decode(&s) |
| 2065 |
want := stdjson.NewDecoder(strings.NewReader(tt.in)).Decode(&stdS) |
| 2066 |
if fmt.Sprint(err) != fmt.Sprint(want) { |
| 2067 |
t.Errorf("%d. got err = %q, want %q", n, fmt.Sprint(err), fmt.Sprint(want)) |
| 2068 |
} |
| 2069 |
} |
| 2070 |
} |
| 2071 |
|
| 2072 |
func TestRefUnmarshal(t *testing.T) { |
| 2073 |
type S struct { |
| 2074 |
// Ref is defined in encode_test.go. |
| 2075 |
R0 Ref |
| 2076 |
R1 *Ref |
| 2077 |
R2 RefText |
| 2078 |
R3 *RefText |
| 2079 |
} |
| 2080 |
want := S{ |
| 2081 |
R0: 12, |
| 2082 |
R1: new(Ref), |
| 2083 |
R2: 13, |
| 2084 |
R3: new(RefText), |
| 2085 |
} |
| 2086 |
*want.R1 = 12 |
| 2087 |
*want.R3 = 13 |
| 2088 |
|
| 2089 |
var got S |
| 2090 |
if err := json.Unmarshal([]byte(`{"R0":"ref","R1":"ref","R2":"ref","R3":"ref"}`), &got); err != nil { |
| 2091 |
t.Fatalf("Unmarshal: %v", err) |
| 2092 |
} |
| 2093 |
if !reflect.DeepEqual(got, want) { |
| 2094 |
t.Errorf("got %+v, want %+v", got, want) |
| 2095 |
} |
| 2096 |
} |
| 2097 |
|
| 2098 |
// Test that the empty string doesn't panic decoding when ,string is specified |
| 2099 |
// Issue 3450 |
| 2100 |
func TestEmptyString(t *testing.T) { |
| 2101 |
type T2 struct { |
| 2102 |
Number1 int `json:",string"` |
| 2103 |
Number2 int `json:",string"` |
| 2104 |
} |
| 2105 |
data := `{"Number1":"1", "Number2":""}` |
| 2106 |
dec := json.NewDecoder(strings.NewReader(data)) |
| 2107 |
var t2 T2 |
| 2108 |
err := dec.Decode(&t2) |
| 2109 |
if err == nil { |
| 2110 |
t.Fatal("Decode: did not return error") |
| 2111 |
} |
| 2112 |
if t2.Number1 != 1 { |
| 2113 |
t.Fatal("Decode: did not set Number1") |
| 2114 |
} |
| 2115 |
} |
| 2116 |
|
| 2117 |
// Test that a null for ,string is not replaced with the previous quoted string (issue 7046). |
| 2118 |
// It should also not be an error (issue 2540, issue 8587). |
| 2119 |
func TestNullString(t *testing.T) { |
| 2120 |
type T struct { |
| 2121 |
A int `json:",string"` |
| 2122 |
B int `json:",string"` |
| 2123 |
C *int `json:",string"` |
| 2124 |
} |
| 2125 |
data := []byte(`{"A": "1", "B": null, "C": null}`) |
| 2126 |
var s T |
| 2127 |
s.B = 1 |
| 2128 |
s.C = new(int) |
| 2129 |
*s.C = 2 |
| 2130 |
err := json.Unmarshal(data, &s) |
| 2131 |
if err != nil { |
| 2132 |
t.Fatalf("Unmarshal: %v", err) |
| 2133 |
} |
| 2134 |
if s.B != 1 || s.C != nil { |
| 2135 |
t.Fatalf("after Unmarshal, s.B=%d, s.C=%p, want 1, nil", s.B, s.C) |
| 2136 |
} |
| 2137 |
} |
| 2138 |
|
| 2139 |
func intp(x int) *int { |
| 2140 |
p := new(int) |
| 2141 |
*p = x |
| 2142 |
return p |
| 2143 |
} |
| 2144 |
|
| 2145 |
func intpp(x *int) **int { |
| 2146 |
pp := new(*int) |
| 2147 |
*pp = x |
| 2148 |
return pp |
| 2149 |
} |
| 2150 |
|
| 2151 |
var interfaceSetTests = []struct { |
| 2152 |
pre any |
| 2153 |
json string |
| 2154 |
post any |
| 2155 |
}{ |
| 2156 |
{"foo", `"bar"`, "bar"}, |
| 2157 |
{"foo", `2`, 2.0}, |
| 2158 |
{"foo", `true`, true}, |
| 2159 |
{"foo", `null`, nil}, |
| 2160 |
{nil, `null`, nil}, |
| 2161 |
{new(int), `null`, nil}, |
| 2162 |
{(*int)(nil), `null`, nil}, |
| 2163 |
//{new(*int), `null`, new(*int)}, |
| 2164 |
{(**int)(nil), `null`, nil}, |
| 2165 |
{intp(1), `null`, nil}, |
| 2166 |
//{intpp(nil), `null`, intpp(nil)}, |
| 2167 |
//{intpp(intp(1)), `null`, intpp(nil)}, |
| 2168 |
} |
| 2169 |
|
| 2170 |
func TestInterfaceSet(t *testing.T) { |
| 2171 |
for idx, tt := range interfaceSetTests { |
| 2172 |
b := struct{ X any }{tt.pre} |
| 2173 |
blob := `{"X":` + tt.json + `}` |
| 2174 |
if err := json.Unmarshal([]byte(blob), &b); err != nil { |
| 2175 |
t.Errorf("Unmarshal %#q: %v", blob, err) |
| 2176 |
continue |
| 2177 |
} |
| 2178 |
if !reflect.DeepEqual(b.X, tt.post) { |
| 2179 |
t.Errorf("%d: Unmarshal %#q into %#v: X=%#v, want %#v", idx, blob, tt.pre, b.X, tt.post) |
| 2180 |
} |
| 2181 |
} |
| 2182 |
} |
| 2183 |
|
| 2184 |
// JSON null values should be ignored for primitives and string values instead of resulting in an error. |
| 2185 |
// Issue 2540 |
| 2186 |
func TestUnmarshalNulls(t *testing.T) { |
| 2187 |
// Unmarshal docs: |
| 2188 |
// The JSON null value unmarshals into an interface, map, pointer, or slice |
| 2189 |
// by setting that Go value to nil. Because null is often used in JSON to mean |
| 2190 |
// ``not present,'' unmarshaling a JSON null into any other Go type has no effect |
| 2191 |
// on the value and produces no error. |
| 2192 |
|
| 2193 |
jsonData := []byte(`{ |
| 2194 |
"Bool" : null, |
| 2195 |
"Int" : null, |
| 2196 |
"Int8" : null, |
| 2197 |
"Int16" : null, |
| 2198 |
"Int32" : null, |
| 2199 |
"Int64" : null, |
| 2200 |
"Uint" : null, |
| 2201 |
"Uint8" : null, |
| 2202 |
"Uint16" : null, |
| 2203 |
"Uint32" : null, |
| 2204 |
"Uint64" : null, |
| 2205 |
"Float32" : null, |
| 2206 |
"Float64" : null, |
| 2207 |
"String" : null, |
| 2208 |
"PBool": null, |
| 2209 |
"Map": null, |
| 2210 |
"Slice": null, |
| 2211 |
"Interface": null, |
| 2212 |
"PRaw": null, |
| 2213 |
"PTime": null, |
| 2214 |
"PBigInt": null, |
| 2215 |
"PText": null, |
| 2216 |
"PBuffer": null, |
| 2217 |
"PStruct": null, |
| 2218 |
"Raw": null, |
| 2219 |
"Time": null, |
| 2220 |
"BigInt": null, |
| 2221 |
"Text": null, |
| 2222 |
"Buffer": null, |
| 2223 |
"Struct": null |
| 2224 |
}`) |
| 2225 |
nulls := NullTest{ |
| 2226 |
Bool: true, |
| 2227 |
Int: 2, |
| 2228 |
Int8: 3, |
| 2229 |
Int16: 4, |
| 2230 |
Int32: 5, |
| 2231 |
Int64: 6, |
| 2232 |
Uint: 7, |
| 2233 |
Uint8: 8, |
| 2234 |
Uint16: 9, |
| 2235 |
Uint32: 10, |
| 2236 |
Uint64: 11, |
| 2237 |
Float32: 12.1, |
| 2238 |
Float64: 13.1, |
| 2239 |
String: "14", |
| 2240 |
PBool: new(bool), |
| 2241 |
Map: map[string]string{}, |
| 2242 |
Slice: []string{}, |
| 2243 |
Interface: new(MustNotUnmarshalJSON), |
| 2244 |
PRaw: new(json.RawMessage), |
| 2245 |
PTime: new(time.Time), |
| 2246 |
PBigInt: new(big.Int), |
| 2247 |
PText: new(MustNotUnmarshalText), |
| 2248 |
PStruct: new(struct{}), |
| 2249 |
PBuffer: new(bytes.Buffer), |
| 2250 |
Raw: json.RawMessage("123"), |
| 2251 |
Time: time.Unix(123456789, 0), |
| 2252 |
BigInt: *big.NewInt(123), |
| 2253 |
} |
| 2254 |
|
| 2255 |
before := nulls.Time.String() |
| 2256 |
|
| 2257 |
err := json.Unmarshal(jsonData, &nulls) |
| 2258 |
if err != nil { |
| 2259 |
t.Errorf("Unmarshal of null values failed: %v", err) |
| 2260 |
} |
| 2261 |
if !nulls.Bool || nulls.Int != 2 || nulls.Int8 != 3 || nulls.Int16 != 4 || nulls.Int32 != 5 || nulls.Int64 != 6 || |
| 2262 |
nulls.Uint != 7 || nulls.Uint8 != 8 || nulls.Uint16 != 9 || nulls.Uint32 != 10 || nulls.Uint64 != 11 || |
| 2263 |
nulls.Float32 != 12.1 || nulls.Float64 != 13.1 || nulls.String != "14" { |
| 2264 |
t.Errorf("Unmarshal of null values affected primitives") |
| 2265 |
} |
| 2266 |
|
| 2267 |
if nulls.PBool != nil { |
| 2268 |
t.Errorf("Unmarshal of null did not clear nulls.PBool") |
| 2269 |
} |
| 2270 |
if nulls.Map != nil { |
| 2271 |
t.Errorf("Unmarshal of null did not clear nulls.Map") |
| 2272 |
} |
| 2273 |
if nulls.Slice != nil { |
| 2274 |
t.Errorf("Unmarshal of null did not clear nulls.Slice") |
| 2275 |
} |
| 2276 |
if nulls.Interface != nil { |
| 2277 |
t.Errorf("Unmarshal of null did not clear nulls.Interface") |
| 2278 |
} |
| 2279 |
if nulls.PRaw != nil { |
| 2280 |
t.Errorf("Unmarshal of null did not clear nulls.PRaw") |
| 2281 |
} |
| 2282 |
if nulls.PTime != nil { |
| 2283 |
t.Errorf("Unmarshal of null did not clear nulls.PTime") |
| 2284 |
} |
| 2285 |
if nulls.PBigInt != nil { |
| 2286 |
t.Errorf("Unmarshal of null did not clear nulls.PBigInt") |
| 2287 |
} |
| 2288 |
if nulls.PText != nil { |
| 2289 |
t.Errorf("Unmarshal of null did not clear nulls.PText") |
| 2290 |
} |
| 2291 |
if nulls.PBuffer != nil { |
| 2292 |
t.Errorf("Unmarshal of null did not clear nulls.PBuffer") |
| 2293 |
} |
| 2294 |
if nulls.PStruct != nil { |
| 2295 |
t.Errorf("Unmarshal of null did not clear nulls.PStruct") |
| 2296 |
} |
| 2297 |
|
| 2298 |
if string(nulls.Raw) != "null" { |
| 2299 |
t.Errorf("Unmarshal of RawMessage null did not record null: %v", string(nulls.Raw)) |
| 2300 |
} |
| 2301 |
if nulls.Time.String() != before { |
| 2302 |
t.Errorf("Unmarshal of time.Time null set time to %v", nulls.Time.String()) |
| 2303 |
} |
| 2304 |
if nulls.BigInt.String() != "123" { |
| 2305 |
t.Errorf("Unmarshal of big.Int null set int to %v", nulls.BigInt.String()) |
| 2306 |
} |
| 2307 |
} |
| 2308 |
|
| 2309 |
func TestStringKind(t *testing.T) { |
| 2310 |
type stringKind string |
| 2311 |
|
| 2312 |
var m1, m2 map[stringKind]int |
| 2313 |
m1 = map[stringKind]int{ |
| 2314 |
"foo": 42, |
| 2315 |
} |
| 2316 |
|
| 2317 |
data, err := json.Marshal(m1) |
| 2318 |
if err != nil { |
| 2319 |
t.Errorf("Unexpected error marshaling: %v", err) |
| 2320 |
} |
| 2321 |
|
| 2322 |
err = json.Unmarshal(data, &m2) |
| 2323 |
if err != nil { |
| 2324 |
t.Errorf("Unexpected error unmarshaling: %v", err) |
| 2325 |
} |
| 2326 |
|
| 2327 |
if !reflect.DeepEqual(m1, m2) { |
| 2328 |
t.Error("Items should be equal after encoding and then decoding") |
| 2329 |
} |
| 2330 |
} |
| 2331 |
|
| 2332 |
// Custom types with []byte as underlying type could not be marshaled |
| 2333 |
// and then unmarshaled. |
| 2334 |
// Issue 8962. |
| 2335 |
func TestByteKind(t *testing.T) { |
| 2336 |
type byteKind []byte |
| 2337 |
|
| 2338 |
a := byteKind("hello") |
| 2339 |
|
| 2340 |
data, err := json.Marshal(a) |
| 2341 |
if err != nil { |
| 2342 |
t.Error(err) |
| 2343 |
} |
| 2344 |
var b byteKind |
| 2345 |
err = json.Unmarshal(data, &b) |
| 2346 |
if err != nil { |
| 2347 |
t.Fatal(err) |
| 2348 |
} |
| 2349 |
if !reflect.DeepEqual(a, b) { |
| 2350 |
t.Errorf("expected %v == %v", a, b) |
| 2351 |
} |
| 2352 |
} |
| 2353 |
|
| 2354 |
// The fix for issue 8962 introduced a regression. |
| 2355 |
// Issue 12921. |
| 2356 |
func TestSliceOfCustomByte(t *testing.T) { |
| 2357 |
type Uint8 uint8 |
| 2358 |
|
| 2359 |
a := []Uint8("hello") |
| 2360 |
|
| 2361 |
data, err := json.Marshal(a) |
| 2362 |
if err != nil { |
| 2363 |
t.Fatal(err) |
| 2364 |
} |
| 2365 |
var b []Uint8 |
| 2366 |
err = json.Unmarshal(data, &b) |
| 2367 |
if err != nil { |
| 2368 |
t.Fatal(err) |
| 2369 |
} |
| 2370 |
if !reflect.DeepEqual(a, b) { |
| 2371 |
t.Fatalf("expected %v == %v", a, b) |
| 2372 |
} |
| 2373 |
} |
| 2374 |
|
| 2375 |
var decodeTypeErrorTests = []struct { |
| 2376 |
dest any |
| 2377 |
src string |
| 2378 |
}{ |
| 2379 |
{new(string), `{"user": "name"}`}, // issue 4628. |
| 2380 |
{new(error), `{}`}, // issue 4222 |
| 2381 |
{new(error), `[]`}, |
| 2382 |
{new(error), `""`}, |
| 2383 |
{new(error), `123`}, |
| 2384 |
{new(error), `true`}, |
| 2385 |
} |
| 2386 |
|
| 2387 |
func TestUnmarshalTypeError(t *testing.T) { |
| 2388 |
for _, item := range decodeTypeErrorTests { |
| 2389 |
err := json.Unmarshal([]byte(item.src), item.dest) |
| 2390 |
if _, ok := err.(*json.UnmarshalTypeError); !ok { |
| 2391 |
t.Errorf("expected type error for Unmarshal(%q, type %T): got %T", |
| 2392 |
item.src, item.dest, err) |
| 2393 |
} |
| 2394 |
} |
| 2395 |
} |
| 2396 |
|
| 2397 |
var unmarshalSyntaxTests = []string{ |
| 2398 |
"tru", |
| 2399 |
"fals", |
| 2400 |
"nul", |
| 2401 |
"123e", |
| 2402 |
`"hello`, |
| 2403 |
`[1,2,3`, |
| 2404 |
`{"key":1`, |
| 2405 |
`{"key":1,`, |
| 2406 |
} |
| 2407 |
|
| 2408 |
func TestUnmarshalSyntax(t *testing.T) { |
| 2409 |
var x any |
| 2410 |
for _, src := range unmarshalSyntaxTests { |
| 2411 |
err := json.Unmarshal([]byte(src), &x) |
| 2412 |
if _, ok := err.(*json.SyntaxError); !ok { |
| 2413 |
t.Errorf("expected syntax error for Unmarshal(%q): got %T", src, err) |
| 2414 |
} |
| 2415 |
} |
| 2416 |
} |
| 2417 |
|
| 2418 |
// Test handling of unexported fields that should be ignored. |
| 2419 |
// Issue 4660 |
| 2420 |
type unexportedFields struct { |
| 2421 |
Name string |
| 2422 |
m map[string]any `json:"-"` |
| 2423 |
m2 map[string]any `json:"abcd"` |
| 2424 |
|
| 2425 |
s []int `json:"-"` |
| 2426 |
} |
| 2427 |
|
| 2428 |
func TestUnmarshalUnexported(t *testing.T) { |
| 2429 |
input := `{"Name": "Bob", "m": {"x": 123}, "m2": {"y": 456}, "abcd": {"z": 789}, "s": [2, 3]}` |
| 2430 |
want := &unexportedFields{Name: "Bob"} |
| 2431 |
|
| 2432 |
out := &unexportedFields{} |
| 2433 |
err := json.Unmarshal([]byte(input), out) |
| 2434 |
if err != nil { |
| 2435 |
t.Errorf("got error %v, expected nil", err) |
| 2436 |
} |
| 2437 |
if !reflect.DeepEqual(out, want) { |
| 2438 |
t.Errorf("got %q, want %q", out, want) |
| 2439 |
} |
| 2440 |
} |
| 2441 |
|
| 2442 |
// Time3339 is a time.Time which encodes to and from JSON |
| 2443 |
// as an RFC 3339 time in UTC. |
| 2444 |
type Time3339 time.Time |
| 2445 |
|
| 2446 |
func (t *Time3339) UnmarshalJSON(b []byte) error { |
| 2447 |
if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' { |
| 2448 |
return fmt.Errorf("types: failed to unmarshal non-string value %q as an RFC 3339 time", b) |
| 2449 |
} |
| 2450 |
tm, err := time.Parse(time.RFC3339, string(b[1:len(b)-1])) |
| 2451 |
if err != nil { |
| 2452 |
return err |
| 2453 |
} |
| 2454 |
*t = Time3339(tm) |
| 2455 |
return nil |
| 2456 |
} |
| 2457 |
|
| 2458 |
func TestUnmarshalJSONLiteralError(t *testing.T) { |
| 2459 |
var t3 Time3339 |
| 2460 |
err := json.Unmarshal([]byte(`"0000-00-00T00:00:00Z"`), &t3) |
| 2461 |
if err == nil { |
| 2462 |
t.Fatalf("expected error; got time %v", time.Time(t3)) |
| 2463 |
} |
| 2464 |
if !strings.Contains(err.Error(), "range") { |
| 2465 |
t.Errorf("got err = %v; want out of range error", err) |
| 2466 |
} |
| 2467 |
} |
| 2468 |
|
| 2469 |
// Test that extra object elements in an array do not result in a |
| 2470 |
// "data changing underfoot" error. |
| 2471 |
// Issue 3717 |
| 2472 |
func TestSkipArrayObjects(t *testing.T) { |
| 2473 |
data := `[{}]` |
| 2474 |
var dest [0]any |
| 2475 |
|
| 2476 |
err := json.Unmarshal([]byte(data), &dest) |
| 2477 |
if err != nil { |
| 2478 |
t.Errorf("got error %q, want nil", err) |
| 2479 |
} |
| 2480 |
} |
| 2481 |
|
| 2482 |
// Test semantics of pre-filled data, such as struct fields, map elements, |
| 2483 |
// slices, and arrays. |
| 2484 |
// Issues 4900 and 8837, among others. |
| 2485 |
func TestPrefilled(t *testing.T) { |
| 2486 |
// Values here change, cannot reuse table across runs. |
| 2487 |
var prefillTests = []struct { |
| 2488 |
in string |
| 2489 |
ptr any |
| 2490 |
out any |
| 2491 |
}{ |
| 2492 |
{ |
| 2493 |
in: `{"X": 1, "Y": 2}`, |
| 2494 |
ptr: &XYZ{X: float32(3), Y: int16(4), Z: 1.5}, |
| 2495 |
out: &XYZ{X: float64(1), Y: float64(2), Z: 1.5}, |
| 2496 |
}, |
| 2497 |
{ |
| 2498 |
in: `{"X": 1, "Y": 2}`, |
| 2499 |
ptr: &map[string]any{"X": float32(3), "Y": int16(4), "Z": 1.5}, |
| 2500 |
out: &map[string]any{"X": float64(1), "Y": float64(2), "Z": 1.5}, |
| 2501 |
}, |
| 2502 |
{ |
| 2503 |
in: `[2]`, |
| 2504 |
ptr: &[]int{1}, |
| 2505 |
out: &[]int{2}, |
| 2506 |
}, |
| 2507 |
{ |
| 2508 |
in: `[2, 3]`, |
| 2509 |
ptr: &[]int{1}, |
| 2510 |
out: &[]int{2, 3}, |
| 2511 |
}, |
| 2512 |
{ |
| 2513 |
in: `[2, 3]`, |
| 2514 |
ptr: &[...]int{1}, |
| 2515 |
out: &[...]int{2}, |
| 2516 |
}, |
| 2517 |
{ |
| 2518 |
in: `[3]`, |
| 2519 |
ptr: &[...]int{1, 2}, |
| 2520 |
out: &[...]int{3, 0}, |
| 2521 |
}, |
| 2522 |
} |
| 2523 |
|
| 2524 |
for _, tt := range prefillTests { |
| 2525 |
ptrstr := fmt.Sprintf("%v", tt.ptr) |
| 2526 |
err := json.Unmarshal([]byte(tt.in), tt.ptr) // tt.ptr edited here |
| 2527 |
if err != nil { |
| 2528 |
t.Errorf("Unmarshal: %v", err) |
| 2529 |
} |
| 2530 |
if !reflect.DeepEqual(tt.ptr, tt.out) { |
| 2531 |
t.Errorf("Unmarshal(%#q, %s): have %v, want %v", tt.in, ptrstr, tt.ptr, tt.out) |
| 2532 |
} |
| 2533 |
} |
| 2534 |
} |
| 2535 |
|
| 2536 |
var invalidUnmarshalTests = []struct { |
| 2537 |
v any |
| 2538 |
want string |
| 2539 |
}{ |
| 2540 |
{nil, "json: Unmarshal(nil)"}, |
| 2541 |
{struct{}{}, "json: Unmarshal(non-pointer struct {})"}, |
| 2542 |
{(*int)(nil), "json: Unmarshal(nil *int)"}, |
| 2543 |
} |
| 2544 |
|
| 2545 |
func TestInvalidUnmarshal(t *testing.T) { |
| 2546 |
buf := []byte(`{"a":"1"}`) |
| 2547 |
for _, tt := range invalidUnmarshalTests { |
| 2548 |
err := json.Unmarshal(buf, tt.v) |
| 2549 |
if err == nil { |
| 2550 |
t.Errorf("Unmarshal expecting error, got nil") |
| 2551 |
continue |
| 2552 |
} |
| 2553 |
if got := err.Error(); got != tt.want { |
| 2554 |
t.Errorf("Unmarshal = %q; want %q", got, tt.want) |
| 2555 |
} |
| 2556 |
} |
| 2557 |
} |
| 2558 |
|
| 2559 |
var invalidUnmarshalTextTests = []struct { |
| 2560 |
v any |
| 2561 |
want string |
| 2562 |
}{ |
| 2563 |
{nil, "json: Unmarshal(nil)"}, |
| 2564 |
{struct{}{}, "json: Unmarshal(non-pointer struct {})"}, |
| 2565 |
{(*int)(nil), "json: Unmarshal(nil *int)"}, |
| 2566 |
{new(net.IP), "json: cannot unmarshal number into Go value of type *net.IP"}, |
| 2567 |
} |
| 2568 |
|
| 2569 |
func TestInvalidUnmarshalText(t *testing.T) { |
| 2570 |
// The errors are the ones of encoding/json of the running Go. |
| 2571 |
buf := []byte(`123`) |
| 2572 |
for _, tt := range invalidUnmarshalTextTests { |
| 2573 |
err := json.Unmarshal(buf, tt.v) |
| 2574 |
if err == nil { |
| 2575 |
t.Errorf("Unmarshal expecting error, got nil") |
| 2576 |
continue |
| 2577 |
} |
| 2578 |
want := stdjson.Unmarshal(buf, tt.v) |
| 2579 |
if got := err.Error(); want == nil || got != want.Error() { |
| 2580 |
t.Errorf("Unmarshal = %q; want %v", got, want) |
| 2581 |
} |
| 2582 |
} |
| 2583 |
} |
| 2584 |
|
| 2585 |
// Whether the type is a pointer is validated when the decoder is compiled, not for every call, |
| 2586 |
// so every entry point must keep returning InvalidUnmarshalError no matter how many times it is called. |
| 2587 |
func TestInvalidUnmarshalEntryPoints(t *testing.T) { |
| 2588 |
entryPoints := []struct { |
| 2589 |
name string |
| 2590 |
decode func([]byte, any) error |
| 2591 |
}{ |
| 2592 |
{"Unmarshal", json.Unmarshal}, |
| 2593 |
{"UnmarshalNoEscape", func(data []byte, v any) error { |
| 2594 |
return json.UnmarshalNoEscape(data, v) |
| 2595 |
}}, |
| 2596 |
{"UnmarshalContext", func(data []byte, v any) error { |
| 2597 |
return json.UnmarshalContext(context.Background(), data, v) |
| 2598 |
}}, |
| 2599 |
{"Decoder.Decode", func(data []byte, v any) error { |
| 2600 |
return json.NewDecoder(bytes.NewReader(data)).Decode(v) |
| 2601 |
}}, |
| 2602 |
} |
| 2603 |
values := []struct { |
| 2604 |
name string |
| 2605 |
v any |
| 2606 |
}{ |
| 2607 |
{"nil", nil}, |
| 2608 |
{"non-pointer struct", struct{ A int }{}}, |
| 2609 |
{"non-pointer int", 1}, |
| 2610 |
{"nil pointer", (*int)(nil)}, |
| 2611 |
{"nil map", map[string]int(nil)}, |
| 2612 |
{"non-nil map", map[string]int{}}, |
| 2613 |
{"non-pointer value whose pointer implements json.Unmarshaler", time.Time{}}, |
| 2614 |
} |
| 2615 |
for _, entryPoint := range entryPoints { |
| 2616 |
for _, value := range values { |
| 2617 |
t.Run(entryPoint.name+"/"+value.name, func(t *testing.T) { |
| 2618 |
for i := 0; i < 2; i++ { |
| 2619 |
err := entryPoint.decode([]byte(`{"A":1}`), value.v) |
| 2620 |
var invalidErr *json.InvalidUnmarshalError |
| 2621 |
if !errors.As(err, &invalidErr) { |
| 2622 |
t.Fatalf("call %d: expected InvalidUnmarshalError but got %T: %v", i, err, err) |
| 2623 |
} |
| 2624 |
if want := reflect.TypeOf(value.v); invalidErr.Type != want { |
| 2625 |
t.Fatalf("call %d: expected type %v but got %v", i, want, invalidErr.Type) |
| 2626 |
} |
| 2627 |
} |
| 2628 |
}) |
| 2629 |
} |
| 2630 |
} |
| 2631 |
} |
| 2632 |
|
| 2633 |
// Test that string option is ignored for invalid types. |
| 2634 |
// Issue 9812. |
| 2635 |
func TestInvalidStringOption(t *testing.T) { |
| 2636 |
num := 0 |
| 2637 |
item := struct { |
| 2638 |
T time.Time `json:",string"` |
| 2639 |
M map[string]string `json:",string"` |
| 2640 |
S []string `json:",string"` |
| 2641 |
A [1]string `json:",string"` |
| 2642 |
I any `json:",string"` |
| 2643 |
P *int `json:",string"` |
| 2644 |
}{M: make(map[string]string), S: make([]string, 0), I: num, P: &num} |
| 2645 |
|
| 2646 |
data, err := json.Marshal(item) |
| 2647 |
if err != nil { |
| 2648 |
t.Fatalf("Marshal: %v", err) |
| 2649 |
} |
| 2650 |
err = json.Unmarshal(data, &item) |
| 2651 |
if err != nil { |
| 2652 |
t.Fatalf("Unmarshal: %v", err) |
| 2653 |
} |
| 2654 |
} |
| 2655 |
|
| 2656 |
// Test unmarshal behavior with regards to embedded unexported structs. |
| 2657 |
// |
| 2658 |
// (Issue 21357) If the embedded struct is a pointer and is unallocated, |
| 2659 |
// this returns an error because unmarshal cannot set the field. |
| 2660 |
// |
| 2661 |
// (Issue 24152) If the embedded struct is given an explicit name, |
| 2662 |
// ensure that the normal unmarshal logic does not panic in reflect. |
| 2663 |
// |
| 2664 |
// (Issue 28145) If the embedded struct is given an explicit name and has |
| 2665 |
// exported methods, don't cause a panic trying to get its value. |
| 2666 |
func TestUnmarshalEmbeddedUnexported(t *testing.T) { |
| 2667 |
type ( |
| 2668 |
embed1 struct{ Q int } |
| 2669 |
embed2 struct{ Q int } |
| 2670 |
embed3 struct { |
| 2671 |
Q int64 `json:",string"` |
| 2672 |
} |
| 2673 |
S1 struct { |
| 2674 |
*embed1 |
| 2675 |
R int |
| 2676 |
} |
| 2677 |
S2 struct { |
| 2678 |
*embed1 |
| 2679 |
Q int |
| 2680 |
} |
| 2681 |
S3 struct { |
| 2682 |
embed1 |
| 2683 |
R int |
| 2684 |
} |
| 2685 |
S4 struct { |
| 2686 |
*embed1 |
| 2687 |
embed2 |
| 2688 |
} |
| 2689 |
S5 struct { |
| 2690 |
*embed3 |
| 2691 |
R int |
| 2692 |
} |
| 2693 |
S6 struct { |
| 2694 |
embed1 `json:"embed1"` |
| 2695 |
} |
| 2696 |
S7 struct { |
| 2697 |
embed1 `json:"embed1"` |
| 2698 |
embed2 |
| 2699 |
} |
| 2700 |
S8 struct { |
| 2701 |
embed1 `json:"embed1"` |
| 2702 |
embed2 `json:"embed2"` |
| 2703 |
Q int |
| 2704 |
} |
| 2705 |
S9 struct { |
| 2706 |
unexportedWithMethods `json:"embed"` |
| 2707 |
} |
| 2708 |
) |
| 2709 |
|
| 2710 |
tests := []struct { |
| 2711 |
in string |
| 2712 |
ptr any |
| 2713 |
out any |
| 2714 |
err error |
| 2715 |
}{{ |
| 2716 |
// Error since we cannot set S1.embed1, but still able to set S1.R. |
| 2717 |
in: `{"R":2,"Q":1}`, |
| 2718 |
ptr: new(S1), |
| 2719 |
out: &S1{R: 2}, |
| 2720 |
err: fmt.Errorf("json: cannot set embedded pointer to unexported struct: json_test.embed1"), |
| 2721 |
}, { |
| 2722 |
// The top level Q field takes precedence. |
| 2723 |
in: `{"Q":1}`, |
| 2724 |
ptr: new(S2), |
| 2725 |
out: &S2{Q: 1}, |
| 2726 |
}, { |
| 2727 |
// No issue with non-pointer variant. |
| 2728 |
in: `{"R":2,"Q":1}`, |
| 2729 |
ptr: new(S3), |
| 2730 |
out: &S3{embed1: embed1{Q: 1}, R: 2}, |
| 2731 |
}, { |
| 2732 |
// No error since both embedded structs have field R, which annihilate each other. |
| 2733 |
// Thus, no attempt is made at setting S4.embed1. |
| 2734 |
in: `{"R":2}`, |
| 2735 |
ptr: new(S4), |
| 2736 |
out: new(S4), |
| 2737 |
}, { |
| 2738 |
// Error since we cannot set S5.embed1, but still able to set S5.R. |
| 2739 |
in: `{"R":2,"Q":1}`, |
| 2740 |
ptr: new(S5), |
| 2741 |
out: &S5{R: 2}, |
| 2742 |
err: fmt.Errorf("json: cannot set embedded pointer to unexported struct: json_test.embed3"), |
| 2743 |
}, { |
| 2744 |
// Issue 24152, ensure decodeState.indirect does not panic. |
| 2745 |
in: `{"embed1": {"Q": 1}}`, |
| 2746 |
ptr: new(S6), |
| 2747 |
out: &S6{embed1{1}}, |
| 2748 |
}, { |
| 2749 |
// Issue 24153, check that we can still set forwarded fields even in |
| 2750 |
// the presence of a name conflict. |
| 2751 |
// |
| 2752 |
// This relies on obscure behavior of reflect where it is possible |
| 2753 |
// to set a forwarded exported field on an unexported embedded struct |
| 2754 |
// even though there is a name conflict, even when it would have been |
| 2755 |
// impossible to do so according to Go visibility rules. |
| 2756 |
// Go forbids this because it is ambiguous whether S7.Q refers to |
| 2757 |
// S7.embed1.Q or S7.embed2.Q. Since embed1 and embed2 are unexported, |
| 2758 |
// it should be impossible for an external package to set either Q. |
| 2759 |
// |
| 2760 |
// It is probably okay for a future reflect change to break this. |
| 2761 |
in: `{"embed1": {"Q": 1}, "Q": 2}`, |
| 2762 |
ptr: new(S7), |
| 2763 |
out: &S7{embed1{1}, embed2{2}}, |
| 2764 |
}, { |
| 2765 |
// Issue 24153, similar to the S7 case. |
| 2766 |
in: `{"embed1": {"Q": 1}, "embed2": {"Q": 2}, "Q": 3}`, |
| 2767 |
ptr: new(S8), |
| 2768 |
out: &S8{embed1{1}, embed2{2}, 3}, |
| 2769 |
}, { |
| 2770 |
// Issue 228145, similar to the cases above. |
| 2771 |
in: `{"embed": {}}`, |
| 2772 |
ptr: new(S9), |
| 2773 |
out: &S9{}, |
| 2774 |
}} |
| 2775 |
|
| 2776 |
for i, tt := range tests { |
| 2777 |
err := json.Unmarshal([]byte(tt.in), tt.ptr) |
| 2778 |
if (err != nil) != (tt.err != nil) { |
| 2779 |
t.Errorf("#%d: %v, want %v", i, err, tt.err) |
| 2780 |
} |
| 2781 |
// the error is the one of encoding/json of the Go version: a type error of the struct with Go 1.27 |
| 2782 |
wantErr := stdjson.Unmarshal([]byte(tt.in), reflect.New(reflect.TypeOf(tt.ptr).Elem()).Interface()) |
| 2783 |
if describeTypeError(err) != describeTypeError(wantErr) { |
| 2784 |
t.Errorf("#%d: %v, want %v", i, err, wantErr) |
| 2785 |
} |
| 2786 |
if !reflect.DeepEqual(tt.ptr, tt.out) { |
| 2787 |
t.Errorf("#%d: mismatch\ngot: %#+v\nwant: %#+v", i, tt.ptr, tt.out) |
| 2788 |
} |
| 2789 |
} |
| 2790 |
} |
| 2791 |
|
| 2792 |
func TestUnmarshalErrorAfterMultipleJSON(t *testing.T) { |
| 2793 |
// The error after the values of a stream is the one of encoding/json of the Go version. |
| 2794 |
for i, in := range []string{`1 false null :`, `1 [] [,]`, `1 [] [true:]`, `1 {} {"x"=}`, `falsetruenul#`} { |
| 2795 |
lastError := func(newDecoder func() interface{ Decode(any) error }) error { |
| 2796 |
dec := newDecoder() |
| 2797 |
for { |
| 2798 |
var v any |
| 2799 |
if err := dec.Decode(&v); err != nil { |
| 2800 |
return err |
| 2801 |
} |
| 2802 |
} |
| 2803 |
} |
| 2804 |
err := lastError(func() interface{ Decode(any) error } { return json.NewDecoder(strings.NewReader(in)) }) |
| 2805 |
want := lastError(func() interface{ Decode(any) error } { return stdjson.NewDecoder(strings.NewReader(in)) }) |
| 2806 |
if describeTypeError(err) != describeTypeError(want) { |
| 2807 |
t.Errorf("#%d: got %#v, want %#v", i, err, want) |
| 2808 |
} |
| 2809 |
} |
| 2810 |
} |
| 2811 |
|
| 2812 |
type unmarshalPanic struct{} |
| 2813 |
|
| 2814 |
func (unmarshalPanic) UnmarshalJSON([]byte) error { panic(0xdead) } |
| 2815 |
|
| 2816 |
func TestUnmarshalPanic(t *testing.T) { |
| 2817 |
defer func() { |
| 2818 |
if got := recover(); !reflect.DeepEqual(got, 0xdead) { |
| 2819 |
t.Errorf("panic() = (%T)(%v), want 0xdead", got, got) |
| 2820 |
} |
| 2821 |
}() |
| 2822 |
json.Unmarshal([]byte("{}"), &unmarshalPanic{}) |
| 2823 |
t.Fatalf("Unmarshal should have panicked") |
| 2824 |
} |
| 2825 |
|
| 2826 |
// The decoder used to hang if decoding into an interface pointing to its own address. |
| 2827 |
// See golang.org/issues/31740. |
| 2828 |
func TestUnmarshalRecursivePointer(t *testing.T) { |
| 2829 |
var v any |
| 2830 |
v = &v |
| 2831 |
data := []byte(`{"a": "b"}`) |
| 2832 |
|
| 2833 |
if err := json.Unmarshal(data, v); err != nil { |
| 2834 |
t.Fatal(err) |
| 2835 |
} |
| 2836 |
} |
| 2837 |
|
| 2838 |
type textUnmarshalerString string |
| 2839 |
|
| 2840 |
func (m *textUnmarshalerString) UnmarshalText(text []byte) error { |
| 2841 |
*m = textUnmarshalerString(strings.ToLower(string(text))) |
| 2842 |
return nil |
| 2843 |
} |
| 2844 |
|
| 2845 |
// Test unmarshal to a map, where the map key is a user defined type. |
| 2846 |
// See golang.org/issues/34437. |
| 2847 |
func TestUnmarshalMapWithTextUnmarshalerStringKey(t *testing.T) { |
| 2848 |
var p map[textUnmarshalerString]string |
| 2849 |
if err := json.Unmarshal([]byte(`{"FOO": "1"}`), &p); err != nil { |
| 2850 |
t.Fatalf("Unmarshal unexpected error: %v", err) |
| 2851 |
} |
| 2852 |
|
| 2853 |
if _, ok := p["foo"]; !ok { |
| 2854 |
t.Errorf(`Key "foo" does not exist in map: %v`, p) |
| 2855 |
} |
| 2856 |
} |
| 2857 |
|
| 2858 |
func TestUnmarshalRescanLiteralMangledUnquote(t *testing.T) { |
| 2859 |
// See golang.org/issues/38105. |
| 2860 |
var p map[textUnmarshalerString]string |
| 2861 |
if err := json.Unmarshal([]byte(`{"开源":"12345开源"}`), &p); err != nil { |
| 2862 |
t.Fatalf("Unmarshal unexpected error: %v", err) |
| 2863 |
} |
| 2864 |
if _, ok := p["开源"]; !ok { |
| 2865 |
t.Errorf(`Key "开源" does not exist in map: %v`, p) |
| 2866 |
} |
| 2867 |
|
| 2868 |
// See golang.org/issues/38126. |
| 2869 |
type T struct { |
| 2870 |
F1 string `json:"F1,string"` |
| 2871 |
} |
| 2872 |
t1 := T{"aaa\tbbb"} |
| 2873 |
|
| 2874 |
b, err := json.Marshal(t1) |
| 2875 |
if err != nil { |
| 2876 |
t.Fatalf("Marshal unexpected error: %v", err) |
| 2877 |
} |
| 2878 |
var t2 T |
| 2879 |
if err := json.Unmarshal(b, &t2); err != nil { |
| 2880 |
t.Fatalf("Unmarshal unexpected error: %v", err) |
| 2881 |
} |
| 2882 |
if t1 != t2 { |
| 2883 |
t.Errorf("Marshal and Unmarshal roundtrip mismatch: want %q got %q", t1, t2) |
| 2884 |
} |
| 2885 |
|
| 2886 |
// See golang.org/issues/39555. |
| 2887 |
input := map[textUnmarshalerString]string{"FOO": "", `"`: ""} |
| 2888 |
|
| 2889 |
encoded, err := json.Marshal(input) |
| 2890 |
if err != nil { |
| 2891 |
t.Fatalf("Marshal unexpected error: %v", err) |
| 2892 |
} |
| 2893 |
var got map[textUnmarshalerString]string |
| 2894 |
if err := json.Unmarshal(encoded, &got); err != nil { |
| 2895 |
t.Fatalf("Unmarshal unexpected error: %v", err) |
| 2896 |
} |
| 2897 |
want := map[textUnmarshalerString]string{"foo": "", `"`: ""} |
| 2898 |
if !reflect.DeepEqual(want, got) { |
| 2899 |
t.Fatalf("Unexpected roundtrip result:\nwant: %q\ngot: %q", want, got) |
| 2900 |
} |
| 2901 |
} |
| 2902 |
|
| 2903 |
func TestUnmarshalMaxDepth(t *testing.T) { |
| 2904 |
testcases := []struct { |
| 2905 |
name string |
| 2906 |
data string |
| 2907 |
errMaxDepth bool |
| 2908 |
}{ |
| 2909 |
{ |
| 2910 |
name: "ArrayUnderMaxNestingDepth", |
| 2911 |
data: `{"a":` + strings.Repeat(`[`, 10000-1) + strings.Repeat(`]`, 10000-1) + `}`, |
| 2912 |
errMaxDepth: false, |
| 2913 |
}, |
| 2914 |
{ |
| 2915 |
name: "ArrayOverMaxNestingDepth", |
| 2916 |
data: `{"a":` + strings.Repeat(`[`, 10000) + strings.Repeat(`]`, 10000) + `}`, |
| 2917 |
errMaxDepth: true, |
| 2918 |
}, |
| 2919 |
{ |
| 2920 |
name: "ArrayOverStackDepth", |
| 2921 |
data: `{"a":` + strings.Repeat(`[`, 3000000) + strings.Repeat(`]`, 3000000) + `}`, |
| 2922 |
errMaxDepth: true, |
| 2923 |
}, |
| 2924 |
{ |
| 2925 |
name: "ObjectUnderMaxNestingDepth", |
| 2926 |
data: `{"a":` + strings.Repeat(`{"a":`, 10000-1) + `0` + strings.Repeat(`}`, 10000-1) + `}`, |
| 2927 |
errMaxDepth: false, |
| 2928 |
}, |
| 2929 |
{ |
| 2930 |
name: "ObjectOverMaxNestingDepth", |
| 2931 |
data: `{"a":` + strings.Repeat(`{"a":`, 10000) + `0` + strings.Repeat(`}`, 10000) + `}`, |
| 2932 |
errMaxDepth: true, |
| 2933 |
}, |
| 2934 |
{ |
| 2935 |
name: "ObjectOverStackDepth", |
| 2936 |
data: `{"a":` + strings.Repeat(`{"a":`, 3000000) + `0` + strings.Repeat(`}`, 3000000) + `}`, |
| 2937 |
errMaxDepth: true, |
| 2938 |
}, |
| 2939 |
} |
| 2940 |
|
| 2941 |
targets := []struct { |
| 2942 |
name string |
| 2943 |
newValue func() any |
| 2944 |
}{ |
| 2945 |
{ |
| 2946 |
name: "unstructured", |
| 2947 |
newValue: func() any { |
| 2948 |
var v any |
| 2949 |
return &v |
| 2950 |
}, |
| 2951 |
}, |
| 2952 |
{ |
| 2953 |
name: "typed named field", |
| 2954 |
newValue: func() any { |
| 2955 |
v := struct { |
| 2956 |
A any `json:"a"` |
| 2957 |
}{} |
| 2958 |
return &v |
| 2959 |
}, |
| 2960 |
}, |
| 2961 |
{ |
| 2962 |
name: "typed missing field", |
| 2963 |
newValue: func() any { |
| 2964 |
v := struct { |
| 2965 |
B any `json:"b"` |
| 2966 |
}{} |
| 2967 |
return &v |
| 2968 |
}, |
| 2969 |
}, |
| 2970 |
{ |
| 2971 |
name: "custom unmarshaler", |
| 2972 |
newValue: func() any { |
| 2973 |
v := unmarshaler{} |
| 2974 |
return &v |
| 2975 |
}, |
| 2976 |
}, |
| 2977 |
} |
| 2978 |
|
| 2979 |
for _, tc := range testcases { |
| 2980 |
for _, target := range targets { |
| 2981 |
t.Run(target.name+"-"+tc.name, func(t *testing.T) { |
| 2982 |
t.Run("unmarshal", func(t *testing.T) { |
| 2983 |
err := json.Unmarshal([]byte(tc.data), target.newValue()) |
| 2984 |
if !tc.errMaxDepth { |
| 2985 |
if err != nil { |
| 2986 |
t.Errorf("unexpected error: %v", err) |
| 2987 |
} |
| 2988 |
} else { |
| 2989 |
if err == nil { |
| 2990 |
t.Errorf("expected error containing 'exceeded max depth', got none") |
| 2991 |
} else if !strings.Contains(err.Error(), "exceeded max depth") { |
| 2992 |
t.Errorf("expected error containing 'exceeded max depth', got: %v", err) |
| 2993 |
} |
| 2994 |
} |
| 2995 |
}) |
| 2996 |
t.Run("stream", func(t *testing.T) { |
| 2997 |
err := json.NewDecoder(strings.NewReader(tc.data)).Decode(target.newValue()) |
| 2998 |
if !tc.errMaxDepth { |
| 2999 |
if err != nil { |
| 3000 |
t.Errorf("unexpected error: %v", err) |
| 3001 |
} |
| 3002 |
} else { |
| 3003 |
if err == nil { |
| 3004 |
t.Errorf("expected error containing 'exceeded max depth', got none") |
| 3005 |
} else if !strings.Contains(err.Error(), "exceeded max depth") { |
| 3006 |
t.Errorf("expected error containing 'exceeded max depth', got: %v", err) |
| 3007 |
} |
| 3008 |
} |
| 3009 |
}) |
| 3010 |
}) |
| 3011 |
} |
| 3012 |
} |
| 3013 |
} |
| 3014 |
|
| 3015 |
func TestDecodeSlice(t *testing.T) { |
| 3016 |
type B struct{ Int int32 } |
| 3017 |
type A struct{ B *B } |
| 3018 |
type X struct{ A []*A } |
| 3019 |
|
| 3020 |
w1 := &X{} |
| 3021 |
w2 := &X{} |
| 3022 |
|
| 3023 |
if err := json.Unmarshal([]byte(`{"a": [ {"b":{"int": 42} } ] }`), w1); err != nil { |
| 3024 |
t.Fatal(err) |
| 3025 |
} |
| 3026 |
w1addr := uintptr(unsafe.Pointer(w1.A[0].B)) |
| 3027 |
|
| 3028 |
if err := json.Unmarshal([]byte(`{"a": [ {"b":{"int": 112} } ] }`), w2); err != nil { |
| 3029 |
t.Fatal(err) |
| 3030 |
} |
| 3031 |
if uintptr(unsafe.Pointer(w1.A[0].B)) != w1addr { |
| 3032 |
t.Fatal("wrong addr") |
| 3033 |
} |
| 3034 |
w2addr := uintptr(unsafe.Pointer(w2.A[0].B)) |
| 3035 |
if w1addr == w2addr { |
| 3036 |
t.Fatal("invaid address") |
| 3037 |
} |
| 3038 |
} |
| 3039 |
|
| 3040 |
func TestDecodeMultipleUnmarshal(t *testing.T) { |
| 3041 |
data := []byte(`[{"AA":{"X":[{"a": "A"},{"b": "B"}],"Y":"y","Z":"z"},"BB":"bb"},{"AA":{"X":[],"Y":"y","Z":"z"},"BB":"bb"}]`) |
| 3042 |
var a []json.RawMessage |
| 3043 |
if err := json.Unmarshal(data, &a); err != nil { |
| 3044 |
t.Fatal(err) |
| 3045 |
} |
| 3046 |
if len(a) != 2 { |
| 3047 |
t.Fatalf("failed to decode: got %v", a) |
| 3048 |
} |
| 3049 |
t.Run("first", func(t *testing.T) { |
| 3050 |
data := a[0] |
| 3051 |
var v map[string]json.RawMessage |
| 3052 |
if err := json.Unmarshal(data, &v); err != nil { |
| 3053 |
t.Fatal(err) |
| 3054 |
} |
| 3055 |
if string(v["AA"]) != `{"X":[{"a": "A"},{"b": "B"}],"Y":"y","Z":"z"}` { |
| 3056 |
t.Fatalf("failed to decode. got %q", v["AA"]) |
| 3057 |
} |
| 3058 |
var aa map[string]json.RawMessage |
| 3059 |
if err := json.Unmarshal(v["AA"], &aa); err != nil { |
| 3060 |
t.Fatal(err) |
| 3061 |
} |
| 3062 |
if string(aa["X"]) != `[{"a": "A"},{"b": "B"}]` { |
| 3063 |
t.Fatalf("failed to decode. got %q", v["X"]) |
| 3064 |
} |
| 3065 |
var x []json.RawMessage |
| 3066 |
if err := json.Unmarshal(aa["X"], &x); err != nil { |
| 3067 |
t.Fatal(err) |
| 3068 |
} |
| 3069 |
if len(x) != 2 { |
| 3070 |
t.Fatalf("failed to decode: %v", x) |
| 3071 |
} |
| 3072 |
if string(x[0]) != `{"a": "A"}` { |
| 3073 |
t.Fatal("failed to decode") |
| 3074 |
} |
| 3075 |
if string(x[1]) != `{"b": "B"}` { |
| 3076 |
t.Fatal("failed to decode") |
| 3077 |
} |
| 3078 |
}) |
| 3079 |
t.Run("second", func(t *testing.T) { |
| 3080 |
data := a[1] |
| 3081 |
var v map[string]json.RawMessage |
| 3082 |
if err := json.Unmarshal(data, &v); err != nil { |
| 3083 |
t.Fatal(err) |
| 3084 |
} |
| 3085 |
if string(v["AA"]) != `{"X":[],"Y":"y","Z":"z"}` { |
| 3086 |
t.Fatalf("failed to decode. got %q", v["AA"]) |
| 3087 |
} |
| 3088 |
var aa map[string]json.RawMessage |
| 3089 |
if err := json.Unmarshal(v["AA"], &aa); err != nil { |
| 3090 |
t.Fatal(err) |
| 3091 |
} |
| 3092 |
if string(aa["X"]) != `[]` { |
| 3093 |
t.Fatalf("failed to decode. got %q", v["X"]) |
| 3094 |
} |
| 3095 |
var x []json.RawMessage |
| 3096 |
if err := json.Unmarshal(aa["X"], &x); err != nil { |
| 3097 |
t.Fatal(err) |
| 3098 |
} |
| 3099 |
if len(x) != 0 { |
| 3100 |
t.Fatalf("failed to decode: %v", x) |
| 3101 |
} |
| 3102 |
}) |
| 3103 |
} |
| 3104 |
|
| 3105 |
func TestMultipleDecodeWithRawMessage(t *testing.T) { |
| 3106 |
original := []byte(`{ |
| 3107 |
"Body": { |
| 3108 |
"List": [ |
| 3109 |
{ |
| 3110 |
"Returns": [ |
| 3111 |
{ |
| 3112 |
"Value": "10", |
| 3113 |
"nodeType": "Literal" |
| 3114 |
} |
| 3115 |
], |
| 3116 |
"nodeKind": "Return", |
| 3117 |
"nodeType": "Statement" |
| 3118 |
} |
| 3119 |
], |
| 3120 |
"nodeKind": "Block", |
| 3121 |
"nodeType": "Statement" |
| 3122 |
}, |
| 3123 |
"nodeType": "Function" |
| 3124 |
}`) |
| 3125 |
|
| 3126 |
var a map[string]json.RawMessage |
| 3127 |
if err := json.Unmarshal(original, &a); err != nil { |
| 3128 |
t.Fatal(err) |
| 3129 |
} |
| 3130 |
var b map[string]json.RawMessage |
| 3131 |
if err := json.Unmarshal(a["Body"], &b); err != nil { |
| 3132 |
t.Fatal(err) |
| 3133 |
} |
| 3134 |
var c []json.RawMessage |
| 3135 |
if err := json.Unmarshal(b["List"], &c); err != nil { |
| 3136 |
t.Fatal(err) |
| 3137 |
} |
| 3138 |
var d map[string]json.RawMessage |
| 3139 |
if err := json.Unmarshal(c[0], &d); err != nil { |
| 3140 |
t.Fatal(err) |
| 3141 |
} |
| 3142 |
var e []json.RawMessage |
| 3143 |
if err := json.Unmarshal(d["Returns"], &e); err != nil { |
| 3144 |
t.Fatal(err) |
| 3145 |
} |
| 3146 |
var f map[string]json.RawMessage |
| 3147 |
if err := json.Unmarshal(e[0], &f); err != nil { |
| 3148 |
t.Fatal(err) |
| 3149 |
} |
| 3150 |
} |
| 3151 |
|
| 3152 |
type intUnmarshaler int |
| 3153 |
|
| 3154 |
func (u *intUnmarshaler) UnmarshalJSON(b []byte) error { |
| 3155 |
if *u != 0 && *u != 10 { |
| 3156 |
return fmt.Errorf("failed to decode of slice with int unmarshaler") |
| 3157 |
} |
| 3158 |
*u = 10 |
| 3159 |
return nil |
| 3160 |
} |
| 3161 |
|
| 3162 |
type arrayUnmarshaler [5]int |
| 3163 |
|
| 3164 |
func (u *arrayUnmarshaler) UnmarshalJSON(b []byte) error { |
| 3165 |
if (*u)[0] != 0 && (*u)[0] != 10 { |
| 3166 |
return fmt.Errorf("failed to decode of slice with array unmarshaler") |
| 3167 |
} |
| 3168 |
(*u)[0] = 10 |
| 3169 |
return nil |
| 3170 |
} |
| 3171 |
|
| 3172 |
type mapUnmarshaler map[string]int |
| 3173 |
|
| 3174 |
func (u *mapUnmarshaler) UnmarshalJSON(b []byte) error { |
| 3175 |
if len(*u) != 0 && len(*u) != 1 { |
| 3176 |
return fmt.Errorf("failed to decode of slice with map unmarshaler") |
| 3177 |
} |
| 3178 |
*u = map[string]int{"a": 10} |
| 3179 |
return nil |
| 3180 |
} |
| 3181 |
|
| 3182 |
type structUnmarshaler struct { |
| 3183 |
A int |
| 3184 |
notFirst bool |
| 3185 |
} |
| 3186 |
|
| 3187 |
func (u *structUnmarshaler) UnmarshalJSON(b []byte) error { |
| 3188 |
if !u.notFirst && u.A != 0 { |
| 3189 |
return fmt.Errorf("failed to decode of slice with struct unmarshaler") |
| 3190 |
} |
| 3191 |
u.A = 10 |
| 3192 |
u.notFirst = true |
| 3193 |
return nil |
| 3194 |
} |
| 3195 |
|
| 3196 |
func TestSliceElemUnmarshaler(t *testing.T) { |
| 3197 |
t.Run("int", func(t *testing.T) { |
| 3198 |
var v []intUnmarshaler |
| 3199 |
if err := json.Unmarshal([]byte(`[1,2,3,4,5]`), &v); err != nil { |
| 3200 |
t.Fatal(err) |
| 3201 |
} |
| 3202 |
if len(v) != 5 { |
| 3203 |
t.Fatalf("failed to decode of slice with int unmarshaler: %v", v) |
| 3204 |
} |
| 3205 |
if v[0] != 10 { |
| 3206 |
t.Fatalf("failed to decode of slice with int unmarshaler: %v", v) |
| 3207 |
} |
| 3208 |
if err := json.Unmarshal([]byte(`[6]`), &v); err != nil { |
| 3209 |
t.Fatal(err) |
| 3210 |
} |
| 3211 |
if len(v) != 1 { |
| 3212 |
t.Fatalf("failed to decode of slice with int unmarshaler: %v", v) |
| 3213 |
} |
| 3214 |
if v[0] != 10 { |
| 3215 |
t.Fatalf("failed to decode of slice with int unmarshaler: %v", v) |
| 3216 |
} |
| 3217 |
}) |
| 3218 |
t.Run("slice", func(t *testing.T) { |
| 3219 |
var v []json.RawMessage |
| 3220 |
if err := json.Unmarshal([]byte(`[1,2,3,4,5]`), &v); err != nil { |
| 3221 |
t.Fatal(err) |
| 3222 |
} |
| 3223 |
if len(v) != 5 { |
| 3224 |
t.Fatalf("failed to decode of slice with slice unmarshaler: %v", v) |
| 3225 |
} |
| 3226 |
if len(v[0]) != 1 { |
| 3227 |
t.Fatalf("failed to decode of slice with slice unmarshaler: %v", v) |
| 3228 |
} |
| 3229 |
if err := json.Unmarshal([]byte(`[6]`), &v); err != nil { |
| 3230 |
t.Fatal(err) |
| 3231 |
} |
| 3232 |
if len(v) != 1 { |
| 3233 |
t.Fatalf("failed to decode of slice with slice unmarshaler: %v", v) |
| 3234 |
} |
| 3235 |
if len(v[0]) != 1 { |
| 3236 |
t.Fatalf("failed to decode of slice with slice unmarshaler: %v", v) |
| 3237 |
} |
| 3238 |
}) |
| 3239 |
t.Run("array", func(t *testing.T) { |
| 3240 |
var v []arrayUnmarshaler |
| 3241 |
if err := json.Unmarshal([]byte(`[1,2,3,4,5]`), &v); err != nil { |
| 3242 |
t.Fatal(err) |
| 3243 |
} |
| 3244 |
if len(v) != 5 { |
| 3245 |
t.Fatalf("failed to decode of slice with array unmarshaler: %v", v) |
| 3246 |
} |
| 3247 |
if v[0][0] != 10 { |
| 3248 |
t.Fatalf("failed to decode of slice with array unmarshaler: %v", v) |
| 3249 |
} |
| 3250 |
if err := json.Unmarshal([]byte(`[6]`), &v); err != nil { |
| 3251 |
t.Fatal(err) |
| 3252 |
} |
| 3253 |
if len(v) != 1 { |
| 3254 |
t.Fatalf("failed to decode of slice with array unmarshaler: %v", v) |
| 3255 |
} |
| 3256 |
if v[0][0] != 10 { |
| 3257 |
t.Fatalf("failed to decode of slice with array unmarshaler: %v", v) |
| 3258 |
} |
| 3259 |
}) |
| 3260 |
t.Run("map", func(t *testing.T) { |
| 3261 |
var v []mapUnmarshaler |
| 3262 |
if err := json.Unmarshal([]byte(`[{"a":1},{"b":2},{"c":3},{"d":4},{"e":5}]`), &v); err != nil { |
| 3263 |
t.Fatal(err) |
| 3264 |
} |
| 3265 |
if len(v) != 5 { |
| 3266 |
t.Fatalf("failed to decode of slice with map unmarshaler: %v", v) |
| 3267 |
} |
| 3268 |
if v[0]["a"] != 10 { |
| 3269 |
t.Fatalf("failed to decode of slice with map unmarshaler: %v", v) |
| 3270 |
} |
| 3271 |
if err := json.Unmarshal([]byte(`[6]`), &v); err != nil { |
| 3272 |
t.Fatal(err) |
| 3273 |
} |
| 3274 |
if len(v) != 1 { |
| 3275 |
t.Fatalf("failed to decode of slice with map unmarshaler: %v", v) |
| 3276 |
} |
| 3277 |
if v[0]["a"] != 10 { |
| 3278 |
t.Fatalf("failed to decode of slice with map unmarshaler: %v", v) |
| 3279 |
} |
| 3280 |
}) |
| 3281 |
t.Run("struct", func(t *testing.T) { |
| 3282 |
var v []structUnmarshaler |
| 3283 |
if err := json.Unmarshal([]byte(`[1,2,3,4,5]`), &v); err != nil { |
| 3284 |
t.Fatal(err) |
| 3285 |
} |
| 3286 |
if len(v) != 5 { |
| 3287 |
t.Fatalf("failed to decode of slice with struct unmarshaler: %v", v) |
| 3288 |
} |
| 3289 |
if v[0].A != 10 { |
| 3290 |
t.Fatalf("failed to decode of slice with struct unmarshaler: %v", v) |
| 3291 |
} |
| 3292 |
if err := json.Unmarshal([]byte(`[6]`), &v); err != nil { |
| 3293 |
t.Fatal(err) |
| 3294 |
} |
| 3295 |
if len(v) != 1 { |
| 3296 |
t.Fatalf("failed to decode of slice with struct unmarshaler: %v", v) |
| 3297 |
} |
| 3298 |
if v[0].A != 10 { |
| 3299 |
t.Fatalf("failed to decode of slice with struct unmarshaler: %v", v) |
| 3300 |
} |
| 3301 |
}) |
| 3302 |
} |
| 3303 |
|
| 3304 |
type keepRefTest struct { |
| 3305 |
A int |
| 3306 |
B string |
| 3307 |
} |
| 3308 |
|
| 3309 |
func (t *keepRefTest) UnmarshalJSON(data []byte) error { |
| 3310 |
v := []any{&t.A, &t.B} |
| 3311 |
return json.Unmarshal(data, &v) |
| 3312 |
} |
| 3313 |
|
| 3314 |
func TestKeepReferenceSlice(t *testing.T) { |
| 3315 |
var v keepRefTest |
| 3316 |
if err := json.Unmarshal([]byte(`[54,"hello"]`), &v); err != nil { |
| 3317 |
t.Fatal(err) |
| 3318 |
} |
| 3319 |
if v.A != 54 { |
| 3320 |
t.Fatal("failed to keep reference for slice") |
| 3321 |
} |
| 3322 |
if v.B != "hello" { |
| 3323 |
t.Fatal("failed to keep reference for slice") |
| 3324 |
} |
| 3325 |
} |
| 3326 |
|
| 3327 |
func TestInvalidTopLevelValue(t *testing.T) { |
| 3328 |
t.Run("invalid end of buffer", func(t *testing.T) { |
| 3329 |
var v struct{} |
| 3330 |
if err := stdjson.Unmarshal([]byte(`{}0`), &v); err == nil { |
| 3331 |
t.Fatal("expected error") |
| 3332 |
} |
| 3333 |
if err := json.Unmarshal([]byte(`{}0`), &v); err == nil { |
| 3334 |
t.Fatal("expected error") |
| 3335 |
} |
| 3336 |
}) |
| 3337 |
t.Run("invalid object", func(t *testing.T) { |
| 3338 |
var v any |
| 3339 |
if err := stdjson.Unmarshal([]byte(`{"a":4}{"a"5}`), &v); err == nil { |
| 3340 |
t.Fatal("expected error") |
| 3341 |
} |
| 3342 |
if err := json.Unmarshal([]byte(`{"a":4}{"a"5}`), &v); err == nil { |
| 3343 |
t.Fatal("expected error") |
| 3344 |
} |
| 3345 |
}) |
| 3346 |
} |
| 3347 |
|
| 3348 |
func TestInvalidNumber(t *testing.T) { |
| 3349 |
t.Run("invalid length of number", func(t *testing.T) { |
| 3350 |
invalidNum := strings.Repeat("1", 30) |
| 3351 |
t.Run("int", func(t *testing.T) { |
| 3352 |
var v int64 |
| 3353 |
stdErr := stdjson.Unmarshal([]byte(invalidNum), &v) |
| 3354 |
if stdErr == nil { |
| 3355 |
t.Fatal("expected error") |
| 3356 |
} |
| 3357 |
err := json.Unmarshal([]byte(invalidNum), &v) |
| 3358 |
if err == nil { |
| 3359 |
t.Fatal("expected error") |
| 3360 |
} |
| 3361 |
if stdErr.Error() != err.Error() { |
| 3362 |
t.Fatalf("unexpected error message. expected: %q but got %q", stdErr.Error(), err.Error()) |
| 3363 |
} |
| 3364 |
}) |
| 3365 |
t.Run("uint", func(t *testing.T) { |
| 3366 |
var v uint64 |
| 3367 |
stdErr := stdjson.Unmarshal([]byte(invalidNum), &v) |
| 3368 |
if stdErr == nil { |
| 3369 |
t.Fatal("expected error") |
| 3370 |
} |
| 3371 |
err := json.Unmarshal([]byte(invalidNum), &v) |
| 3372 |
if err == nil { |
| 3373 |
t.Fatal("expected error") |
| 3374 |
} |
| 3375 |
if stdErr.Error() != err.Error() { |
| 3376 |
t.Fatalf("unexpected error message. expected: %q but got %q", stdErr.Error(), err.Error()) |
| 3377 |
} |
| 3378 |
}) |
| 3379 |
|
| 3380 |
}) |
| 3381 |
t.Run("invalid number of zero", func(t *testing.T) { |
| 3382 |
t.Run("int", func(t *testing.T) { |
| 3383 |
invalidNum := strings.Repeat("0", 10) |
| 3384 |
var v int64 |
| 3385 |
stdErr := stdjson.Unmarshal([]byte(invalidNum), &v) |
| 3386 |
if stdErr == nil { |
| 3387 |
t.Fatal("expected error") |
| 3388 |
} |
| 3389 |
err := json.Unmarshal([]byte(invalidNum), &v) |
| 3390 |
if err == nil { |
| 3391 |
t.Fatal("expected error") |
| 3392 |
} |
| 3393 |
if stdErr.Error() != err.Error() { |
| 3394 |
t.Fatalf("unexpected error message. expected: %q but got %q", stdErr.Error(), err.Error()) |
| 3395 |
} |
| 3396 |
}) |
| 3397 |
t.Run("uint", func(t *testing.T) { |
| 3398 |
invalidNum := strings.Repeat("0", 10) |
| 3399 |
var v uint64 |
| 3400 |
stdErr := stdjson.Unmarshal([]byte(invalidNum), &v) |
| 3401 |
if stdErr == nil { |
| 3402 |
t.Fatal("expected error") |
| 3403 |
} |
| 3404 |
err := json.Unmarshal([]byte(invalidNum), &v) |
| 3405 |
if err == nil { |
| 3406 |
t.Fatal("expected error") |
| 3407 |
} |
| 3408 |
if stdErr.Error() != err.Error() { |
| 3409 |
t.Fatalf("unexpected error message. expected: %q but got %q", stdErr.Error(), err.Error()) |
| 3410 |
} |
| 3411 |
}) |
| 3412 |
}) |
| 3413 |
t.Run("invalid number", func(t *testing.T) { |
| 3414 |
t.Run("int", func(t *testing.T) { |
| 3415 |
t.Run("-0", func(t *testing.T) { |
| 3416 |
var v int64 |
| 3417 |
if err := stdjson.Unmarshal([]byte(`-0`), &v); err != nil { |
| 3418 |
t.Fatal(err) |
| 3419 |
} |
| 3420 |
if err := json.Unmarshal([]byte(`-0`), &v); err != nil { |
| 3421 |
t.Fatal(err) |
| 3422 |
} |
| 3423 |
}) |
| 3424 |
t.Run("+0", func(t *testing.T) { |
| 3425 |
var v int64 |
| 3426 |
if err := stdjson.Unmarshal([]byte(`+0`), &v); err == nil { |
| 3427 |
t.Error("expected error") |
| 3428 |
} |
| 3429 |
if err := json.Unmarshal([]byte(`+0`), &v); err == nil { |
| 3430 |
t.Error("expected error") |
| 3431 |
} |
| 3432 |
}) |
| 3433 |
}) |
| 3434 |
t.Run("uint", func(t *testing.T) { |
| 3435 |
t.Run("-0", func(t *testing.T) { |
| 3436 |
var v uint64 |
| 3437 |
if err := stdjson.Unmarshal([]byte(`-0`), &v); err == nil { |
| 3438 |
t.Error("expected error") |
| 3439 |
} |
| 3440 |
if err := json.Unmarshal([]byte(`-0`), &v); err == nil { |
| 3441 |
t.Error("expected error") |
| 3442 |
} |
| 3443 |
}) |
| 3444 |
t.Run("+0", func(t *testing.T) { |
| 3445 |
var v uint64 |
| 3446 |
if err := stdjson.Unmarshal([]byte(`+0`), &v); err == nil { |
| 3447 |
t.Error("expected error") |
| 3448 |
} |
| 3449 |
if err := json.Unmarshal([]byte(`+0`), &v); err == nil { |
| 3450 |
t.Error("expected error") |
| 3451 |
} |
| 3452 |
}) |
| 3453 |
}) |
| 3454 |
t.Run("float", func(t *testing.T) { |
| 3455 |
t.Run("0.0", func(t *testing.T) { |
| 3456 |
var f float64 |
| 3457 |
if err := stdjson.Unmarshal([]byte(`0.0`), &f); err != nil { |
| 3458 |
t.Fatal(err) |
| 3459 |
} |
| 3460 |
if err := json.Unmarshal([]byte(`0.0`), &f); err != nil { |
| 3461 |
t.Fatal(err) |
| 3462 |
} |
| 3463 |
}) |
| 3464 |
t.Run("0.000000000", func(t *testing.T) { |
| 3465 |
var f float64 |
| 3466 |
if err := stdjson.Unmarshal([]byte(`0.000000000`), &f); err != nil { |
| 3467 |
t.Fatal(err) |
| 3468 |
} |
| 3469 |
if err := json.Unmarshal([]byte(`0.000000000`), &f); err != nil { |
| 3470 |
t.Fatal(err) |
| 3471 |
} |
| 3472 |
}) |
| 3473 |
t.Run("repeat zero a lot with float value", func(t *testing.T) { |
| 3474 |
var f float64 |
| 3475 |
if err := stdjson.Unmarshal([]byte("0."+strings.Repeat("0", 30)), &f); err != nil { |
| 3476 |
t.Fatal(err) |
| 3477 |
} |
| 3478 |
if err := json.Unmarshal([]byte("0."+strings.Repeat("0", 30)), &f); err != nil { |
| 3479 |
t.Fatal(err) |
| 3480 |
} |
| 3481 |
}) |
| 3482 |
}) |
| 3483 |
}) |
| 3484 |
} |
| 3485 |
|
| 3486 |
type someInterface interface { |
| 3487 |
DoesNotMatter() |
| 3488 |
} |
| 3489 |
|
| 3490 |
func TestDecodeUnknownInterface(t *testing.T) { |
| 3491 |
t.Run("unmarshal", func(t *testing.T) { |
| 3492 |
var v map[string]someInterface |
| 3493 |
if err := json.Unmarshal([]byte(`{"a":null,"b":null}`), &v); err != nil { |
| 3494 |
t.Fatal(err) |
| 3495 |
} |
| 3496 |
if len(v) != 2 { |
| 3497 |
t.Fatalf("failed to decode: %v", v) |
| 3498 |
} |
| 3499 |
if a, exists := v["a"]; a != nil || !exists { |
| 3500 |
t.Fatalf("failed to decode: %v", v) |
| 3501 |
} |
| 3502 |
if b, exists := v["b"]; b != nil || !exists { |
| 3503 |
t.Fatalf("failed to decode: %v", v) |
| 3504 |
} |
| 3505 |
}) |
| 3506 |
t.Run("stream", func(t *testing.T) { |
| 3507 |
var v map[string]someInterface |
| 3508 |
if err := json.NewDecoder(strings.NewReader(`{"a":null,"b":null}`)).Decode(&v); err != nil { |
| 3509 |
t.Fatal(err) |
| 3510 |
} |
| 3511 |
if len(v) != 2 { |
| 3512 |
t.Fatalf("failed to decode: %v", v) |
| 3513 |
} |
| 3514 |
if a, exists := v["a"]; a != nil || !exists { |
| 3515 |
t.Fatalf("failed to decode: %v", v) |
| 3516 |
} |
| 3517 |
if b, exists := v["b"]; b != nil || !exists { |
| 3518 |
t.Fatalf("failed to decode: %v", v) |
| 3519 |
} |
| 3520 |
}) |
| 3521 |
} |
| 3522 |
|
| 3523 |
func TestDecodeByteSliceNull(t *testing.T) { |
| 3524 |
t.Run("unmarshal", func(t *testing.T) { |
| 3525 |
var v1 []byte |
| 3526 |
if err := stdjson.Unmarshal([]byte(`null`), &v1); err != nil { |
| 3527 |
t.Fatal(err) |
| 3528 |
} |
| 3529 |
var v2 []byte |
| 3530 |
if err := json.Unmarshal([]byte(`null`), &v2); err != nil { |
| 3531 |
t.Fatal(err) |
| 3532 |
} |
| 3533 |
if v1 == nil && v2 != nil || len(v1) != len(v2) { |
| 3534 |
t.Fatalf("failed to decode null to []byte. expected:%#v but got %#v", v1, v2) |
| 3535 |
} |
| 3536 |
}) |
| 3537 |
t.Run("stream", func(t *testing.T) { |
| 3538 |
var v1 []byte |
| 3539 |
if err := stdjson.NewDecoder(strings.NewReader(`null`)).Decode(&v1); err != nil { |
| 3540 |
t.Fatal(err) |
| 3541 |
} |
| 3542 |
var v2 []byte |
| 3543 |
if err := json.NewDecoder(strings.NewReader(`null`)).Decode(&v2); err != nil { |
| 3544 |
t.Fatal(err) |
| 3545 |
} |
| 3546 |
if v1 == nil && v2 != nil || len(v1) != len(v2) { |
| 3547 |
t.Fatalf("failed to decode null to []byte. expected:%#v but got %#v", v1, v2) |
| 3548 |
} |
| 3549 |
}) |
| 3550 |
} |
| 3551 |
|
| 3552 |
func TestDecodeBackSlash(t *testing.T) { |
| 3553 |
t.Run("unmarshal", func(t *testing.T) { |
| 3554 |
t.Run("string", func(t *testing.T) { |
| 3555 |
var v1 map[string]stdjson.RawMessage |
| 3556 |
if err := stdjson.Unmarshal([]byte(`{"c":"\\"}`), &v1); err != nil { |
| 3557 |
t.Fatal(err) |
| 3558 |
} |
| 3559 |
var v2 map[string]json.RawMessage |
| 3560 |
if err := json.Unmarshal([]byte(`{"c":"\\"}`), &v2); err != nil { |
| 3561 |
t.Fatal(err) |
| 3562 |
} |
| 3563 |
if len(v1) != len(v2) || !bytes.Equal(v1["c"], v2["c"]) { |
| 3564 |
t.Fatalf("failed to decode backslash: expected %#v but got %#v", v1, v2) |
| 3565 |
} |
| 3566 |
}) |
| 3567 |
t.Run("array", func(t *testing.T) { |
| 3568 |
var v1 map[string]stdjson.RawMessage |
| 3569 |
if err := stdjson.Unmarshal([]byte(`{"c":["\\"]}`), &v1); err != nil { |
| 3570 |
t.Fatal(err) |
| 3571 |
} |
| 3572 |
var v2 map[string]json.RawMessage |
| 3573 |
if err := json.Unmarshal([]byte(`{"c":["\\"]}`), &v2); err != nil { |
| 3574 |
t.Fatal(err) |
| 3575 |
} |
| 3576 |
if len(v1) != len(v2) || !bytes.Equal(v1["c"], v2["c"]) { |
| 3577 |
t.Fatalf("failed to decode backslash: expected %#v but got %#v", v1, v2) |
| 3578 |
} |
| 3579 |
}) |
| 3580 |
t.Run("object", func(t *testing.T) { |
| 3581 |
var v1 map[string]stdjson.RawMessage |
| 3582 |
if err := stdjson.Unmarshal([]byte(`{"c":{"\\":"\\"}}`), &v1); err != nil { |
| 3583 |
t.Fatal(err) |
| 3584 |
} |
| 3585 |
var v2 map[string]json.RawMessage |
| 3586 |
if err := json.Unmarshal([]byte(`{"c":{"\\":"\\"}}`), &v2); err != nil { |
| 3587 |
t.Fatal(err) |
| 3588 |
} |
| 3589 |
if len(v1) != len(v2) || !bytes.Equal(v1["c"], v2["c"]) { |
| 3590 |
t.Fatalf("failed to decode backslash: expected %#v but got %#v", v1, v2) |
| 3591 |
} |
| 3592 |
}) |
| 3593 |
}) |
| 3594 |
t.Run("stream", func(t *testing.T) { |
| 3595 |
t.Run("string", func(t *testing.T) { |
| 3596 |
var v1 map[string]stdjson.RawMessage |
| 3597 |
if err := stdjson.NewDecoder(strings.NewReader(`{"c":"\\"}`)).Decode(&v1); err != nil { |
| 3598 |
t.Fatal(err) |
| 3599 |
} |
| 3600 |
var v2 map[string]json.RawMessage |
| 3601 |
if err := json.NewDecoder(strings.NewReader(`{"c":"\\"}`)).Decode(&v2); err != nil { |
| 3602 |
t.Fatal(err) |
| 3603 |
} |
| 3604 |
if len(v1) != len(v2) || !bytes.Equal(v1["c"], v2["c"]) { |
| 3605 |
t.Fatalf("failed to decode backslash: expected %#v but got %#v", v1, v2) |
| 3606 |
} |
| 3607 |
}) |
| 3608 |
t.Run("array", func(t *testing.T) { |
| 3609 |
var v1 map[string]stdjson.RawMessage |
| 3610 |
if err := stdjson.NewDecoder(strings.NewReader(`{"c":["\\"]}`)).Decode(&v1); err != nil { |
| 3611 |
t.Fatal(err) |
| 3612 |
} |
| 3613 |
var v2 map[string]json.RawMessage |
| 3614 |
if err := json.NewDecoder(strings.NewReader(`{"c":["\\"]}`)).Decode(&v2); err != nil { |
| 3615 |
t.Fatal(err) |
| 3616 |
} |
| 3617 |
if len(v1) != len(v2) || !bytes.Equal(v1["c"], v2["c"]) { |
| 3618 |
t.Fatalf("failed to decode backslash: expected %#v but got %#v", v1, v2) |
| 3619 |
} |
| 3620 |
}) |
| 3621 |
t.Run("object", func(t *testing.T) { |
| 3622 |
var v1 map[string]stdjson.RawMessage |
| 3623 |
if err := stdjson.NewDecoder(strings.NewReader(`{"c":{"\\":"\\"}}`)).Decode(&v1); err != nil { |
| 3624 |
t.Fatal(err) |
| 3625 |
} |
| 3626 |
var v2 map[string]json.RawMessage |
| 3627 |
if err := json.NewDecoder(strings.NewReader(`{"c":{"\\":"\\"}}`)).Decode(&v2); err != nil { |
| 3628 |
t.Fatal(err) |
| 3629 |
} |
| 3630 |
if len(v1) != len(v2) || !bytes.Equal(v1["c"], v2["c"]) { |
| 3631 |
t.Fatalf("failed to decode backslash: expected %#v but got %#v", v1, v2) |
| 3632 |
} |
| 3633 |
}) |
| 3634 |
}) |
| 3635 |
} |
| 3636 |
|
| 3637 |
func TestIssue218(t *testing.T) { |
| 3638 |
type A struct { |
| 3639 |
X int |
| 3640 |
} |
| 3641 |
type B struct { |
| 3642 |
Y int |
| 3643 |
} |
| 3644 |
type S struct { |
| 3645 |
A *A `json:"a,omitempty"` |
| 3646 |
B *B `json:"b,omitempty"` |
| 3647 |
} |
| 3648 |
tests := []struct { |
| 3649 |
name string |
| 3650 |
given []S |
| 3651 |
expected []S |
| 3652 |
}{ |
| 3653 |
{ |
| 3654 |
name: "A should be correct", |
| 3655 |
given: []S{{ |
| 3656 |
A: &A{ |
| 3657 |
X: 1, |
| 3658 |
}, |
| 3659 |
}}, |
| 3660 |
expected: []S{{ |
| 3661 |
A: &A{ |
| 3662 |
X: 1, |
| 3663 |
}, |
| 3664 |
}}, |
| 3665 |
}, |
| 3666 |
{ |
| 3667 |
name: "B should be correct", |
| 3668 |
given: []S{{ |
| 3669 |
B: &B{ |
| 3670 |
Y: 2, |
| 3671 |
}, |
| 3672 |
}}, |
| 3673 |
expected: []S{{ |
| 3674 |
B: &B{ |
| 3675 |
Y: 2, |
| 3676 |
}, |
| 3677 |
}}, |
| 3678 |
}, |
| 3679 |
} |
| 3680 |
for _, test := range tests { |
| 3681 |
test := test |
| 3682 |
t.Run(test.name, func(t *testing.T) { |
| 3683 |
var buf bytes.Buffer |
| 3684 |
if err := json.NewEncoder(&buf).Encode(test.given); err != nil { |
| 3685 |
t.Fatal(err) |
| 3686 |
} |
| 3687 |
var actual []S |
| 3688 |
if err := json.NewDecoder(bytes.NewReader(buf.Bytes())).Decode(&actual); err != nil { |
| 3689 |
t.Fatal(err) |
| 3690 |
} |
| 3691 |
if !reflect.DeepEqual(test.expected, actual) { |
| 3692 |
t.Fatalf("mismatch value: expected %v but got %v", test.expected, actual) |
| 3693 |
} |
| 3694 |
}) |
| 3695 |
} |
| 3696 |
} |
| 3697 |
|
| 3698 |
func TestDecodeEscapedCharField(t *testing.T) { |
| 3699 |
b := []byte(`{"\u6D88\u606F":"\u6D88\u606F"}`) |
| 3700 |
t.Run("unmarshal", func(t *testing.T) { |
| 3701 |
v := struct { |
| 3702 |
Msg string `json:"消息"` |
| 3703 |
}{} |
| 3704 |
if err := json.Unmarshal(b, &v); err != nil { |
| 3705 |
t.Fatal(err) |
| 3706 |
} |
| 3707 |
if !bytes.Equal([]byte(v.Msg), []byte("消息")) { |
| 3708 |
t.Fatal("failed to decode unicode char") |
| 3709 |
} |
| 3710 |
}) |
| 3711 |
t.Run("stream", func(t *testing.T) { |
| 3712 |
v := struct { |
| 3713 |
Msg string `json:"消息"` |
| 3714 |
}{} |
| 3715 |
if err := json.NewDecoder(bytes.NewBuffer(b)).Decode(&v); err != nil { |
| 3716 |
t.Fatal(err) |
| 3717 |
} |
| 3718 |
if !bytes.Equal([]byte(v.Msg), []byte("消息")) { |
| 3719 |
t.Fatal("failed to decode unicode char") |
| 3720 |
} |
| 3721 |
}) |
| 3722 |
} |
| 3723 |
|
| 3724 |
type unmarshalContextKey struct{} |
| 3725 |
|
| 3726 |
type unmarshalContextStructType struct { |
| 3727 |
v int |
| 3728 |
} |
| 3729 |
|
| 3730 |
func (t *unmarshalContextStructType) UnmarshalJSON(ctx context.Context, b []byte) error { |
| 3731 |
v := ctx.Value(unmarshalContextKey{}) |
| 3732 |
s, ok := v.(string) |
| 3733 |
if !ok { |
| 3734 |
return fmt.Errorf("failed to propagate parent context.Context") |
| 3735 |
} |
| 3736 |
if s != "hello" { |
| 3737 |
return fmt.Errorf("failed to propagate parent context.Context") |
| 3738 |
} |
| 3739 |
t.v = 100 |
| 3740 |
return nil |
| 3741 |
} |
| 3742 |
|
| 3743 |
func TestDecodeContextOption(t *testing.T) { |
| 3744 |
src := []byte("10") |
| 3745 |
buf := bytes.NewBuffer(src) |
| 3746 |
|
| 3747 |
t.Run("UnmarshalContext", func(t *testing.T) { |
| 3748 |
ctx := context.WithValue(context.Background(), unmarshalContextKey{}, "hello") |
| 3749 |
var v unmarshalContextStructType |
| 3750 |
if err := json.UnmarshalContext(ctx, src, &v); err != nil { |
| 3751 |
t.Fatal(err) |
| 3752 |
} |
| 3753 |
if v.v != 100 { |
| 3754 |
t.Fatal("failed to decode with context") |
| 3755 |
} |
| 3756 |
}) |
| 3757 |
t.Run("DecodeContext", func(t *testing.T) { |
| 3758 |
ctx := context.WithValue(context.Background(), unmarshalContextKey{}, "hello") |
| 3759 |
var v unmarshalContextStructType |
| 3760 |
if err := json.NewDecoder(buf).DecodeContext(ctx, &v); err != nil { |
| 3761 |
t.Fatal(err) |
| 3762 |
} |
| 3763 |
if v.v != 100 { |
| 3764 |
t.Fatal("failed to decode with context") |
| 3765 |
} |
| 3766 |
}) |
| 3767 |
} |
| 3768 |
|
| 3769 |
func TestIssue251(t *testing.T) { |
| 3770 |
array := [3]int{1, 2, 3} |
| 3771 |
err := stdjson.Unmarshal([]byte("[ ]"), &array) |
| 3772 |
if err != nil { |
| 3773 |
t.Fatal(err) |
| 3774 |
} |
| 3775 |
t.Log(array) |
| 3776 |
|
| 3777 |
array = [3]int{1, 2, 3} |
| 3778 |
err = json.Unmarshal([]byte("[ ]"), &array) |
| 3779 |
if err != nil { |
| 3780 |
t.Fatal(err) |
| 3781 |
} |
| 3782 |
t.Log(array) |
| 3783 |
|
| 3784 |
array = [3]int{1, 2, 3} |
| 3785 |
err = json.NewDecoder(strings.NewReader(`[ ]`)).Decode(&array) |
| 3786 |
if err != nil { |
| 3787 |
t.Fatal(err) |
| 3788 |
} |
| 3789 |
t.Log(array) |
| 3790 |
} |
| 3791 |
|
| 3792 |
func TestDecodeBinaryTypeWithEscapedChar(t *testing.T) { |
| 3793 |
type T struct { |
| 3794 |
Msg []byte `json:"msg"` |
| 3795 |
} |
| 3796 |
content := []byte(`{"msg":"aGVsbG8K\n"}`) |
| 3797 |
t.Run("unmarshal", func(t *testing.T) { |
| 3798 |
var expected T |
| 3799 |
if err := stdjson.Unmarshal(content, &expected); err != nil { |
| 3800 |
t.Fatal(err) |
| 3801 |
} |
| 3802 |
var got T |
| 3803 |
if err := json.Unmarshal(content, &got); err != nil { |
| 3804 |
t.Fatal(err) |
| 3805 |
} |
| 3806 |
if !bytes.Equal(expected.Msg, got.Msg) { |
| 3807 |
t.Fatalf("failed to decode binary type with escaped char. expected %q but got %q", expected.Msg, got.Msg) |
| 3808 |
} |
| 3809 |
}) |
| 3810 |
t.Run("stream", func(t *testing.T) { |
| 3811 |
var expected T |
| 3812 |
if err := stdjson.NewDecoder(bytes.NewBuffer(content)).Decode(&expected); err != nil { |
| 3813 |
t.Fatal(err) |
| 3814 |
} |
| 3815 |
var got T |
| 3816 |
if err := json.NewDecoder(bytes.NewBuffer(content)).Decode(&got); err != nil { |
| 3817 |
t.Fatal(err) |
| 3818 |
} |
| 3819 |
if !bytes.Equal(expected.Msg, got.Msg) { |
| 3820 |
t.Fatalf("failed to decode binary type with escaped char. expected %q but got %q", expected.Msg, got.Msg) |
| 3821 |
} |
| 3822 |
}) |
| 3823 |
} |
| 3824 |
|
| 3825 |
func TestIssue282(t *testing.T) { |
| 3826 |
var J = []byte(`{ |
| 3827 |
"a": {}, |
| 3828 |
"b": {}, |
| 3829 |
"c": {}, |
| 3830 |
"d": {}, |
| 3831 |
"e": {}, |
| 3832 |
"f": {}, |
| 3833 |
"g": {}, |
| 3834 |
"h": { |
| 3835 |
"m": "1" |
| 3836 |
}, |
| 3837 |
"i": {} |
| 3838 |
}`) |
| 3839 |
|
| 3840 |
type T4 struct { |
| 3841 |
F0 string |
| 3842 |
F1 string |
| 3843 |
F2 string |
| 3844 |
F3 string |
| 3845 |
F4 string |
| 3846 |
F5 string |
| 3847 |
F6 int |
| 3848 |
} |
| 3849 |
type T3 struct { |
| 3850 |
F0 string |
| 3851 |
F1 T4 |
| 3852 |
} |
| 3853 |
type T2 struct { |
| 3854 |
F0 string `json:"m"` |
| 3855 |
F1 T3 |
| 3856 |
} |
| 3857 |
type T0 map[string]T2 |
| 3858 |
|
| 3859 |
// T2 size is 136 bytes. This is indirect type. |
| 3860 |
var v T0 |
| 3861 |
if err := json.Unmarshal(J, &v); err != nil { |
| 3862 |
t.Fatal(err) |
| 3863 |
} |
| 3864 |
if v["h"].F0 != "1" { |
| 3865 |
t.Fatalf("failed to assign map value") |
| 3866 |
} |
| 3867 |
} |
| 3868 |
|
| 3869 |
func TestDecodeStructFieldMap(t *testing.T) { |
| 3870 |
type Foo struct { |
| 3871 |
Bar map[float64]float64 `json:"bar,omitempty"` |
| 3872 |
} |
| 3873 |
var v Foo |
| 3874 |
if err := json.Unmarshal([]byte(`{"name":"test"}`), &v); err != nil { |
| 3875 |
t.Fatal(err) |
| 3876 |
} |
| 3877 |
if v.Bar != nil { |
| 3878 |
t.Fatalf("failed to decode v.Bar = %+v", v.Bar) |
| 3879 |
} |
| 3880 |
} |
| 3881 |
|
| 3882 |
type issue303 struct { |
| 3883 |
Count int |
| 3884 |
Type string |
| 3885 |
Value any |
| 3886 |
} |
| 3887 |
|
| 3888 |
func (t *issue303) UnmarshalJSON(b []byte) error { |
| 3889 |
type tmpType issue303 |
| 3890 |
|
| 3891 |
wrapped := struct { |
| 3892 |
Value json.RawMessage |
| 3893 |
tmpType |
| 3894 |
}{} |
| 3895 |
if err := json.Unmarshal(b, &wrapped); err != nil { |
| 3896 |
return err |
| 3897 |
} |
| 3898 |
*t = issue303(wrapped.tmpType) |
| 3899 |
|
| 3900 |
switch wrapped.Type { |
| 3901 |
case "string": |
| 3902 |
var str string |
| 3903 |
if err := json.Unmarshal(wrapped.Value, &str); err != nil { |
| 3904 |
return err |
| 3905 |
} |
| 3906 |
t.Value = str |
| 3907 |
} |
| 3908 |
return nil |
| 3909 |
} |
| 3910 |
|
| 3911 |
func TestIssue303(t *testing.T) { |
| 3912 |
var v issue303 |
| 3913 |
if err := json.Unmarshal([]byte(`{"Count":7,"Type":"string","Value":"hello"}`), &v); err != nil { |
| 3914 |
t.Fatal(err) |
| 3915 |
} |
| 3916 |
if v.Count != 7 || v.Type != "string" || v.Value != "hello" { |
| 3917 |
t.Fatalf("failed to decode. count = %d type = %s value = %v", v.Count, v.Type, v.Value) |
| 3918 |
} |
| 3919 |
} |
| 3920 |
|
| 3921 |
func TestIssue327(t *testing.T) { |
| 3922 |
var v struct { |
| 3923 |
Date time.Time `json:"date"` |
| 3924 |
} |
| 3925 |
dec := json.NewDecoder(strings.NewReader(`{"date": "2021-11-23T13:47:30+01:00"})`)) |
| 3926 |
if err := dec.DecodeContext(context.Background(), &v); err != nil { |
| 3927 |
t.Fatal(err) |
| 3928 |
} |
| 3929 |
expected := "2021-11-23T13:47:30+01:00" |
| 3930 |
if got := v.Date.Format(time.RFC3339); got != expected { |
| 3931 |
t.Fatalf("failed to decode. expected %q but got %q", expected, got) |
| 3932 |
} |
| 3933 |
} |
| 3934 |
|
| 3935 |
func TestIssue337(t *testing.T) { |
| 3936 |
in := strings.Repeat(" ", 510) + "{}" |
| 3937 |
var m map[string]string |
| 3938 |
if err := json.NewDecoder(strings.NewReader(in)).Decode(&m); err != nil { |
| 3939 |
t.Fatal("unexpected error:", err) |
| 3940 |
} |
| 3941 |
if len(m) != 0 { |
| 3942 |
t.Fatal("unexpected result", m) |
| 3943 |
} |
| 3944 |
} |
| 3945 |
|
| 3946 |
func Benchmark306(b *testing.B) { |
| 3947 |
type T0 struct { |
| 3948 |
Str string |
| 3949 |
} |
| 3950 |
in := []byte(`{"Str":"` + strings.Repeat(`abcd\"`, 10000) + `"}`) |
| 3951 |
b.Run("stdjson", func(b *testing.B) { |
| 3952 |
var x T0 |
| 3953 |
for i := 0; i < b.N; i++ { |
| 3954 |
stdjson.Unmarshal(in, &x) |
| 3955 |
} |
| 3956 |
}) |
| 3957 |
b.Run("go-json", func(b *testing.B) { |
| 3958 |
var x T0 |
| 3959 |
for i := 0; i < b.N; i++ { |
| 3960 |
json.Unmarshal(in, &x) |
| 3961 |
} |
| 3962 |
}) |
| 3963 |
} |
| 3964 |
|
| 3965 |
func TestIssue348(t *testing.T) { |
| 3966 |
in := strings.Repeat("["+strings.Repeat(",1000", 500)[1:]+"]", 2) |
| 3967 |
dec := json.NewDecoder(strings.NewReader(in)) |
| 3968 |
for dec.More() { |
| 3969 |
var foo any |
| 3970 |
if err := dec.Decode(&foo); err != nil { |
| 3971 |
t.Error(err) |
| 3972 |
} |
| 3973 |
} |
| 3974 |
} |
| 3975 |
|
| 3976 |
type issue342 string |
| 3977 |
|
| 3978 |
func (t *issue342) UnmarshalJSON(b []byte) error { |
| 3979 |
panic("unreachable") |
| 3980 |
} |
| 3981 |
|
| 3982 |
func TestIssue342(t *testing.T) { |
| 3983 |
var v map[issue342]int |
| 3984 |
in := []byte(`{"a":1}`) |
| 3985 |
if err := json.Unmarshal(in, &v); err != nil { |
| 3986 |
t.Errorf("unexpected error: %v", err) |
| 3987 |
} |
| 3988 |
expected := 1 |
| 3989 |
if got := v["a"]; got != expected { |
| 3990 |
t.Errorf("unexpected result: got(%v) != expected(%v)", got, expected) |
| 3991 |
} |
| 3992 |
} |
| 3993 |
|
| 3994 |
func TestIssue360(t *testing.T) { |
| 3995 |
var uints []uint8 |
| 3996 |
err := json.Unmarshal([]byte(`[0, 1, 2]`), &uints) |
| 3997 |
if err != nil { |
| 3998 |
t.Errorf("unexpected error: %v", err) |
| 3999 |
} |
| 4000 |
if len(uints) != 3 || !(uints[0] == 0 && uints[1] == 1 && uints[2] == 2) { |
| 4001 |
t.Errorf("unexpected result: %v", uints) |
| 4002 |
} |
| 4003 |
} |
| 4004 |
|
| 4005 |
func TestIssue359(t *testing.T) { |
| 4006 |
var a any = 1 |
| 4007 |
var b any = &a |
| 4008 |
var c any = &b |
| 4009 |
v, err := json.Marshal(c) |
| 4010 |
if err != nil { |
| 4011 |
t.Errorf("unexpected error: %v", err) |
| 4012 |
} |
| 4013 |
if string(v) != "1" { |
| 4014 |
t.Errorf("unexpected result: %v", string(v)) |
| 4015 |
} |
| 4016 |
} |
| 4017 |
|
| 4018 |
func TestIssue364(t *testing.T) { |
| 4019 |
var v struct { |
| 4020 |
Description string `json:"description"` |
| 4021 |
} |
| 4022 |
err := json.Unmarshal([]byte(`{"description":"\uD83D\uDE87 Toledo is a metro station"}`), &v) |
| 4023 |
if err != nil { |
| 4024 |
t.Errorf("unexpected error: %v", err) |
| 4025 |
} |
| 4026 |
if v.Description != "🚇 Toledo is a metro station" { |
| 4027 |
t.Errorf("unexpected result: %v", v.Description) |
| 4028 |
} |
| 4029 |
} |
| 4030 |
|
| 4031 |
func TestIssue362(t *testing.T) { |
| 4032 |
type AliasedPrimitive int |
| 4033 |
type Combiner struct { |
| 4034 |
SomeField int |
| 4035 |
AliasedPrimitive |
| 4036 |
} |
| 4037 |
originalCombiner := Combiner{AliasedPrimitive: 7} |
| 4038 |
b, err := json.Marshal(originalCombiner) |
| 4039 |
assertErr(t, err) |
| 4040 |
newCombiner := Combiner{} |
| 4041 |
err = json.Unmarshal(b, &newCombiner) |
| 4042 |
assertErr(t, err) |
| 4043 |
assertEq(t, "TestEmbeddedPrimitiveAlias", originalCombiner, newCombiner) |
| 4044 |
} |
| 4045 |
|
| 4046 |
func TestIssue335(t *testing.T) { |
| 4047 |
var v []string |
| 4048 |
in := []byte(`["\u","A"]`) |
| 4049 |
err := json.Unmarshal(in, &v) |
| 4050 |
if err == nil { |
| 4051 |
t.Errorf("unexpected success") |
| 4052 |
} |
| 4053 |
} |
| 4054 |
|
| 4055 |
func TestIssue372(t *testing.T) { |
| 4056 |
type A int |
| 4057 |
type T struct { |
| 4058 |
_ int |
| 4059 |
*A |
| 4060 |
} |
| 4061 |
var v T |
| 4062 |
err := json.Unmarshal([]byte(`{"A":7}`), &v) |
| 4063 |
assertErr(t, err) |
| 4064 |
|
| 4065 |
got := *v.A |
| 4066 |
expected := A(7) |
| 4067 |
if got != expected { |
| 4068 |
t.Errorf("unexpected result: %v != %v", got, expected) |
| 4069 |
} |
| 4070 |
} |
| 4071 |
|
| 4072 |
type issue384 struct{} |
| 4073 |
|
| 4074 |
func (t *issue384) UnmarshalJSON(b []byte) error { |
| 4075 |
return nil |
| 4076 |
} |
| 4077 |
|
| 4078 |
func TestIssue384(t *testing.T) { |
| 4079 |
testcases := []string{ |
| 4080 |
`{"data": "` + strings.Repeat("-", 500) + `\""}`, |
| 4081 |
`["` + strings.Repeat("-", 508) + `\""]`, |
| 4082 |
} |
| 4083 |
for _, tc := range testcases { |
| 4084 |
dec := json.NewDecoder(strings.NewReader(tc)) |
| 4085 |
var v issue384 |
| 4086 |
if err := dec.Decode(&v); err != nil { |
| 4087 |
t.Errorf("unexpected error: %v", err) |
| 4088 |
} |
| 4089 |
} |
| 4090 |
} |
| 4091 |
|
| 4092 |
func TestIssue408(t *testing.T) { |
| 4093 |
type T struct { |
| 4094 |
Arr [2]int32 `json:"arr"` |
| 4095 |
} |
| 4096 |
var v T |
| 4097 |
if err := json.Unmarshal([]byte(`{"arr": [1,2]}`), &v); err != nil { |
| 4098 |
t.Fatal(err) |
| 4099 |
} |
| 4100 |
} |
| 4101 |
|
| 4102 |
func TestIssue416(t *testing.T) { |
| 4103 |
b := []byte(`{"Сообщение":"Текст"}`) |
| 4104 |
|
| 4105 |
type T struct { |
| 4106 |
Msg string `json:"Сообщение"` |
| 4107 |
} |
| 4108 |
var x T |
| 4109 |
err := json.Unmarshal(b, &x) |
| 4110 |
assertErr(t, err) |
| 4111 |
assertEq(t, "unexpected result", "Текст", x.Msg) |
| 4112 |
} |
| 4113 |
|
| 4114 |
func TestIssue429(t *testing.T) { |
| 4115 |
var x struct { |
| 4116 |
N int32 |
| 4117 |
} |
| 4118 |
for _, b := range []string{ |
| 4119 |
`{"\u"`, |
| 4120 |
`{"\u0"`, |
| 4121 |
`{"\u00"`, |
| 4122 |
} { |
| 4123 |
if err := json.Unmarshal([]byte(b), &x); err == nil { |
| 4124 |
t.Errorf("unexpected success") |
| 4125 |
} |
| 4126 |
} |
| 4127 |
} |
| 4128 |
|
| 4129 |
func TestUnmarshalEndOfInputConcurrently(t *testing.T) { |
| 4130 |
// The end of the input is validated in the buffer of the context before the context is reused by another call. |
| 4131 |
var wg sync.WaitGroup |
| 4132 |
for g := 0; g < 8; g++ { |
| 4133 |
wg.Add(1) |
| 4134 |
go func(g int) { |
| 4135 |
defer wg.Done() |
| 4136 |
for i := 0; i < 2000; i++ { |
| 4137 |
var v int |
| 4138 |
if g%2 == 0 { |
| 4139 |
if err := json.Unmarshal([]byte(`1`), &v); err != nil { |
| 4140 |
t.Errorf("valid input: %v", err) |
| 4141 |
return |
| 4142 |
} |
| 4143 |
} else if err := json.Unmarshal([]byte(`1 x`), &v); err == nil { |
| 4144 |
t.Error("invalid input: no error") |
| 4145 |
return |
| 4146 |
} |
| 4147 |
} |
| 4148 |
}(g) |
| 4149 |
} |
| 4150 |
wg.Wait() |
| 4151 |
} |
| 4152 |
|
| 4153 |
func TestUnmarshalIntoSetInterface(t *testing.T) { |
| 4154 |
// A value of interface{} which is set is decoded into what it points to if it is a pointer to a value which is |
| 4155 |
// not an interface value, and is replaced else, as encoding/json does. |
| 4156 |
type inner struct{ A, B int } |
| 4157 |
var nilInner *inner |
| 4158 |
for _, doc := range []string{`{"A":1}`, `null`, `[1,2]`, `"s"`, ` {"B":2} `} { |
| 4159 |
for i, newValue := range []func() any{ |
| 4160 |
func() any { return nil }, |
| 4161 |
func() any { return &inner{A: 9, B: 8} }, |
| 4162 |
func() any { return nilInner }, |
| 4163 |
func() any { return inner{A: 9} }, |
| 4164 |
func() any { return 1.5 }, |
| 4165 |
func() any { return map[string]any{"x": 1.0} }, |
| 4166 |
} { |
| 4167 |
want, got := newValue(), newValue() |
| 4168 |
wantErr := stdjson.Unmarshal([]byte(doc), &want) |
| 4169 |
gotErr := json.Unmarshal([]byte(doc), &got) |
| 4170 |
if (wantErr == nil) != (gotErr == nil) { |
| 4171 |
t.Errorf("%s into value %d: got error %v, want %v", doc, i, gotErr, wantErr) |
| 4172 |
continue |
| 4173 |
} |
| 4174 |
if !reflect.DeepEqual(got, want) { |
| 4175 |
t.Errorf("%s into value %d: got %#v, want %#v", doc, i, got, want) |
| 4176 |
} |
| 4177 |
} |
| 4178 |
} |
| 4179 |
} |
| 4180 |
|
| 4181 |
func TestUnmarshalIntoInterfaceValuesOfTypes(t *testing.T) { |
| 4182 |
// How a value held by an interface value is decoded depends on its type, which is found once for a type: the |
| 4183 |
// values of the types which alternate in one field are each decoded as their type is. |
| 4184 |
type inner struct{ A, B int } |
| 4185 |
type other struct{ C string } |
| 4186 |
docs := []string{`{"A":1,"C":"c"}`, `null`, `"text"`, `[1]`} |
| 4187 |
anyValues := []func() any{ |
| 4188 |
func() any { return &inner{A: 9} }, |
| 4189 |
func() any { return &other{C: "x"} }, |
| 4190 |
func() any { return inner{A: 9} }, |
| 4191 |
func() any { return 1.5 }, |
| 4192 |
func() any { return &inner{B: 7} }, |
| 4193 |
} |
| 4194 |
for round := 0; round < 2; round++ { |
| 4195 |
for _, doc := range docs { |
| 4196 |
for i, newValue := range anyValues { |
| 4197 |
want := struct{ V any }{newValue()} |
| 4198 |
got := struct{ V any }{newValue()} |
| 4199 |
wantErr := stdjson.Unmarshal([]byte(`{"V":`+doc+`}`), &want) |
| 4200 |
gotErr := json.Unmarshal([]byte(`{"V":`+doc+`}`), &got) |
| 4201 |
if (wantErr == nil) != (gotErr == nil) { |
| 4202 |
t.Errorf("%s into any value %d: got error %v, want %v", doc, i, gotErr, wantErr) |
| 4203 |
continue |
| 4204 |
} |
| 4205 |
if !reflect.DeepEqual(got, want) { |
| 4206 |
t.Errorf("%s into any value %d: got %#v, want %#v", doc, i, got.V, want.V) |
| 4207 |
} |
| 4208 |
} |
| 4209 |
} |
| 4210 |
} |
| 4211 |
} |