[go] encoding/json: document contrasting v2 behavior in StringifyWithLegacySemantics and UnmarshalArrayFromAnyLength

2 views
Skip to first unread message

Joseph Tsai (Gerrit)

unread,
Jun 9, 2026, 3:23:24 PM (14 hours ago) Jun 9
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Joseph Tsai has uploaded the change for review

Commit message

encoding/json: document contrasting v2 behavior in StringifyWithLegacySemantics and UnmarshalArrayFromAnyLength

Updates #79733
Change-Id: I97505b8c49152f3880b2c66e87c3b444e28d0b26

Change diff

diff --git a/src/encoding/json/v2_options.go b/src/encoding/json/v2_options.go
index d2414db..d7a95b6 100644
--- a/src/encoding/json/v2_options.go
+++ b/src/encoding/json/v2_options.go
@@ -501,7 +501,12 @@
// StringifyWithLegacySemantics specifies that the `string` tag option
// may stringify bools and string values. It only takes effect on fields
// where the top-level type is a bool, string, numeric kind, or a pointer to
-// such a kind.
+// such a kind. In contrast, the v2 default only allows the `string` tag option
+// on Go types that would have otherwise serialized as a JSON number,
+// and stringifies the JSON number within a a JSON string. In particular,
+// the v2 default does not stringify Go bools and strings.
+// If [ReportErrorsWithLegacySemantics] is false,
+// then incorrect usages of `string` results in a runtime error.
//
// When marshaling, such Go values are serialized as their usual JSON
// representation, but quoted within a JSON string.
@@ -511,6 +516,8 @@
// Note that the Go number grammar is a superset of the JSON number grammar.
// A JSON null quoted in a JSON string is a valid substitute for JSON null
// while unmarshaling into a Go value that `string` takes effect on.
+// In contrast, the v2 default rejects stringified numbers outside of the
+// grammar for a JSON number and also rejects a JSON null quoted in a JSON string.
//
// This affects either marshaling or unmarshaling.
// The v1 default is true.
@@ -526,6 +533,8 @@
// from input JSON arrays of any length. If the JSON array is too short,
// then the remaining Go array elements are zeroed. If the JSON array
// is too long, then the excess JSON array elements are skipped over.
+// In contrast, the v2 default expects that Go arrays be unmarshaled
+// from input JSON arrays of the exact length.
//
// This only affects unmarshaling and is ignored when marshaling.
// The v1 default is true.

Change information

Files:
  • M src/encoding/json/v2_options.go
Change size: S
Delta: 1 file changed, 10 insertions(+), 1 deletion(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I97505b8c49152f3880b2c66e87c3b444e28d0b26
Gerrit-Change-Number: 788940
Gerrit-PatchSet: 1
Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
unsatisfied_requirement
satisfied_requirement
open
diffy

Michael Pratt (Gerrit)

unread,
Jun 9, 2026, 6:19:34 PM (11 hours ago) Jun 9
to Joseph Tsai, goph...@pubsubhelper.golang.org, Michael Pratt, Austin Clements, Damien Neil, golang-co...@googlegroups.com
Attention needed from Austin Clements, Damien Neil and Joseph Tsai

Michael Pratt voted and added 1 comment

Votes added by Michael Pratt

Code-Review+2

1 comment

File src/encoding/json/v2_options.go
Line 506, Patchset 1 (Latest):// and stringifies the JSON number within a a JSON string. In particular,
Michael Pratt . unresolved

```suggestion
// and stringifies the JSON number within a JSON string. In particular,
```

Open in Gerrit

Related details

Attention is currently required from:
  • Austin Clements
  • Damien Neil
  • Joseph Tsai
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I97505b8c49152f3880b2c66e87c3b444e28d0b26
Gerrit-Change-Number: 788940
Gerrit-PatchSet: 1
Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Austin Clements <aus...@google.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Attention: Joseph Tsai <joe...@digital-static.net>
Gerrit-Attention: Austin Clements <aus...@google.com>
Gerrit-Comment-Date: Tue, 09 Jun 2026 22:19:28 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages