Skip to content

Commit

Permalink
Fixes #7 - printing JSON scalar values.
Browse files Browse the repository at this point in the history
Signed-off-by: Springcomp <springcomp@users.noreply.github.com>
  • Loading branch information
springcomp committed Mar 21, 2023
1 parent bed50f1 commit f6ab3ff
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 f6ab3ff

Please sign in to comment.