| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
stdjson "encoding/json" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"math/rand" |
| 8 |
"reflect" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
|
| 13 |
json "github.com/goccy/go-json" |
| 14 |
) |
| 15 |
|
| 16 |
// The cases of the reported issues of the decoder: go-json decodes them as encoding/json of the running Go does. |
| 17 |
|
| 18 |
// checkUnmarshal decodes doc into the values which newValue makes by go-json and by encoding/json, and compares |
| 19 |
// the errors and the decoded values. |
| 20 |
func checkUnmarshal(t *testing.T, doc string, newValue func() any) { |
| 21 |
t.Helper() |
| 22 |
want, got := newValue(), newValue() |
| 23 |
wantErr := describeTypeError(stdjson.Unmarshal([]byte(doc), want)) |
| 24 |
gotErr := describeTypeError(json.Unmarshal([]byte(doc), got)) |
| 25 |
if gotErr != wantErr { |
| 26 |
t.Errorf("%q:\n got %s\nwant %s", doc, gotErr, wantErr) |
| 27 |
return |
| 28 |
} |
| 29 |
if wantErr == "SyntaxError" { |
| 30 |
return |
| 31 |
} |
| 32 |
// a NaN is not equal to itself: the values are the same if they are printed the same |
| 33 |
if !reflect.DeepEqual(got, want) && describeValue(got) != describeValue(want) { |
| 34 |
t.Errorf("%q: decoded %s, want %s", doc, describeValue(got), describeValue(want)) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
// describeValue prints the value which v points to, with the length of the strings, so that a string whose bytes |
| 39 |
// are not what its length says is seen. |
| 40 |
func describeValue(v any) string { |
| 41 |
return fmt.Sprintf("%#v", reflect.ValueOf(v).Elem().Interface()) |
| 42 |
} |
| 43 |
|
| 44 |
// issue642Pair is a struct whose pointer implements encoding.TextUnmarshaler. |
| 45 |
type issue642Pair struct{ A, B int } |
| 46 |
|
| 47 |
func (p *issue642Pair) UnmarshalText(b []byte) error { |
| 48 |
p.A, p.B = len(b), -len(b) |
| 49 |
return nil |
| 50 |
} |
| 51 |
|
| 52 |
// issue642Name is a string whose pointer implements encoding.TextUnmarshaler. |
| 53 |
type issue642Name string |
| 54 |
|
| 55 |
func (n *issue642Name) UnmarshalText(b []byte) error { |
| 56 |
*n = issue642Name("text:" + string(b)) |
| 57 |
return nil |
| 58 |
} |
| 59 |
|
| 60 |
// issue642List is a slice whose pointer implements encoding.TextUnmarshaler: null sets it to nil, as it does to |
| 61 |
// any slice. |
| 62 |
type issue642List []string |
| 63 |
|
| 64 |
func (l *issue642List) UnmarshalText(b []byte) error { |
| 65 |
*l = issue642List{string(b)} |
| 66 |
return nil |
| 67 |
} |
| 68 |
|
| 69 |
// issue642Map is a map whose pointer implements encoding.TextUnmarshaler: null sets it to nil. |
| 70 |
type issue642Map map[string]int |
| 71 |
|
| 72 |
func (m *issue642Map) UnmarshalText(b []byte) error { |
| 73 |
*m = issue642Map{string(b): 1} |
| 74 |
return nil |
| 75 |
} |
| 76 |
|
| 77 |
func TestIssue642NullIntoTextUnmarshaler(t *testing.T) { |
| 78 |
// null is not given to UnmarshalText: a value of a type whose pointer implements encoding.TextUnmarshaler is |
| 79 |
// left as it is by null, but a slice, a map, a pointer or an interface value is set to nil. |
| 80 |
type fields struct { |
| 81 |
P issue642Pair |
| 82 |
N issue642Name |
| 83 |
L issue642List |
| 84 |
M issue642Map |
| 85 |
PP *issue642Pair |
| 86 |
PN *issue642Name |
| 87 |
} |
| 88 |
full := func() fields { |
| 89 |
pair, name := issue642Pair{7, 8}, issue642Name("keep") |
| 90 |
return fields{ |
| 91 |
P: issue642Pair{7, 8}, N: "keep", L: issue642List{"keep"}, M: issue642Map{"keep": 1}, |
| 92 |
PP: &pair, PN: &name, |
| 93 |
} |
| 94 |
} |
| 95 |
for _, doc := range []string{`null`, ` null `, `"x"`} { |
| 96 |
checkUnmarshal(t, doc, func() any { v := issue642Pair{7, 8}; return &v }) |
| 97 |
checkUnmarshal(t, doc, func() any { v := issue642Name("keep"); return &v }) |
| 98 |
checkUnmarshal(t, doc, func() any { v := issue642List{"keep"}; return &v }) |
| 99 |
checkUnmarshal(t, doc, func() any { v := issue642Map{"keep": 1}; return &v }) |
| 100 |
} |
| 101 |
for _, doc := range []string{ |
| 102 |
`{"P":null,"N":null,"L":null,"M":null,"PP":null,"PN":null}`, |
| 103 |
`{"P":"x","N":"x","L":"x","M":"x","PP":"x","PN":"x"}`, |
| 104 |
} { |
| 105 |
checkUnmarshal(t, doc, func() any { v := full(); return &v }) |
| 106 |
} |
| 107 |
for _, doc := range []string{`[null,"x",null]`} { |
| 108 |
checkUnmarshal(t, doc, func() any { return &[]issue642Pair{{7, 8}} }) |
| 109 |
checkUnmarshal(t, doc, func() any { return &[]issue642Name{"keep"} }) |
| 110 |
checkUnmarshal(t, doc, func() any { return &[3]issue642Name{"keep", "keep", "keep"} }) |
| 111 |
checkUnmarshal(t, doc, func() any { return &[3]issue642Pair{{7, 8}, {7, 8}, {7, 8}} }) |
| 112 |
} |
| 113 |
checkUnmarshal(t, `{"a":null,"b":"x"}`, func() any { return &map[string]issue642Name{"a": "keep"} }) |
| 114 |
checkUnmarshal(t, `{"a":null,"b":"x"}`, func() any { return &map[string]issue642Pair{"a": {7, 8}} }) |
| 115 |
} |
| 116 |
|
| 117 |
type decodeIssue450Embedded struct { |
| 118 |
Embedded string `json:"Embedded"` |
| 119 |
Other int |
| 120 |
} |
| 121 |
|
| 122 |
type decodeIssue450Struct struct { |
| 123 |
decodeIssue450Embedded |
| 124 |
Name string |
| 125 |
} |
| 126 |
|
| 127 |
type decodeIssue450PtrStruct struct { |
| 128 |
*decodeIssue450Embedded |
| 129 |
Name string |
| 130 |
} |
| 131 |
|
| 132 |
type decodeIssue450Inner struct{ Inner string } |
| 133 |
|
| 134 |
type decodeIssue450Outer struct{ decodeIssue450Inner } |
| 135 |
|
| 136 |
// DecodeIssue450Label is embedded as a value which is not a struct: it is a field of its name. |
| 137 |
type DecodeIssue450Label string |
| 138 |
|
| 139 |
type decodeIssue450Labeled struct { |
| 140 |
DecodeIssue450Label |
| 141 |
Visible string |
| 142 |
} |
| 143 |
|
| 144 |
func TestIssue450EmbeddedFieldOfItsName(t *testing.T) { |
| 145 |
// A field promoted from an embedded struct is decoded although it has the name of the embedded struct, which |
| 146 |
// is no key of its own. |
| 147 |
for _, doc := range []string{ |
| 148 |
`{"Embedded":"inside","Other":111,"Name":"outside"}`, |
| 149 |
`{"embedded":"inside"}`, |
| 150 |
} { |
| 151 |
checkUnmarshal(t, doc, func() any { return new(decodeIssue450Struct) }) |
| 152 |
checkUnmarshal(t, doc, func() any { return new(decodeIssue450PtrStruct) }) |
| 153 |
} |
| 154 |
checkUnmarshal(t, `{"Inner":"x"}`, func() any { return new(decodeIssue450Outer) }) |
| 155 |
// an embedded value which is not a struct is a field of its name |
| 156 |
checkUnmarshal(t, `{"DecodeIssue450Label":"x","Visible":"v"}`, func() any { return new(decodeIssue450Labeled) }) |
| 157 |
} |
| 158 |
|
| 159 |
// numberGrammarCases are numbers which are not valid, and valid ones around them. |
| 160 |
var numberGrammarCases = []string{ |
| 161 |
`0.`, `1.`, `-2.`, `01`, `00`, `-01`, `-012`, `012`, `1.e5`, `0.e1`, `2.e+3`, `2.e-3`, `-.123`, `.5`, `-`, `-.`, |
| 162 |
`1e`, `1e+`, `1E-`, `1.5e`, `1..2`, `1e5.5`, `1ee5`, `1.2.3`, `--1`, `1-`, `-e1`, `1e+-2`, `0.e`, `+1`, |
| 163 |
`0`, `-0`, `0.5`, `-0.5`, `10`, `1e5`, `1E+5`, `1e-5`, `0e0`, `-0.0e-0`, `123.456e7`, |
| 164 |
} |
| 165 |
|
| 166 |
func TestIssue448NumberGrammar(t *testing.T) { |
| 167 |
// A number is checked by the grammar of JSON wherever it is: skipped, decoded into any of the types, or |
| 168 |
// validated. |
| 169 |
for _, n := range numberGrammarCases { |
| 170 |
for _, doc := range []string{n, `[` + n + `]`, `{"x":` + n + `}`, `{"skipped":` + n + `,"x":1}`, `[[` + n + `],1]`} { |
| 171 |
checkUnmarshal(t, doc, func() any { return new(any) }) |
| 172 |
checkUnmarshal(t, doc, func() any { return new(struct{}) }) |
| 173 |
checkUnmarshal(t, doc, func() any { return new(struct{ X any }) }) |
| 174 |
checkUnmarshal(t, doc, func() any { return new(struct{ X float64 }) }) |
| 175 |
checkUnmarshal(t, doc, func() any { return new(struct{ X int }) }) |
| 176 |
checkUnmarshal(t, doc, func() any { return new(map[string]any) }) |
| 177 |
checkUnmarshal(t, doc, func() any { return new([]any) }) |
| 178 |
if got, want := json.Valid([]byte(doc)), stdjson.Valid([]byte(doc)); got != want { |
| 179 |
t.Errorf("Valid(%q) = %v, want %v", doc, got, want) |
| 180 |
} |
| 181 |
} |
| 182 |
for _, newValue := range []func() any{ |
| 183 |
func() any { return new(float64) }, func() any { return new(float32) }, func() any { return new(int) }, |
| 184 |
func() any { return new(uint) }, func() any { return new(json.Number) }, |
| 185 |
func() any { return new(struct{ X json.Number }) }, |
| 186 |
func() any { |
| 187 |
return new(struct { |
| 188 |
X int `json:",string"` |
| 189 |
}) |
| 190 |
}, |
| 191 |
} { |
| 192 |
checkUnmarshal(t, n, newValue) |
| 193 |
checkUnmarshal(t, `{"X":`+n+`}`, newValue) |
| 194 |
checkUnmarshal(t, `{"X":"`+n+`"}`, newValue) |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func TestIssue555NumbersOutOfRange(t *testing.T) { |
| 200 |
// A number out of the range of a float64 decoded into an interface{} is a type error, after which the |
| 201 |
// decoding goes on, and a float field is set as encoding/json of the Go version sets it. |
| 202 |
huge := "1" + strings.Repeat("0", 400) |
| 203 |
for _, n := range []string{`1e400`, `-1e400`, huge, `1e39`, `-1e39`, `1e-400`} { |
| 204 |
for _, doc := range []string{n, `{"n":` + n + `,"m":1}`, `[` + n + `,1]`} { |
| 205 |
checkUnmarshal(t, doc, func() any { return new(any) }) |
| 206 |
checkUnmarshal(t, doc, func() any { return new(map[string]any) }) |
| 207 |
checkUnmarshal(t, doc, func() any { return new([]any) }) |
| 208 |
} |
| 209 |
checkUnmarshal(t, `{"F":`+n+`,"X":1}`, func() any { return new(struct{ F, X float64 }) }) |
| 210 |
checkUnmarshal(t, `{"F":`+n+`,"X":1}`, func() any { return new(struct{ F, X float32 }) }) |
| 211 |
checkUnmarshal(t, `{"F":`+n+`,"X":1}`, func() any { return new(struct{ F, X any }) }) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func TestStringOptionNumbers(t *testing.T) { |
| 216 |
// A number in a string of the string option is read by strconv, as encoding/json does: it may have leading |
| 217 |
// zeros, and a value which fails is not stored. |
| 218 |
type fields struct { |
| 219 |
I int `json:",string"` |
| 220 |
U uint8 `json:",string"` |
| 221 |
F float64 `json:",string"` |
| 222 |
F3 float32 `json:",string"` |
| 223 |
P *int `json:",string"` |
| 224 |
X int |
| 225 |
} |
| 226 |
for _, s := range []string{ |
| 227 |
`01`, `00`, `-01`, `+1`, `1-`, ` 1`, `1 `, `1.`, `.5`, `1e5`, `1.e5`, `-Inf`, `+Inf`, `Inf`, `NaN`, `0x10`, |
| 228 |
`1_000`, `300`, `-1`, `1e400`, `1e39`, ``, `-`, `1.5`, `99999999999999999999`, |
| 229 |
} { |
| 230 |
for _, name := range []string{"I", "U", "F", "F3", "P"} { |
| 231 |
checkUnmarshal(t, `{"`+name+`":"`+s+`","X":7}`, func() any { return new(fields) }) |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
func TestNumberMapKeys(t *testing.T) { |
| 237 |
// The keys of a map of numbers are read by strconv, as encoding/json reads them. |
| 238 |
for _, key := range []string{`01`, `+1`, `-1`, ` 1`, `1 `, `1.5`, `1e2`, `300`, `-Inf`, `0x10`, `1_000`, ``} { |
| 239 |
doc := `{"` + key + `":1,"2":2}` |
| 240 |
checkUnmarshal(t, doc, func() any { return new(map[int]int) }) |
| 241 |
checkUnmarshal(t, doc, func() any { return new(map[uint8]int) }) |
| 242 |
checkUnmarshal(t, doc, func() any { return new(map[float64]int) }) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
// tokenDecoder is the decoder of go-json and the one of encoding/json. |
| 247 |
type tokenDecoder interface { |
| 248 |
Token() (stdjson.Token, error) |
| 249 |
Decode(any) error |
| 250 |
More() bool |
| 251 |
InputOffset() int64 |
| 252 |
} |
| 253 |
|
| 254 |
// streamTokens reads the tokens of doc by Token and Decode by turns, as by the calls of pattern: 't' a Token, |
| 255 |
// 'd' a Decode into an interface{}, 'm' a More. |
| 256 |
func streamTokens(doc string, pattern string, newDecoder func(string) tokenDecoder) string { |
| 257 |
dec := newDecoder(doc) |
| 258 |
var b strings.Builder |
| 259 |
for i := 0; i < 40; i++ { |
| 260 |
switch pattern[i%len(pattern)] { |
| 261 |
case 't': |
| 262 |
tok, err := dec.Token() |
| 263 |
fmt.Fprintf(&b, "T(%v %v %d) ", tok, describeTypeErrorMessage(err), dec.InputOffset()) |
| 264 |
if err != nil { |
| 265 |
return b.String() |
| 266 |
} |
| 267 |
case 'd': |
| 268 |
var v any |
| 269 |
err := dec.Decode(&v) |
| 270 |
fmt.Fprintf(&b, "D(%v %v %d) ", v, describeTypeErrorMessage(err), dec.InputOffset()) |
| 271 |
if err != nil { |
| 272 |
return b.String() |
| 273 |
} |
| 274 |
case 'm': |
| 275 |
fmt.Fprintf(&b, "M(%v) ", dec.More()) |
| 276 |
} |
| 277 |
} |
| 278 |
return b.String() |
| 279 |
} |
| 280 |
|
| 281 |
// describeTypeErrorMessage describes an error with its message, and the offset of a syntax error. |
| 282 |
func describeTypeErrorMessage(err error) string { |
| 283 |
var std *stdjson.SyntaxError |
| 284 |
var goErr *json.SyntaxError |
| 285 |
switch { |
| 286 |
case err == nil: |
| 287 |
return "<nil>" |
| 288 |
case errors.As(err, &std): |
| 289 |
return fmt.Sprintf("syntax %q at %d", std.Error(), std.Offset) |
| 290 |
case errors.As(err, &goErr): |
| 291 |
return fmt.Sprintf("syntax %q at %d", goErr.Error(), goErr.Offset) |
| 292 |
} |
| 293 |
return err.Error() |
| 294 |
} |
| 295 |
|
| 296 |
func TestIssue548TokenGrammar(t *testing.T) { |
| 297 |
// The delimiters of the tokens are checked by the grammar, and a value decoded between the tokens follows the |
| 298 |
// comma or the colon before it, as encoding/json has them. |
| 299 |
stdDecoder := func(doc string) tokenDecoder { |
| 300 |
return stdjson.NewDecoder(strings.NewReader(doc)) |
| 301 |
} |
| 302 |
goDecoder := func(doc string) tokenDecoder { |
| 303 |
return json.NewDecoder(strings.NewReader(doc)) |
| 304 |
} |
| 305 |
for _, doc := range []string{ |
| 306 |
`{"hello": "value"` + "\n" + ` "foo": "bar"}`, |
| 307 |
`[1 2]`, `{"a" "b"}`, `{"a":1,}`, `[1,]`, `[,1]`, `{,"a":1}`, `{"a"::1}`, `{"a":1:2}`, `[1:2]`, `{"a",1}`, |
| 308 |
`{1:2}`, `{"a":1]`, `[1}`, `[1,,2]`, `{"a":1,,"b":2}`, `{"a":{"b":1}"c":2}`, `[[1][2]]`, `[true false]`, |
| 309 |
`{"a":1 "b":2}`, `]`, `}`, `,`, `:`, `1 , 2`, `1 2`, `{"a":[1,{"b":2}],"c":"d"} [3]`, |
| 310 |
`[{"a":1},{"a":2}]`, `{"a":{"b":[1,2]},"c":3}`, ``, `[`, `{"a"`, `{"a":`, `[1,`, |
| 311 |
`[01]`, `[1-2]`, `[1.]`, `[-]`, `[1e5e]`, `[tru]`, `[truex]`, `[nul,1]`, `["a\q"]`, `{"a\q":1}`, `[1.5 , 2]`, |
| 312 |
`01 2`, ` [ 1 , 2 ] `, "[\"a\x01\"]", `[1,2] x`, |
| 313 |
`[1 あ]`, `{"a" é:1}`, `{"a":1 é}`, `[1,]`, `{"a":[1,]}`, `[[1,]]`, `{"a":{"b":1,}}`, `[1,[}]`, `{"a":}`, `{"a":]`, |
| 314 |
} { |
| 315 |
for _, pattern := range []string{"t", "d", "td", "ttd", "tmd", "tttd", "tdt", "ttdd"} { |
| 316 |
want := streamTokens(doc, pattern, stdDecoder) |
| 317 |
got := streamTokens(doc, pattern, goDecoder) |
| 318 |
if got != want { |
| 319 |
t.Errorf("%q by %q:\n got %s\nwant %s", doc, pattern, got, want) |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
func TestValidByGrammar(t *testing.T) { |
| 326 |
// Valid is the grammar of JSON, as encoding/json has it. |
| 327 |
for _, doc := range []string{ |
| 328 |
`[1]]`, `1]`, `{}}`, `["x"]]`, `[-1e+9999]`, `[1.5e+9999]`, `[123123e100000]`, `0.4e0066999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006`, |
| 329 |
`123` + "\x00", "{\"a\":1}\x00", "\x00", ``, ` `, `null`, ` true `, `"a"`, `"\x"`, "\"\x01\"", `[`, `{"a"`, `{"a":1,}`, |
| 330 |
`[1,2,3]`, `{"a":{"b":[1,{"c":null}]}}`, `01`, `1.`, `-`, "\"\xff\"", `{"a" 1}`, `[1 2]`, `tru`, `nul`, `falsy`, |
| 331 |
} { |
| 332 |
if got, want := json.Valid([]byte(doc)), stdjson.Valid([]byte(doc)); got != want { |
| 333 |
t.Errorf("Valid(%q) = %v, want %v", doc, got, want) |
| 334 |
} |
| 335 |
} |
| 336 |
r := rand.New(rand.NewSource(3)) |
| 337 |
for i := 0; i < 20000; i++ { |
| 338 |
doc := randomSkippedValue(r, 1+r.Intn(4)) |
| 339 |
if r.Intn(2) == 0 { |
| 340 |
doc = mutate(r, doc) |
| 341 |
} |
| 342 |
if got, want := json.Valid([]byte(doc)), stdjson.Valid([]byte(doc)); got != want { |
| 343 |
t.Fatalf("Valid(%q) = %v, want %v", doc, got, want) |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
func TestFuncFieldTypeError(t *testing.T) { |
| 349 |
// A value of a func is a type error of its field, after which the decoding goes on; null leaves it nil. |
| 350 |
type funcs struct { |
| 351 |
F func() |
| 352 |
A int |
| 353 |
} |
| 354 |
for _, doc := range []string{ |
| 355 |
`{"F":1,"A":2}`, `{"F":"x","A":2}`, `{"F":null,"A":2}`, `{"F":[1,{"a":2}],"A":2}`, `{"F":{},"A":2}`, |
| 356 |
`{"F":true,"A":2}`, `{"F":nul,"A":2}`, `{"F":[1 2],"A":2}`, |
| 357 |
} { |
| 358 |
checkUnmarshal(t, doc, func() any { return new(funcs) }) |
| 359 |
} |
| 360 |
checkUnmarshal(t, `[true,null]`, func() any { return new([]func()) }) |
| 361 |
checkUnmarshal(t, `1`, func() any { return new(func()) }) |
| 362 |
} |
| 363 |
|
| 364 |
func TestStringOptionJSONNumber(t *testing.T) { |
| 365 |
// The bytes of the string of a json.Number of the string option are checked as encoding/json of the Go version |
| 366 |
// checks them: before Go 1.27, any bytes which start as a number are stored as they are. |
| 367 |
type number struct { |
| 368 |
N json.Number `json:",string"` |
| 369 |
A int |
| 370 |
} |
| 371 |
type stdNumber struct { |
| 372 |
N stdjson.Number `json:",string"` |
| 373 |
A int |
| 374 |
} |
| 375 |
check := func(doc string) { |
| 376 |
t.Helper() |
| 377 |
want, got := new(stdNumber), new(number) |
| 378 |
wantErr := describeTypeError(stdjson.Unmarshal([]byte(doc), want)) |
| 379 |
gotErr := describeTypeError(json.Unmarshal([]byte(doc), got)) |
| 380 |
if gotErr != wantErr || string(got.N) != string(want.N) || got.A != want.A { |
| 381 |
t.Errorf("%q:\n got %q %d %s\nwant %q %d %s", doc, got.N, got.A, gotErr, want.N, want.A, wantErr) |
| 382 |
} |
| 383 |
} |
| 384 |
for _, s := range []string{ |
| 385 |
`1`, `-1.5e3`, `01`, `0x`, `1e`, `-`, `1.`, `1..2`, `--1`, `1 `, ` 1`, `+1`, `00`, `.5`, `12abc`, `1,2`, ``, |
| 386 |
`true`, `false`, `null`, `nul`, `{}`, `\"1\"`, `\"x\"`, `\"1`, `\u0031`, `1\u0030`, ` null`, `\"1\" `, |
| 387 |
} { |
| 388 |
check(`{"N":"` + s + `","A":2}`) |
| 389 |
} |
| 390 |
for _, doc := range []string{`{"N":1,"A":2}`, `{"N":true,"A":2}`, `{"N":{},"A":2}`, `{"N":null,"A":2}`} { |
| 391 |
check(doc) |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
func TestStringOptionWhiteSpace(t *testing.T) { |
| 396 |
// The bytes of a string of the string option are a value with nothing around it: a white space is an error. |
| 397 |
type fields struct { |
| 398 |
B bool `json:",string"` |
| 399 |
S string `json:",string"` |
| 400 |
I int `json:",string"` |
| 401 |
F float64 `json:",string"` |
| 402 |
PB *bool `json:",string"` |
| 403 |
U uint8 `json:",string"` |
| 404 |
} |
| 405 |
for field, values := range map[string][]string{ |
| 406 |
"B": {` true`, `true `, `\ttrue`, `true\n`, ` false `}, |
| 407 |
"S": {` \"a\"`, `\"a\" `, ` \"a\" `}, |
| 408 |
"I": {` 1`, `1 `, ` -1`}, |
| 409 |
"F": {` 1.5`, `1.5 `}, |
| 410 |
"PB": {` true`, `true `}, |
| 411 |
} { |
| 412 |
for _, value := range values { |
| 413 |
checkUnmarshal(t, `{"`+field+`":"`+value+`","U":"7"}`, func() any { return new(fields) }) |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
func TestTimeOfOtherKinds(t *testing.T) { |
| 419 |
// A time.Time is decoded from a string: any other value is given to its UnmarshalJSON before Go 1.27, and is a |
| 420 |
// type error by Go 1.27. |
| 421 |
type times struct { |
| 422 |
T time.Time |
| 423 |
U int |
| 424 |
} |
| 425 |
for _, doc := range []string{ |
| 426 |
`{"T":1,"U":2}`, `{"T":true,"U":2}`, `{"T":{"a":[1]},"U":2}`, `{"T":[],"U":2}`, `{"T":null,"U":2}`, |
| 427 |
`{"T":"2020-01-01T00:00:00Z","U":2}`, `{"T":-1.5e3,"U":2}`, `{"T":tru,"U":2}`, |
| 428 |
} { |
| 429 |
checkUnmarshal(t, doc, func() any { return new(times) }) |
| 430 |
} |
| 431 |
checkUnmarshal(t, `1`, func() any { return new(time.Time) }) |
| 432 |
} |
| 433 |
|
| 434 |
// methodErrorValue is a value whose unmarshal methods fail for 0 and x. |
| 435 |
type methodErrorValue struct{ V int } |
| 436 |
|
| 437 |
func (v *methodErrorValue) UnmarshalJSON(b []byte) error { |
| 438 |
if string(b) == "0" { |
| 439 |
return errors.New("method error") |
| 440 |
} |
| 441 |
v.V = len(b) |
| 442 |
return nil |
| 443 |
} |
| 444 |
|
| 445 |
// methodErrorText is a value whose UnmarshalText fails for x. |
| 446 |
type methodErrorText struct{ V string } |
| 447 |
|
| 448 |
func (t *methodErrorText) UnmarshalText(b []byte) error { |
| 449 |
if string(b) == "x" { |
| 450 |
return errors.New("text method error") |
| 451 |
} |
| 452 |
t.V = string(b) |
| 453 |
return nil |
| 454 |
} |
| 455 |
|
| 456 |
// methodStdTypeError is a value whose UnmarshalJSON returns a type error of encoding/json. |
| 457 |
type methodStdTypeError struct{} |
| 458 |
|
| 459 |
func (*methodStdTypeError) UnmarshalJSON([]byte) error { |
| 460 |
return &stdjson.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(0)} |
| 461 |
} |
| 462 |
|
| 463 |
func TestUnmarshalMethodErrors(t *testing.T) { |
| 464 |
// The error of an unmarshal method stops the decoding before Go 1.27. By Go 1.27, the decoding goes on after |
| 465 |
// it, as after a type error, and the first error is returned: the error of the method as it is. |
| 466 |
type fields struct { |
| 467 |
A int |
| 468 |
B methodErrorValue |
| 469 |
C *methodErrorValue |
| 470 |
T methodErrorText |
| 471 |
E int |
| 472 |
} |
| 473 |
for _, doc := range []string{ |
| 474 |
`{"A":1,"B":0,"E":2}`, `{"A":"x","B":0,"E":2}`, `{"B":0,"A":"x","E":2}`, |
| 475 |
`{"T":"x","E":2}`, `{"B":0,"E":"x"}`, `{"B":0,"E":2,"C":1}`, `{"B":1,"T":"y","E":2}`, |
| 476 |
} { |
| 477 |
checkUnmarshal(t, doc, func() any { return new(fields) }) |
| 478 |
} |
| 479 |
// a type error of encoding/json has its field set before Go 1.27, as encoding/json sets it |
| 480 |
type stdTypeError struct { |
| 481 |
N methodStdTypeError |
| 482 |
E int |
| 483 |
} |
| 484 |
checkUnmarshal(t, `{"N":1,"E":2}`, func() any { return new(stdTypeError) }) |
| 485 |
} |
| 486 |
|
| 487 |
func TestTypeErrorPathOfKeys(t *testing.T) { |
| 488 |
// The path of a type error has the keys of the input, which Go 1.27 escapes as the tokens of a JSON pointer. |
| 489 |
for _, doc := range []string{`{"a/b":"x"}`, `{"a~b":"x"}`, `{"a.b":"x"}`, `{"~1/":{"c":"x"}}`} { |
| 490 |
checkUnmarshal(t, doc, func() any { return new(map[string]int) }) |
| 491 |
checkUnmarshal(t, doc, func() any { return new(map[string]map[string]int) }) |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
func TestNulAfterValue(t *testing.T) { |
| 496 |
// A nul byte in the input is a byte which the grammar doesn't have: after a value, it is a syntax error. |
| 497 |
for _, doc := range []string{"123\x00", "{\"a\":1}\x00", "\"s\" \x00", "[1]\x00 ", "true\x00", "1 \x00 2"} { |
| 498 |
checkUnmarshal(t, doc, func() any { return new(any) }) |
| 499 |
checkUnmarshal(t, doc, func() any { return new(struct{ A int }) }) |
| 500 |
var got, want any |
| 501 |
gotErr, wantErr := json.UnmarshalOf([]byte(doc), &got), stdjson.Unmarshal([]byte(doc), &want) |
| 502 |
if describeTypeErrorMessage(gotErr) != describeTypeErrorMessage(wantErr) { |
| 503 |
t.Errorf("UnmarshalOf(%q): got %s, want %s", doc, describeTypeErrorMessage(gotErr), describeTypeErrorMessage(wantErr)) |
| 504 |
} |
| 505 |
} |
| 506 |
} |