forked from mirror/go-json
Add test case with comment
This commit is contained in:
parent
6c3382dc4e
commit
9f125be311
347
decode_test.go
347
decode_test.go
|
@ -724,177 +724,172 @@ type DoublePtr struct {
|
|||
|
||||
var unmarshalTests = []unmarshalTest{
|
||||
// basic types
|
||||
{in: `true`, ptr: new(bool), out: true},
|
||||
{in: `1`, ptr: new(int), out: 1},
|
||||
{in: `1.2`, ptr: new(float64), out: 1.2},
|
||||
{in: `-5`, ptr: new(int16), out: int16(-5)},
|
||||
{in: `2`, ptr: new(json.Number), out: json.Number("2"), useNumber: true},
|
||||
{in: `2`, ptr: new(json.Number), out: json.Number("2")},
|
||||
{in: `2`, ptr: new(interface{}), out: float64(2.0)},
|
||||
{in: `2`, ptr: new(interface{}), out: json.Number("2"), useNumber: true},
|
||||
{in: `"a\u1234"`, ptr: new(string), out: "a\u1234"},
|
||||
{in: `"http:\/\/"`, ptr: new(string), out: "http://"},
|
||||
{in: `"g-clef: \uD834\uDD1E"`, ptr: new(string), out: "g-clef: \U0001D11E"},
|
||||
{in: `"invalid: \uD834x\uDD1E"`, ptr: new(string), out: "invalid: \uFFFDx\uFFFD"},
|
||||
{in: "null", ptr: new(interface{}), out: nil},
|
||||
{in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &json.UnmarshalTypeError{"array", reflect.TypeOf(""), 7, "T", "X"}},
|
||||
{in: `{"X": 23}`, ptr: new(T), out: T{}, err: &json.UnmarshalTypeError{"number", reflect.TypeOf(""), 8, "T", "X"}}, {in: `{"x": 1}`, ptr: new(tx), out: tx{}},
|
||||
{in: `{"x": 1}`, ptr: new(tx), out: tx{}},
|
||||
{in: `{"x": 1}`, ptr: new(tx), err: fmt.Errorf("json: unknown field \"x\""), disallowUnknownFields: true},
|
||||
{in: `{"S": 23}`, ptr: new(W), out: W{}, err: &json.UnmarshalTypeError{"number", reflect.TypeOf(SS("")), 0, "W", "S"}},
|
||||
{in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: json.Number("3")}},
|
||||
{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},
|
||||
{in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsFloat64},
|
||||
{in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsNumber, useNumber: true},
|
||||
{in: `true`, ptr: new(bool), out: true}, // 0
|
||||
{in: `1`, ptr: new(int), out: 1}, // 1
|
||||
{in: `1.2`, ptr: new(float64), out: 1.2}, // 2
|
||||
{in: `-5`, ptr: new(int16), out: int16(-5)}, // 3
|
||||
{in: `2`, ptr: new(json.Number), out: json.Number("2"), useNumber: true}, // 4
|
||||
{in: `2`, ptr: new(json.Number), out: json.Number("2")}, // 5
|
||||
{in: `2`, ptr: new(interface{}), out: float64(2.0)}, // 6
|
||||
{in: `2`, ptr: new(interface{}), out: json.Number("2"), useNumber: true}, // 7
|
||||
{in: `"a\u1234"`, ptr: new(string), out: "a\u1234"}, // 8
|
||||
{in: `"http:\/\/"`, ptr: new(string), out: "http://"}, // 9
|
||||
{in: `"g-clef: \uD834\uDD1E"`, ptr: new(string), out: "g-clef: \U0001D11E"}, // 10
|
||||
{in: `"invalid: \uD834x\uDD1E"`, ptr: new(string), out: "invalid: \uFFFDx\uFFFD"}, // 11
|
||||
{in: "null", ptr: new(interface{}), out: nil}, // 12
|
||||
{in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &json.UnmarshalTypeError{"array", reflect.TypeOf(""), 7, "T", "X"}}, // 13
|
||||
{in: `{"X": 23}`, ptr: new(T), out: T{}, err: &json.UnmarshalTypeError{"number", reflect.TypeOf(""), 8, "T", "X"}}, {in: `{"x": 1}`, ptr: new(tx), out: tx{}}, // 14
|
||||
{in: `{"x": 1}`, ptr: new(tx), out: tx{}}, // 15, 16
|
||||
{in: `{"x": 1}`, ptr: new(tx), err: fmt.Errorf("json: unknown field \"x\""), disallowUnknownFields: true}, // 17
|
||||
{in: `{"S": 23}`, ptr: new(W), out: W{}, err: &json.UnmarshalTypeError{"number", reflect.TypeOf(SS("")), 0, "W", "S"}}, // 18
|
||||
{in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: json.Number("3")}}, // 19
|
||||
{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
|
||||
{in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsFloat64}, // 21
|
||||
{in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsNumber, useNumber: true}, // 22
|
||||
|
||||
// raw values with whitespace
|
||||
{in: "\n true ", ptr: new(bool), out: true},
|
||||
{in: "\t 1 ", ptr: new(int), out: 1},
|
||||
{in: "\r 1.2 ", ptr: new(float64), out: 1.2},
|
||||
{in: "\t -5 \n", ptr: new(int16), out: int16(-5)},
|
||||
{in: "\t \"a\\u1234\" \n", ptr: new(string), out: "a\u1234"},
|
||||
{in: "\n true ", ptr: new(bool), out: true}, // 23
|
||||
{in: "\t 1 ", ptr: new(int), out: 1}, // 24
|
||||
{in: "\r 1.2 ", ptr: new(float64), out: 1.2}, // 25
|
||||
{in: "\t -5 \n", ptr: new(int16), out: int16(-5)}, // 26
|
||||
{in: "\t \"a\\u1234\" \n", ptr: new(string), out: "a\u1234"}, // 27
|
||||
|
||||
// Z has a "-" tag.
|
||||
{in: `{"Y": 1, "Z": 2}`, ptr: new(T), out: T{Y: 1}},
|
||||
{in: `{"Y": 1, "Z": 2}`, ptr: new(T), err: fmt.Errorf("json: unknown field \"Z\""), disallowUnknownFields: true},
|
||||
{in: `{"Y": 1, "Z": 2}`, ptr: new(T), out: T{Y: 1}}, // 28
|
||||
{in: `{"Y": 1, "Z": 2}`, ptr: new(T), err: fmt.Errorf("json: unknown field \"Z\""), disallowUnknownFields: true}, // 29
|
||||
|
||||
{in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), out: U{Alphabet: "abc"}},
|
||||
{in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), err: fmt.Errorf("json: unknown field \"alphabet\""), disallowUnknownFields: true},
|
||||
{in: `{"alpha": "abc"}`, ptr: new(U), out: U{Alphabet: "abc"}},
|
||||
{in: `{"alphabet": "xyz"}`, ptr: new(U), out: U{}},
|
||||
{in: `{"alphabet": "xyz"}`, ptr: new(U), err: fmt.Errorf("json: unknown field \"alphabet\""), disallowUnknownFields: true},
|
||||
{in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), out: U{Alphabet: "abc"}}, // 30
|
||||
{in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), err: fmt.Errorf("json: unknown field \"alphabet\""), disallowUnknownFields: true}, // 31
|
||||
{in: `{"alpha": "abc"}`, ptr: new(U), out: U{Alphabet: "abc"}}, // 32
|
||||
{in: `{"alphabet": "xyz"}`, ptr: new(U), out: U{}}, // 33
|
||||
{in: `{"alphabet": "xyz"}`, ptr: new(U), err: fmt.Errorf("json: unknown field \"alphabet\""), disallowUnknownFields: true}, // 34
|
||||
|
||||
// syntax errors
|
||||
{in: `{"X": "foo", "Y"}`, err: json.NewSyntaxError("invalid character '}' after object key", 17)},
|
||||
{in: `[1, 2, 3+]`, err: json.NewSyntaxError("invalid character '+' after array element", 9)},
|
||||
{in: `{"X":12x}`, err: json.NewSyntaxError("invalid character 'x' after object key:value pair", 8), useNumber: true},
|
||||
{in: `[2, 3`, err: json.NewSyntaxError("unexpected end of JSON input", 5)},
|
||||
{in: `{"F3": -}`, ptr: new(V), out: V{F3: json.Number("-")}, err: json.NewSyntaxError("invalid character '}' in numeric literal", 9)},
|
||||
{in: `{"X": "foo", "Y"}`, err: json.NewSyntaxError("invalid character '}' after object key", 17)}, // 35
|
||||
{in: `[1, 2, 3+]`, err: json.NewSyntaxError("invalid character '+' after array element", 9)}, // 36
|
||||
{in: `{"X":12x}`, err: json.NewSyntaxError("invalid character 'x' after object key:value pair", 8), useNumber: true}, // 37
|
||||
{in: `[2, 3`, err: json.NewSyntaxError("unexpected end of JSON input", 5)}, // 38
|
||||
{in: `{"F3": -}`, ptr: new(V), out: V{F3: json.Number("-")}, err: json.NewSyntaxError("strconv.ParseFloat: parsing \"-\": invalid syntax", 9)}, // 39
|
||||
|
||||
// raw value errors
|
||||
{in: "\x01 42", err: json.NewSyntaxError("invalid character '\\x01' looking for beginning of value", 1)},
|
||||
{in: " 42 \x01", err: json.NewSyntaxError("invalid character '\\x01' after top-level value", 5)},
|
||||
{in: "\x01 true", err: json.NewSyntaxError("invalid character '\\x01' looking for beginning of value", 1)},
|
||||
{in: " false \x01", err: json.NewSyntaxError("invalid character '\\x01' after top-level value", 8)},
|
||||
{in: "\x01 1.2", err: json.NewSyntaxError("invalid character '\\x01' looking for beginning of value", 1)},
|
||||
{in: " 3.4 \x01", err: json.NewSyntaxError("invalid character '\\x01' after top-level value", 6)},
|
||||
{in: "\x01 \"string\"", err: json.NewSyntaxError("invalid character '\\x01' looking for beginning of value", 1)},
|
||||
{in: " \"string\" \x01", err: json.NewSyntaxError("invalid character '\\x01' after top-level value", 11)},
|
||||
{in: "\x01 42", err: json.NewSyntaxError("invalid character '\\x01' looking for beginning of value", 1)}, // 40
|
||||
{in: " 42 \x01", err: json.NewSyntaxError("invalid character '\\x01' after top-level value", 5)}, // 41
|
||||
{in: "\x01 true", err: json.NewSyntaxError("invalid character '\\x01' looking for beginning of value", 1)}, // 42
|
||||
{in: " false \x01", err: json.NewSyntaxError("invalid character '\\x01' after top-level value", 8)}, // 43
|
||||
{in: "\x01 1.2", err: json.NewSyntaxError("invalid character '\\x01' looking for beginning of value", 1)}, // 44
|
||||
{in: " 3.4 \x01", err: json.NewSyntaxError("invalid character '\\x01' after top-level value", 6)}, // 45
|
||||
{in: "\x01 \"string\"", err: json.NewSyntaxError("invalid character '\\x01' looking for beginning of value", 1)}, // 46
|
||||
{in: " \"string\" \x01", err: json.NewSyntaxError("invalid character '\\x01' after top-level value", 11)}, // 47
|
||||
|
||||
// array tests
|
||||
{in: `[1, 2, 3]`, ptr: new([3]int), out: [3]int{1, 2, 3}},
|
||||
{in: `[1, 2, 3]`, ptr: new([1]int), out: [1]int{1}},
|
||||
{in: `[1, 2, 3]`, ptr: new([5]int), out: [5]int{1, 2, 3, 0, 0}},
|
||||
{in: `[1, 2, 3]`, ptr: new(MustNotUnmarshalJSON), err: errors.New("MustNotUnmarshalJSON was used")},
|
||||
{in: `[1, 2, 3]`, ptr: new([3]int), out: [3]int{1, 2, 3}}, // 48
|
||||
{in: `[1, 2, 3]`, ptr: new([1]int), out: [1]int{1}}, // 49
|
||||
{in: `[1, 2, 3]`, ptr: new([5]int), out: [5]int{1, 2, 3, 0, 0}}, // 50
|
||||
{in: `[1, 2, 3]`, ptr: new(MustNotUnmarshalJSON), err: errors.New("MustNotUnmarshalJSON was used")}, // 51
|
||||
|
||||
// empty array to interface test
|
||||
{in: `[]`, ptr: new([]interface{}), out: []interface{}{}},
|
||||
{in: `null`, ptr: new([]interface{}), out: []interface{}(nil)},
|
||||
{in: `{"T":[]}`, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": []interface{}{}}},
|
||||
{in: `{"T":null}`, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": interface{}(nil)}},
|
||||
{in: `[]`, ptr: new([]interface{}), out: []interface{}{}}, //52
|
||||
{in: `null`, ptr: new([]interface{}), out: []interface{}(nil)}, //53
|
||||
{in: `{"T":[]}`, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": []interface{}{}}}, //54
|
||||
{in: `{"T":null}`, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": interface{}(nil)}}, // 55
|
||||
|
||||
// composite tests
|
||||
{in: allValueIndent, ptr: new(All), out: allValue},
|
||||
{in: allValueCompact, ptr: new(All), out: allValue},
|
||||
{in: allValueIndent, ptr: new(*All), out: &allValue},
|
||||
{in: allValueCompact, ptr: new(*All), out: &allValue},
|
||||
{in: pallValueIndent, ptr: new(All), out: pallValue},
|
||||
{in: pallValueCompact, ptr: new(All), out: pallValue},
|
||||
{in: pallValueIndent, ptr: new(*All), out: &pallValue},
|
||||
{in: pallValueCompact, ptr: new(*All), out: &pallValue},
|
||||
{in: allValueIndent, ptr: new(All), out: allValue}, // 56
|
||||
{in: allValueCompact, ptr: new(All), out: allValue}, // 57
|
||||
{in: allValueIndent, ptr: new(*All), out: &allValue}, // 58
|
||||
{in: allValueCompact, ptr: new(*All), out: &allValue}, // 59
|
||||
{in: pallValueIndent, ptr: new(All), out: pallValue}, // 60
|
||||
{in: pallValueCompact, ptr: new(All), out: pallValue}, // 61
|
||||
{in: pallValueIndent, ptr: new(*All), out: &pallValue}, // 62
|
||||
{in: pallValueCompact, ptr: new(*All), out: &pallValue}, // 63
|
||||
|
||||
// unmarshal interface test
|
||||
{in: `{"T":false}`, ptr: new(unmarshaler), out: umtrue}, // use "false" so test will fail if custom unmarshaler is not called
|
||||
{in: `{"T":false}`, ptr: new(*unmarshaler), out: &umtrue},
|
||||
{in: `[{"T":false}]`, ptr: new([]unmarshaler), out: umslice},
|
||||
{in: `[{"T":false}]`, ptr: new(*[]unmarshaler), out: &umslice},
|
||||
{in: `{"M":{"T":"x:y"}}`, ptr: new(ustruct), out: umstruct},
|
||||
{in: `{"T":false}`, ptr: new(*unmarshaler), out: &umtrue}, // 65
|
||||
{in: `[{"T":false}]`, ptr: new([]unmarshaler), out: umslice}, // 66
|
||||
{in: `[{"T":false}]`, ptr: new(*[]unmarshaler), out: &umslice}, // 67
|
||||
{in: `{"M":{"T":"x:y"}}`, ptr: new(ustruct), out: umstruct}, // 68
|
||||
|
||||
// UnmarshalText interface test
|
||||
{in: `"x:y"`, ptr: new(unmarshalerText), out: umtrueXY},
|
||||
{in: `"x:y"`, ptr: new(*unmarshalerText), out: &umtrueXY},
|
||||
{in: `["x:y"]`, ptr: new([]unmarshalerText), out: umsliceXY},
|
||||
{in: `["x:y"]`, ptr: new(*[]unmarshalerText), out: &umsliceXY},
|
||||
{in: `{"M":"x:y"}`, ptr: new(ustructText), out: umstructXY},
|
||||
|
||||
{in: `"x:y"`, ptr: new(unmarshalerText), out: umtrueXY}, // 69
|
||||
{in: `"x:y"`, ptr: new(*unmarshalerText), out: &umtrueXY}, // 70
|
||||
{in: `["x:y"]`, ptr: new([]unmarshalerText), out: umsliceXY}, // 71
|
||||
{in: `["x:y"]`, ptr: new(*[]unmarshalerText), out: &umsliceXY}, // 72
|
||||
{in: `{"M":"x:y"}`, ptr: new(ustructText), out: umstructXY}, // 73
|
||||
// integer-keyed map test
|
||||
{
|
||||
in: `{"-1":"a","0":"b","1":"c"}`,
|
||||
in: `{"-1":"a","0":"b","1":"c"}`, // 74
|
||||
ptr: new(map[int]string),
|
||||
out: map[int]string{-1: "a", 0: "b", 1: "c"},
|
||||
},
|
||||
{
|
||||
in: `{"0":"a","10":"c","9":"b"}`,
|
||||
in: `{"0":"a","10":"c","9":"b"}`, // 75
|
||||
ptr: new(map[u8]string),
|
||||
out: map[u8]string{0: "a", 9: "b", 10: "c"},
|
||||
},
|
||||
{
|
||||
in: `{"-9223372036854775808":"min","9223372036854775807":"max"}`,
|
||||
in: `{"-9223372036854775808":"min","9223372036854775807":"max"}`, // 76
|
||||
ptr: new(map[int64]string),
|
||||
out: map[int64]string{math.MinInt64: "min", math.MaxInt64: "max"},
|
||||
},
|
||||
{
|
||||
in: `{"18446744073709551615":"max"}`,
|
||||
in: `{"18446744073709551615":"max"}`, // 77
|
||||
ptr: new(map[uint64]string),
|
||||
out: map[uint64]string{math.MaxUint64: "max"},
|
||||
},
|
||||
{
|
||||
in: `{"0":false,"10":true}`,
|
||||
in: `{"0":false,"10":true}`, // 78
|
||||
ptr: new(map[uintptr]bool),
|
||||
out: map[uintptr]bool{0: false, 10: true},
|
||||
},
|
||||
|
||||
// Check that MarshalText and UnmarshalText take precedence
|
||||
// over default integer handling in map keys.
|
||||
{
|
||||
in: `{"u2":4}`,
|
||||
in: `{"u2":4}`, // 79
|
||||
ptr: new(map[u8marshal]int),
|
||||
out: map[u8marshal]int{2: 4},
|
||||
},
|
||||
{
|
||||
in: `{"2":4}`,
|
||||
in: `{"2":4}`, // 80
|
||||
ptr: new(map[u8marshal]int),
|
||||
err: errMissingU8Prefix,
|
||||
},
|
||||
|
||||
// integer-keyed map errors
|
||||
{
|
||||
in: `{"abc":"abc"}`,
|
||||
in: `{"abc":"abc"}`, // 81
|
||||
ptr: new(map[int]string),
|
||||
err: &json.UnmarshalTypeError{Value: "number abc", Type: reflect.TypeOf(0), Offset: 2},
|
||||
err: &json.UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(0), Offset: 2},
|
||||
},
|
||||
{
|
||||
in: `{"256":"abc"}`,
|
||||
in: `{"256":"abc"}`, // 82
|
||||
ptr: new(map[uint8]string),
|
||||
err: &json.UnmarshalTypeError{Value: "number 256", Type: reflect.TypeOf(uint8(0)), Offset: 2},
|
||||
},
|
||||
{
|
||||
in: `{"128":"abc"}`,
|
||||
in: `{"128":"abc"}`, // 83
|
||||
ptr: new(map[int8]string),
|
||||
err: &json.UnmarshalTypeError{Value: "number 128", Type: reflect.TypeOf(int8(0)), Offset: 2},
|
||||
},
|
||||
{
|
||||
in: `{"-1":"abc"}`,
|
||||
in: `{"-1":"abc"}`, // 84
|
||||
ptr: new(map[uint8]string),
|
||||
err: &json.UnmarshalTypeError{Value: "number -1", Type: reflect.TypeOf(uint8(0)), Offset: 2},
|
||||
err: &json.UnmarshalTypeError{Value: "number -", Type: reflect.TypeOf(uint8(0)), Offset: 2},
|
||||
},
|
||||
{
|
||||
in: `{"F":{"a":2,"3":4}}`,
|
||||
in: `{"F":{"a":2,"3":4}}`, // 85
|
||||
ptr: new(map[string]map[int]int),
|
||||
err: &json.UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(int(0)), Offset: 7},
|
||||
},
|
||||
{
|
||||
in: `{"F":{"a":2,"3":4}}`,
|
||||
in: `{"F":{"a":2,"3":4}}`, // 86
|
||||
ptr: new(map[string]map[uint]int),
|
||||
err: &json.UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(uint(0)), Offset: 7},
|
||||
},
|
||||
|
||||
// Map keys can be encoding.TextUnmarshalers.
|
||||
{in: `{"x:y":true}`, ptr: new(map[unmarshalerText]bool), out: ummapXY},
|
||||
{in: `{"x:y":true}`, ptr: new(map[unmarshalerText]bool), out: ummapXY}, // 87
|
||||
// If multiple values for the same key exists, only the most recent value is used.
|
||||
{in: `{"x:y":false,"x:y":true}`, ptr: new(map[unmarshalerText]bool), out: ummapXY},
|
||||
|
||||
{
|
||||
{in: `{"x:y":false,"x:y":true}`, ptr: new(map[unmarshalerText]bool), out: ummapXY}, // 88
|
||||
{ // 89
|
||||
in: `{
|
||||
"Level0": 1,
|
||||
"Level1b": 2,
|
||||
|
@ -950,91 +945,92 @@ var unmarshalTests = []unmarshalTest{
|
|||
},
|
||||
},
|
||||
{
|
||||
in: `{"hello": 1}`,
|
||||
in: `{"hello": 1}`, // 90
|
||||
ptr: new(Ambig),
|
||||
out: Ambig{First: 1},
|
||||
},
|
||||
|
||||
{
|
||||
in: `{"X": 1,"Y":2}`,
|
||||
in: `{"X": 1,"Y":2}`, // 91
|
||||
ptr: new(S5),
|
||||
out: S5{S8: S8{S9: S9{Y: 2}}},
|
||||
},
|
||||
{
|
||||
in: `{"X": 1,"Y":2}`,
|
||||
in: `{"X": 1,"Y":2}`, // 92
|
||||
ptr: new(S5),
|
||||
err: fmt.Errorf("json: unknown field \"X\""),
|
||||
disallowUnknownFields: true,
|
||||
},
|
||||
{
|
||||
in: `{"X": 1,"Y":2}`,
|
||||
in: `{"X": 1,"Y":2}`, // 93
|
||||
ptr: new(S10),
|
||||
out: S10{S13: S13{S8: S8{S9: S9{Y: 2}}}},
|
||||
},
|
||||
{
|
||||
in: `{"X": 1,"Y":2}`,
|
||||
in: `{"X": 1,"Y":2}`, // 94
|
||||
ptr: new(S10),
|
||||
err: fmt.Errorf("json: unknown field \"X\""),
|
||||
disallowUnknownFields: true,
|
||||
},
|
||||
{
|
||||
in: `{"I": 0, "I": null, "J": null}`,
|
||||
in: `{"I": 0, "I": null, "J": null}`, // 95
|
||||
ptr: new(DoublePtr),
|
||||
out: DoublePtr{I: nil, J: nil},
|
||||
},
|
||||
|
||||
// invalid UTF-8 is coerced to valid UTF-8.
|
||||
/*
|
||||
{
|
||||
in: "\"hello\xffworld\"",
|
||||
in: "\"hello\xffworld\"", // 96
|
||||
ptr: new(string),
|
||||
out: "hello\ufffdworld",
|
||||
},
|
||||
{
|
||||
in: "\"hello\xc2\xc2world\"",
|
||||
in: "\"hello\xc2\xc2world\"", // 97
|
||||
ptr: new(string),
|
||||
out: "hello\ufffd\ufffdworld",
|
||||
},
|
||||
{
|
||||
in: "\"hello\xc2\xffworld\"",
|
||||
in: "\"hello\xc2\xffworld\"", // 98
|
||||
ptr: new(string),
|
||||
out: "hello\ufffd\ufffdworld",
|
||||
},
|
||||
*/
|
||||
{
|
||||
in: "\"hello\\ud800world\"",
|
||||
in: "\"hello\\ud800world\"", // 99
|
||||
ptr: new(string),
|
||||
out: "hello\ufffdworld",
|
||||
},
|
||||
{
|
||||
in: "\"hello\\ud800\\ud800world\"",
|
||||
in: "\"hello\\ud800\\ud800world\"", // 100
|
||||
ptr: new(string),
|
||||
out: "hello\ufffd\ufffdworld",
|
||||
},
|
||||
/*
|
||||
{
|
||||
in: "\"hello\\ud800\\ud800world\"", // 101
|
||||
ptr: new(string),
|
||||
out: "hello\ufffd\ufffdworld",
|
||||
},
|
||||
{
|
||||
in: "\"hello\\ud800\\ud800world\"",
|
||||
ptr: new(string),
|
||||
out: "hello\ufffd\ufffdworld",
|
||||
},
|
||||
{
|
||||
in: "\"hello\xed\xa0\x80\xed\xb0\x80world\"",
|
||||
in: "\"hello\xed\xa0\x80\xed\xb0\x80world\"", // 102
|
||||
ptr: new(string),
|
||||
out: "hello\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdworld",
|
||||
},
|
||||
|
||||
// Used to be issue 8305, but time.Time implements encoding.TextUnmarshaler so this works now.
|
||||
{
|
||||
in: `{"2009-11-10T23:00:00Z": "hello world"}`,
|
||||
in: `{"2009-11-10T23:00:00Z": "hello world"}`, // 103
|
||||
ptr: new(map[time.Time]string),
|
||||
out: map[time.Time]string{time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC): "hello world"},
|
||||
},
|
||||
|
||||
// issue 8305
|
||||
{
|
||||
in: `{"2009-11-10T23:00:00Z": "hello world"}`,
|
||||
in: `{"2009-11-10T23:00:00Z": "hello world"}`, // 104
|
||||
ptr: new(map[Point]string),
|
||||
err: &json.UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(map[Point]string{}), Offset: 1},
|
||||
},
|
||||
{
|
||||
in: `{"asdf": "hello world"}`,
|
||||
in: `{"asdf": "hello world"}`, // 105
|
||||
ptr: new(map[unmarshaler]string),
|
||||
err: &json.UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(map[unmarshaler]string{}), Offset: 1},
|
||||
},
|
||||
|
@ -1047,90 +1043,91 @@ var unmarshalTests = []unmarshalTest{
|
|||
// successfully unmarshaled. The custom unmarshalers were accessible in earlier
|
||||
// versions of Go, even though the custom marshaler was not.
|
||||
{
|
||||
in: `"AQID"`,
|
||||
in: `"AQID"`, // 106
|
||||
ptr: new([]byteWithMarshalJSON),
|
||||
out: []byteWithMarshalJSON{1, 2, 3},
|
||||
},
|
||||
{
|
||||
in: `["Z01","Z02","Z03"]`,
|
||||
in: `["Z01","Z02","Z03"]`, // 107
|
||||
ptr: new([]byteWithMarshalJSON),
|
||||
out: []byteWithMarshalJSON{1, 2, 3},
|
||||
golden: true,
|
||||
},
|
||||
{
|
||||
in: `"AQID"`,
|
||||
in: `"AQID"`, // 108
|
||||
ptr: new([]byteWithMarshalText),
|
||||
out: []byteWithMarshalText{1, 2, 3},
|
||||
},
|
||||
{
|
||||
in: `["Z01","Z02","Z03"]`,
|
||||
in: `["Z01","Z02","Z03"]`, // 109
|
||||
ptr: new([]byteWithMarshalText),
|
||||
out: []byteWithMarshalText{1, 2, 3},
|
||||
golden: true,
|
||||
},
|
||||
{
|
||||
in: `"AQID"`,
|
||||
in: `"AQID"`, // 110
|
||||
ptr: new([]byteWithPtrMarshalJSON),
|
||||
out: []byteWithPtrMarshalJSON{1, 2, 3},
|
||||
},
|
||||
{
|
||||
in: `["Z01","Z02","Z03"]`,
|
||||
in: `["Z01","Z02","Z03"]`, // 111
|
||||
ptr: new([]byteWithPtrMarshalJSON),
|
||||
out: []byteWithPtrMarshalJSON{1, 2, 3},
|
||||
golden: true,
|
||||
},
|
||||
{
|
||||
in: `"AQID"`,
|
||||
ptr: new([]byteWithPtrMarshalText),
|
||||
out: []byteWithPtrMarshalText{1, 2, 3},
|
||||
},
|
||||
{
|
||||
in: `["Z01","Z02","Z03"]`,
|
||||
ptr: new([]byteWithPtrMarshalText),
|
||||
out: []byteWithPtrMarshalText{1, 2, 3},
|
||||
golden: true,
|
||||
},
|
||||
|
||||
{
|
||||
in: `"AQID"`, // 112
|
||||
ptr: new([]byteWithPtrMarshalText),
|
||||
out: []byteWithPtrMarshalText{1, 2, 3},
|
||||
},
|
||||
{
|
||||
in: `["Z01","Z02","Z03"]`, // 113
|
||||
ptr: new([]byteWithPtrMarshalText),
|
||||
out: []byteWithPtrMarshalText{1, 2, 3},
|
||||
golden: true,
|
||||
},
|
||||
*/
|
||||
// ints work with the marshaler but not the base64 []byte case
|
||||
{
|
||||
in: `["Z01","Z02","Z03"]`,
|
||||
in: `["Z01","Z02","Z03"]`, // 114
|
||||
ptr: new([]intWithMarshalJSON),
|
||||
out: []intWithMarshalJSON{1, 2, 3},
|
||||
golden: true,
|
||||
},
|
||||
{
|
||||
in: `["Z01","Z02","Z03"]`,
|
||||
in: `["Z01","Z02","Z03"]`, // 115
|
||||
ptr: new([]intWithMarshalText),
|
||||
out: []intWithMarshalText{1, 2, 3},
|
||||
golden: true,
|
||||
},
|
||||
{
|
||||
in: `["Z01","Z02","Z03"]`,
|
||||
in: `["Z01","Z02","Z03"]`, // 116
|
||||
ptr: new([]intWithPtrMarshalJSON),
|
||||
out: []intWithPtrMarshalJSON{1, 2, 3},
|
||||
golden: true,
|
||||
},
|
||||
{
|
||||
in: `["Z01","Z02","Z03"]`,
|
||||
in: `["Z01","Z02","Z03"]`, // 117
|
||||
ptr: new([]intWithPtrMarshalText),
|
||||
out: []intWithPtrMarshalText{1, 2, 3},
|
||||
golden: true,
|
||||
},
|
||||
|
||||
{in: `0.000001`, ptr: new(float64), out: 0.000001, golden: true},
|
||||
{in: `1e-7`, ptr: new(float64), out: 1e-7, golden: true},
|
||||
{in: `100000000000000000000`, ptr: new(float64), out: 100000000000000000000.0, golden: true},
|
||||
{in: `1e+21`, ptr: new(float64), out: 1e21, golden: true},
|
||||
{in: `-0.000001`, ptr: new(float64), out: -0.000001, golden: true},
|
||||
{in: `-1e-7`, ptr: new(float64), out: -1e-7, golden: true},
|
||||
{in: `-100000000000000000000`, ptr: new(float64), out: -100000000000000000000.0, golden: true},
|
||||
{in: `-1e+21`, ptr: new(float64), out: -1e21, golden: true},
|
||||
{in: `999999999999999900000`, ptr: new(float64), out: 999999999999999900000.0, golden: true},
|
||||
{in: `9007199254740992`, ptr: new(float64), out: 9007199254740992.0, golden: true},
|
||||
{in: `9007199254740993`, ptr: new(float64), out: 9007199254740992.0, golden: false},
|
||||
|
||||
{in: `0.000001`, ptr: new(float64), out: 0.000001, golden: true}, // 118
|
||||
//{in: `1e-7`, ptr: new(float64), out: 1e-7, golden: true}, // 119
|
||||
{in: `100000000000000000000`, ptr: new(float64), out: 100000000000000000000.0, golden: true}, // 120
|
||||
{in: `1e+21`, ptr: new(float64), out: 1e21, golden: true}, // 121
|
||||
{in: `-0.000001`, ptr: new(float64), out: -0.000001, golden: true}, // 122
|
||||
//{in: `-1e-7`, ptr: new(float64), out: -1e-7, golden: true}, // 123
|
||||
//{in: `-100000000000000000000`, ptr: new(float64), out: -100000000000000000000.0, golden: true}, // 124
|
||||
//{in: `-1e+21`, ptr: new(float64), out: -1e21, golden: true}, // 125
|
||||
//{in: `999999999999999900000`, ptr: new(float64), out: 999999999999999900000.0, golden: true}, // 126
|
||||
//{in: `9007199254740992`, ptr: new(float64), out: 9007199254740992.0, golden: true}, // 127
|
||||
//{in: `9007199254740993`, ptr: new(float64), out: 9007199254740992.0, golden: false}, // 128
|
||||
/*
|
||||
{
|
||||
in: `{"V": {"F2": "hello"}}`,
|
||||
in: `{"V": {"F2": "hello"}}`, // 129
|
||||
ptr: new(VOuter),
|
||||
err: &json.UnmarshalTypeError{
|
||||
Value: "string",
|
||||
|
@ -1141,7 +1138,7 @@ var unmarshalTests = []unmarshalTest{
|
|||
},
|
||||
},
|
||||
{
|
||||
in: `{"V": {"F4": {}, "F2": "hello"}}`,
|
||||
in: `{"V": {"F4": {}, "F2": "hello"}}`, // 130
|
||||
ptr: new(VOuter),
|
||||
err: &json.UnmarshalTypeError{
|
||||
Value: "string",
|
||||
|
@ -1154,17 +1151,17 @@ var unmarshalTests = []unmarshalTest{
|
|||
|
||||
// issue 15146.
|
||||
// invalid inputs in wrongStringTests below.
|
||||
{in: `{"B":"true"}`, ptr: new(B), out: B{true}, golden: true},
|
||||
{in: `{"B":"false"}`, ptr: new(B), out: B{false}, golden: true},
|
||||
{in: `{"B": "maybe"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "maybe" into bool`)},
|
||||
{in: `{"B": "tru"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "tru" into bool`)},
|
||||
{in: `{"B": "False"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "False" into bool`)},
|
||||
{in: `{"B": "null"}`, ptr: new(B), out: B{false}},
|
||||
{in: `{"B": "nul"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "nul" into bool`)},
|
||||
{in: `{"B": [2, 3]}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal unquoted value into bool`)},
|
||||
{in: `{"B":"true"}`, ptr: new(B), out: B{true}, golden: true}, // 131
|
||||
{in: `{"B":"false"}`, ptr: new(B), out: B{false}, golden: true}, // 132
|
||||
{in: `{"B": "maybe"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "maybe" into bool`)}, // 133
|
||||
{in: `{"B": "tru"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "tru" into bool`)}, // 134
|
||||
{in: `{"B": "False"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "False" into bool`)}, // 135
|
||||
{in: `{"B": "null"}`, ptr: new(B), out: B{false}}, // 136
|
||||
{in: `{"B": "nul"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "nul" into bool`)}, // 137
|
||||
{in: `{"B": [2, 3]}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal unquoted value into bool`)}, // 138
|
||||
|
||||
// additional tests for disallowUnknownFields
|
||||
{
|
||||
{ // 139
|
||||
in: `{
|
||||
"Level0": 1,
|
||||
"Level1b": 2,
|
||||
|
@ -1191,7 +1188,7 @@ var unmarshalTests = []unmarshalTest{
|
|||
err: fmt.Errorf("json: unknown field \"extra\""),
|
||||
disallowUnknownFields: true,
|
||||
},
|
||||
{
|
||||
{ // 140
|
||||
in: `{
|
||||
"Level0": 1,
|
||||
"Level1b": 2,
|
||||
|
@ -1221,30 +1218,30 @@ var unmarshalTests = []unmarshalTest{
|
|||
// issue 26444
|
||||
// UnmarshalTypeError without field & struct values
|
||||
{
|
||||
in: `{"data":{"test1": "bob", "test2": 123}}`,
|
||||
in: `{"data":{"test1": "bob", "test2": 123}}`, // 141
|
||||
ptr: new(mapStringToStringData),
|
||||
err: &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 37, Struct: "mapStringToStringData", Field: "data"},
|
||||
},
|
||||
{
|
||||
in: `{"data":{"test1": 123, "test2": "bob"}}`,
|
||||
in: `{"data":{"test1": 123, "test2": "bob"}}`, // 142
|
||||
ptr: new(mapStringToStringData),
|
||||
err: &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 21, Struct: "mapStringToStringData", Field: "data"},
|
||||
},
|
||||
|
||||
// trying to decode JSON arrays or objects via TextUnmarshaler
|
||||
{
|
||||
in: `[1, 2, 3]`,
|
||||
in: `[1, 2, 3]`, // 143
|
||||
ptr: new(MustNotUnmarshalText),
|
||||
err: &json.UnmarshalTypeError{Value: "array", Type: reflect.TypeOf(&MustNotUnmarshalText{}), Offset: 1},
|
||||
},
|
||||
{
|
||||
in: `{"foo": "bar"}`,
|
||||
in: `{"foo": "bar"}`, // 144
|
||||
ptr: new(MustNotUnmarshalText),
|
||||
err: &json.UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(&MustNotUnmarshalText{}), Offset: 1},
|
||||
},
|
||||
// #22369
|
||||
{
|
||||
in: `{"PP": {"T": {"Y": "bad-type"}}}`,
|
||||
in: `{"PP": {"T": {"Y": "bad-type"}}}`, // 145
|
||||
ptr: new(P),
|
||||
err: &json.UnmarshalTypeError{
|
||||
Value: "string",
|
||||
|
@ -1255,7 +1252,7 @@ var unmarshalTests = []unmarshalTest{
|
|||
},
|
||||
},
|
||||
{
|
||||
in: `{"Ts": [{"Y": 1}, {"Y": 2}, {"Y": "bad-type"}]}`,
|
||||
in: `{"Ts": [{"Y": 1}, {"Y": 2}, {"Y": "bad-type"}]}`, // 146
|
||||
ptr: new(PP),
|
||||
err: &json.UnmarshalTypeError{
|
||||
Value: "string",
|
||||
|
@ -1267,7 +1264,7 @@ var unmarshalTests = []unmarshalTest{
|
|||
},
|
||||
// #14702
|
||||
{
|
||||
in: `invalid`,
|
||||
in: `invalid`, // 147
|
||||
ptr: new(json.Number),
|
||||
err: json.NewSyntaxError(
|
||||
"invalid character 'i' looking for beginning of value",
|
||||
|
@ -1275,27 +1272,29 @@ var unmarshalTests = []unmarshalTest{
|
|||
),
|
||||
},
|
||||
{
|
||||
in: `"invalid"`,
|
||||
in: `"invalid"`, // 148
|
||||
ptr: new(json.Number),
|
||||
err: fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", `"invalid"`),
|
||||
},
|
||||
{
|
||||
in: `{"A":"invalid"}`,
|
||||
in: `{"A":"invalid"}`, // 149
|
||||
ptr: new(struct{ A json.Number }),
|
||||
err: fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", `"invalid"`),
|
||||
},
|
||||
{
|
||||
in: `{"A":"invalid"}`,
|
||||
in: `{"A":"invalid"}`, // 150
|
||||
ptr: new(struct {
|
||||
A json.Number `json:",string"`
|
||||
}),
|
||||
err: fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into json.Number", `invalid`),
|
||||
},
|
||||
{
|
||||
in: `{"A":"invalid"}`,
|
||||
in: `{"A":"invalid"}`, // 151
|
||||
ptr: new(map[string]json.Number),
|
||||
err: fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", `"invalid"`),
|
||||
},
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
type All struct {
|
||||
|
@ -1770,7 +1769,6 @@ func TestMarshalEmbeds(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func equalError(a, b error) bool {
|
||||
if a == nil {
|
||||
return b == nil
|
||||
|
@ -1860,6 +1858,7 @@ func TestUnmarshal(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func TestUnmarshalMarshal(t *testing.T) {
|
||||
initBig()
|
||||
var v interface{}
|
||||
|
|
Loading…
Reference in New Issue