aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/json/v2_decode_test.go
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2025-10-11 11:57:46 -0700
committerJoseph Tsai <joetsai@digital-static.net>2025-10-14 12:20:23 -0700
commitee5af46172e64eceddb56018de8ea850fe0a6cae (patch)
tree7b80cbd4d4a5490f725890184b5323e1ae0bb62a /src/encoding/json/v2_decode_test.go
parent11d3d2f77d8293fe14638e74cbf52d1241b60e78 (diff)
downloadgo-ee5af46172e64eceddb56018de8ea850fe0a6cae.tar.xz
encoding/json: avoid misleading errors under goexperiment.jsonv2
The jsontext package represents the location of JSON errors using a JSON Pointer (RFC 6901). This uses the JSON type system. Unfortunately the v1 json.UnmarshalTypeError assumes a Go struct-based mechanism for reporting the location of errors (and has historically never been implemented correctly since it was a weird mix of both JSON and Go namespaces; see #43126). Trying to map a JSON Pointer into UnmarshalTypeError.{Struct,Field} is difficult to get right without teaching jsontext about the Go type system. To reduce the probability of misleading errors, check whether the last token looks like a JSON array index and if so, elide the phrase "into Go struct field". Fixes #74801 Change-Id: Id2088ffb9c339a9238ed38c90223d86a89422842 Reviewed-on: https://go-review.googlesource.com/c/go/+/710676 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Damien Neil <dneil@google.com>
Diffstat (limited to 'src/encoding/json/v2_decode_test.go')
-rw-r--r--src/encoding/json/v2_decode_test.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/encoding/json/v2_decode_test.go b/src/encoding/json/v2_decode_test.go
index 28c57ec8bf..26b4448721 100644
--- a/src/encoding/json/v2_decode_test.go
+++ b/src/encoding/json/v2_decode_test.go
@@ -2363,6 +2363,34 @@ func TestUnmarshalTypeError(t *testing.T) {
}
}
+func TestUnmarshalTypeErrorMessage(t *testing.T) {
+ err := &UnmarshalTypeError{
+ Value: "number 5",
+ Type: reflect.TypeFor[int](),
+ Offset: 1234,
+ Struct: "Root",
+ }
+
+ for _, tt := range []struct {
+ field string
+ want string
+ }{
+ {"", "json: cannot unmarshal number 5 into Go struct field Root. of type int"},
+ {"1", "json: cannot unmarshal number 5 into Root.1 of type int"},
+ {"foo", "json: cannot unmarshal number 5 into Go struct field Root.foo of type int"},
+ {"foo.1", "json: cannot unmarshal number 5 into Root.foo.1 of type int"},
+ {"foo.bar", "json: cannot unmarshal number 5 into Go struct field Root.foo.bar of type int"},
+ {"foo.bar.1", "json: cannot unmarshal number 5 into Root.foo.bar.1 of type int"},
+ {"foo.bar.baz", "json: cannot unmarshal number 5 into Go struct field Root.foo.bar.baz of type int"},
+ } {
+ err.Field = tt.field
+ got := err.Error()
+ if got != tt.want {
+ t.Errorf("Error:\n\tgot: %v\n\twant: %v", got, tt.want)
+ }
+ }
+}
+
func TestUnmarshalSyntax(t *testing.T) {
var x any
tests := []struct {