[go] encoding/json: fix Indent trailing whitespace regression in goexperiment.jsonv2

7 views
Skip to first unread message

Joseph Tsai (Gerrit)

unread,
Jul 31, 2025, 7:24:46 PMJul 31
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Joseph Tsai has uploaded the change for review

Commit message

encoding/json: fix Indent trailing whitespace regression in goexperiment.jsonv2

The Indent function preserves trailing whitespace,
while the v1 emulation under v2 implementation accidentally dropped it.

There was prior logic that attempted to preserve it,
but it did not work correctly since it ran in a defer and
accidentally mutated the dst input argument rather than the output argument.
Move the logic to the end and avoid a defer.
Also, add a test to both v1 and v1in2 to codify this behavior.

This only modifies code that is compiled in under goexperiment.jsonv2.

Updates #13520
Fixes #74806
Change-Id: I22b1a8da5185eb969e2a8a111b625d3752cfcbe8

Change diff

diff --git a/src/encoding/json/scanner_test.go b/src/encoding/json/scanner_test.go
index fb64463..a062e91 100644
--- a/src/encoding/json/scanner_test.go
+++ b/src/encoding/json/scanner_test.go
@@ -74,6 +74,7 @@
-5e+2
]`},
{Name(""), "{\"\":\"<>&\u2028\u2029\"}", "{\n\t\"\": \"<>&\u2028\u2029\"\n}"}, // See golang.org/issue/34070
+ {Name(""), `null`, "null \n\r\t"}, // See golang.org/issue/13520 and golang.org/issue/74806
}
var buf bytes.Buffer
for _, tt := range tests {
@@ -102,7 +103,7 @@
buf.Reset()
if err := Indent(&buf, []byte(tt.compact), "", "\t"); err != nil {
t.Errorf("%s: Indent error: %v", tt.Where, err)
- } else if got := buf.String(); got != tt.indent {
+ } else if got := buf.String(); got != strings.TrimRight(tt.indent, " \n\r\t") {
t.Errorf("%s: Compact:\n\tgot: %s\n\twant: %s", tt.Where, indentNewlines(got), indentNewlines(tt.indent))
}
})
diff --git a/src/encoding/json/v2_indent.go b/src/encoding/json/v2_indent.go
index 2655942..b2e8518 100644
--- a/src/encoding/json/v2_indent.go
+++ b/src/encoding/json/v2_indent.go
@@ -88,17 +88,8 @@
}

func appendIndent(dst, src []byte, prefix, indent string) ([]byte, error) {
- // In v2, trailing whitespace is discarded, while v1 preserved it.
- dstLen := len(dst)
- if n := len(src) - len(bytes.TrimRight(src, " \n\r\t")); n > 0 {
- // Append the trailing whitespace afterwards.
- defer func() {
- if len(dst) > dstLen {
- dst = append(dst, src[len(src)-n:]...)
- }
- }()
- }
// In v2, only spaces and tabs are allowed, while v1 allowed any character.
+ dstLen := len(dst)
if len(strings.Trim(prefix, " \t"))+len(strings.Trim(indent, " \t")) > 0 {
// Use placeholder spaces of correct length, and replace afterwards.
invalidPrefix, invalidIndent := prefix, indent
@@ -129,5 +120,10 @@
if err != nil {
return dst[:dstLen], transformSyntacticError(err)
}
+
+ // In v2, trailing whitespace is discarded, while v1 preserved it.
+ if n := len(src) - len(bytes.TrimRight(src, " \n\r\t")); n > 0 {
+ dst = append(dst, src[len(src)-n:]...)
+ }
return dst, nil
}
diff --git a/src/encoding/json/v2_scanner_test.go b/src/encoding/json/v2_scanner_test.go
index bec5521..8885520 100644
--- a/src/encoding/json/v2_scanner_test.go
+++ b/src/encoding/json/v2_scanner_test.go
@@ -74,6 +74,7 @@
-5e+2
]`},
{Name(""), "{\"\":\"<>&\u2028\u2029\"}", "{\n\t\"\": \"<>&\u2028\u2029\"\n}"}, // See golang.org/issue/34070
+ {Name(""), `null`, "null \n\r\t"}, // See golang.org/issue/13520 and golang.org/issue/74806
}
var buf bytes.Buffer
for _, tt := range tests {
@@ -102,7 +103,7 @@
buf.Reset()
if err := Indent(&buf, []byte(tt.compact), "", "\t"); err != nil {
t.Errorf("%s: Indent error: %v", tt.Where, err)
- } else if got := buf.String(); got != tt.indent {
+ } else if got := buf.String(); got != strings.TrimRight(tt.indent, " \n\r\t") {
t.Errorf("%s: Compact:\n\tgot: %s\n\twant: %s", tt.Where, indentNewlines(got), indentNewlines(tt.indent))
}
})

Change information

Files:
  • M src/encoding/json/scanner_test.go
  • M src/encoding/json/v2_indent.go
  • M src/encoding/json/v2_scanner_test.go
Change size: S
Delta: 3 files changed, 10 insertions(+), 12 deletions(-)
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: I22b1a8da5185eb969e2a8a111b625d3752cfcbe8
Gerrit-Change-Number: 692195
Gerrit-PatchSet: 1
Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
unsatisfied_requirement
satisfied_requirement
open
diffy

Joseph Tsai (Gerrit)

unread,
Jul 31, 2025, 7:25:12 PMJul 31
to goph...@pubsubhelper.golang.org, Damien Neil, golang-co...@googlegroups.com
Attention needed from Damien Neil

Joseph Tsai voted

Auto-Submit+1
Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Damien Neil
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: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I22b1a8da5185eb969e2a8a111b625d3752cfcbe8
Gerrit-Change-Number: 692195
Gerrit-PatchSet: 1
Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Comment-Date: Thu, 31 Jul 2025 23:25:09 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Sean Liao (Gerrit)

unread,
Aug 8, 2025, 1:48:27 PMAug 8
to Joseph Tsai, goph...@pubsubhelper.golang.org, Go LUCI, Damien Neil, golang-co...@googlegroups.com
Attention needed from Damien Neil and Joseph Tsai

Sean Liao voted

Auto-Submit+1
Code-Review+2
Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Damien Neil
  • Joseph Tsai
Submit Requirements:
  • requirement 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: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I22b1a8da5185eb969e2a8a111b625d3752cfcbe8
Gerrit-Change-Number: 692195
Gerrit-PatchSet: 2
Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Sean Liao <se...@liao.dev>
Gerrit-Attention: Joseph Tsai <joe...@digital-static.net>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Comment-Date: Fri, 08 Aug 2025 17:48:19 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Dmitri Shuralyov (Gerrit)

unread,
Aug 8, 2025, 5:11:59 PMAug 8
to Joseph Tsai, goph...@pubsubhelper.golang.org, Dmitri Shuralyov, Go LUCI, Damien Neil, golang-co...@googlegroups.com
Attention needed from Damien Neil and Joseph Tsai

Dmitri Shuralyov voted Code-Review+1

Code-Review+1
Open in Gerrit

Related details

Attention is currently required from:
  • Damien Neil
  • Joseph Tsai
Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement 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: I22b1a8da5185eb969e2a8a111b625d3752cfcbe8
    Gerrit-Change-Number: 692195
    Gerrit-PatchSet: 2
    Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Dmitri Shuralyov <dmit...@google.com>
    Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Sean Liao <se...@liao.dev>
    Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
    Gerrit-Attention: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Attention: Damien Neil <dn...@google.com>
    Gerrit-Comment-Date: Fri, 08 Aug 2025 21:11:56 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    David Chase (Gerrit)

    unread,
    Aug 11, 2025, 3:16:23 PMAug 11
    to Joseph Tsai, goph...@pubsubhelper.golang.org, Dmitri Shuralyov, Dmitri Shuralyov, Go LUCI, Damien Neil, golang-co...@googlegroups.com
    Attention needed from Damien Neil and Joseph Tsai

    David Chase voted Code-Review+1

    Code-Review+1
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Damien Neil
    • Joseph Tsai
    Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement satisfiedReview-Enforcement
      • requirement 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: I22b1a8da5185eb969e2a8a111b625d3752cfcbe8
      Gerrit-Change-Number: 692195
      Gerrit-PatchSet: 2
      Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: David Chase <drc...@google.com>
      Gerrit-Reviewer: Dmitri Shuralyov <dmit...@google.com>
      Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Sean Liao <se...@liao.dev>
      Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
      Gerrit-Attention: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Attention: Damien Neil <dn...@google.com>
      Gerrit-Comment-Date: Mon, 11 Aug 2025 19:16:18 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      open
      diffy

      Gopher Robot (Gerrit)

      unread,
      Aug 11, 2025, 3:17:55 PMAug 11
      to Joseph Tsai, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, David Chase, Dmitri Shuralyov, Dmitri Shuralyov, Go LUCI, Damien Neil, golang-co...@googlegroups.com

      Gopher Robot submitted the change

      Change information

      Commit message:
      encoding/json: fix Indent trailing whitespace regression in goexperiment.jsonv2

      The Indent function preserves trailing whitespace,
      while the v1 emulation under v2 implementation accidentally dropped it.

      There was prior logic that attempted to preserve it,
      but it did not work correctly since it ran in a defer and
      accidentally mutated the dst input argument rather than the output argument.
      Move the logic to the end and avoid a defer.
      Also, add a test to both v1 and v1in2 to codify this behavior.

      This only modifies code that is compiled in under goexperiment.jsonv2.

      Updates #13520
      Fixes #74806
      Change-Id: I22b1a8da5185eb969e2a8a111b625d3752cfcbe8
      Reviewed-by: Sean Liao <se...@liao.dev>
      Reviewed-by: Dmitri Shuralyov <dmit...@google.com>
      Reviewed-by: David Chase <drc...@google.com>
      Auto-Submit: Sean Liao <se...@liao.dev>
      Files:
      • M src/encoding/json/scanner_test.go
      • M src/encoding/json/v2_indent.go
      • M src/encoding/json/v2_scanner_test.go
      Change size: S
      Delta: 3 files changed, 10 insertions(+), 12 deletions(-)
      Branch: refs/heads/master
      Submit Requirements:
      • requirement satisfiedCode-Review: +1 by Dmitri Shuralyov, +1 by David Chase, +2 by Sean Liao
      • requirement satisfiedTryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
      Open in Gerrit
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: merged
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I22b1a8da5185eb969e2a8a111b625d3752cfcbe8
      Gerrit-Change-Number: 692195
      Gerrit-PatchSet: 3
      Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: David Chase <drc...@google.com>
      Gerrit-Reviewer: Dmitri Shuralyov <dmit...@google.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      open
      diffy
      satisfied_requirement
      Reply all
      Reply to author
      Forward
      0 new messages