Skip to content

Commit

Permalink
Merge pull request #8 from springcomp/fix/scalar
Browse files Browse the repository at this point in the history
Fixed emitting unwanted leading space when printing JSON scalar values
  • Loading branch information
nwidger committed Mar 21, 2023
2 parents bed50f1 + f6ab3ff commit 9b1e990
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion jsoncolor.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,13 @@ func (fs *formatterState) format(dst io.Writer, src []byte, terminateWithNewline

frame := fs.frame()

// this variable indicates whether the original input
// is a JSON object or JSON array. This allows us to
// correctly print JSON scalar values such as strings,
// numbers, `true`, `false` and `null`.

inputIsObjectOrArray := false

for {
t, err := dec.Token()
if err == io.EOF {
Expand All @@ -599,6 +606,7 @@ func (fs *formatterState) format(dst io.Writer, src []byte, terminateWithNewline
printComma := frame.inArrayOrObject() && more

if x, ok := t.(json.Delim); ok {
inputIsObjectOrArray = inputIsObjectOrArray && true
if x == json.Delim('{') || x == json.Delim('[') {
if frame.inObject() {
fs.printSpace(" ", false)
Expand Down Expand Up @@ -633,7 +641,7 @@ func (fs *formatterState) format(dst io.Writer, src []byte, terminateWithNewline
if printIndent {
fs.printIndent()
}
if !frame.inField() {
if !frame.inField() && inputIsObjectOrArray {
fs.printSpace(" ", false)
}
err = fs.formatToken(t)
Expand Down

0 comments on commit 9b1e990

Please sign in to comment.