aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/json/v2/errors.go
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2025-07-24 11:34:18 -0700
committerGopher Robot <gobot@golang.org>2025-07-24 20:30:25 -0700
commita6eec8bdc79a89f6001d7788d280b8910c5f1b13 (patch)
tree8ee43d469512cbdeaa0847eae2dc7d3ea1419a88 /src/encoding/json/v2/errors.go
parent0fa88dec1e23ceeef9f5719e0f9ccb94766e53e7 (diff)
downloadgo-a6eec8bdc79a89f6001d7788d280b8910c5f1b13.tar.xz
encoding/json: reduce error text regressions under goexperiment.jsonv2
There were minor and unnecessary error text changes when v1 was implemented using v2. Reduce divergences if possible. Of the cases reported in #74713, there are no more differences for: v1: json: cannot unmarshal number into Go value of type chan int v2: json: cannot unmarshal number into Go value of type chan int and v1: json: cannot unmarshal number into Go value of type error v2: json: cannot unmarshal number into Go value of type error However, there is a difference between: v1: json: cannot unmarshal string into Go struct field .F.V of type int v2: json: cannot unmarshal string into Go struct field S.F.V of type int For reasons unclear, the v1 logic was always inconsistent about whether it could properly record the root struct type, while the v1 emulation layer under v2 is always able to. This only modifies code that is compiled in under goexperiment.jsonv2. Fixes #74713 Change-Id: I9e87323b1810130cb929288fdd86aff4be82d5f2 Reviewed-on: https://go-review.googlesource.com/c/go/+/689918 Reviewed-by: Damien Neil <dneil@google.com> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/encoding/json/v2/errors.go')
-rw-r--r--src/encoding/json/v2/errors.go9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/encoding/json/v2/errors.go b/src/encoding/json/v2/errors.go
index 48cdcc953b..1f31505869 100644
--- a/src/encoding/json/v2/errors.go
+++ b/src/encoding/json/v2/errors.go
@@ -120,10 +120,17 @@ func newMarshalErrorBefore(e *jsontext.Encoder, t reflect.Type, err error) error
// is positioned right before the next token or value, which causes an error.
// It does not record the next JSON kind as this error is used to indicate
// the receiving Go value is invalid to unmarshal into (and not a JSON error).
+// However, if [jsonflags.ReportErrorsWithLegacySemantics] is specified,
+// then it does record the next JSON kind for historical reporting reasons.
func newUnmarshalErrorBefore(d *jsontext.Decoder, t reflect.Type, err error) error {
+ var k jsontext.Kind
+ if export.Decoder(d).Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
+ k = d.PeekKind()
+ }
return &SemanticError{action: "unmarshal", GoType: t, Err: err,
ByteOffset: d.InputOffset() + int64(export.Decoder(d).CountNextDelimWhitespace()),
- JSONPointer: jsontext.Pointer(export.Decoder(d).AppendStackPointer(nil, +1))}
+ JSONPointer: jsontext.Pointer(export.Decoder(d).AppendStackPointer(nil, +1)),
+ JSONKind: k}
}
// newUnmarshalErrorBeforeWithSkipping is like [newUnmarshalErrorBefore],