aboutsummaryrefslogtreecommitdiff
path: root/src/encoding
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2025-01-11 12:48:33 -0800
committerGopher Robot <gobot@golang.org>2025-01-13 08:27:22 -0800
commit47a56b2b6d2cca56384810027964968667b86fdc (patch)
tree266fe404a0dbe11e2f749ea5ca458da8b5bfee04 /src/encoding
parent7bb192a1c56e2961b3eeffb8250615e395c903d4 (diff)
downloadgo-47a56b2b6d2cca56384810027964968667b86fdc.tar.xz
encoding/json: add cases to TestUnmarshal for fatal syntactic errors
The presence of a syntax error in the input immediately unmarshaling before unmarshaling into the underlying value. Otherwise, semantic errors are generally lazily reported and allow unmarshaling to continue on. Change-Id: Icf1cfc684e415312d9c8bf739c396ede15299d7d Reviewed-on: https://go-review.googlesource.com/c/go/+/642295 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/encoding')
-rw-r--r--src/encoding/json/decode_test.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/encoding/json/decode_test.go b/src/encoding/json/decode_test.go
index d08d9a4e0a..3905a054ce 100644
--- a/src/encoding/json/decode_test.go
+++ b/src/encoding/json/decode_test.go
@@ -1170,6 +1170,23 @@ var unmarshalTests = []struct {
N Number `json:",string"`
}{"5"},
},
+
+ // Verify that syntactic errors are immediately fatal,
+ // while semantic errors are lazily reported
+ // (i.e., allow processing to continue).
+ {
+ CaseName: Name(""),
+ in: `[1,2,true,4,5}`,
+ ptr: new([]int),
+ err: &SyntaxError{msg: "invalid character '}' after array element", Offset: 14},
+ },
+ {
+ CaseName: Name(""),
+ in: `[1,2,true,4,5]`,
+ ptr: new([]int),
+ out: []int{1, 2, 0, 4, 5},
+ err: &UnmarshalTypeError{Value: "bool", Type: reflect.TypeFor[int](), Offset: 9},
+ },
}
func TestMarshal(t *testing.T) {