Skip to content

Commit 2842ec9

Browse files
committed
Fix #58: Improve handling of array and object literals and trailing/leading spaces
1 parent e7199ff commit 2842ec9

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

internal/utils/utils.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -409,15 +409,6 @@ func FormatJson(reader io.Reader, writer io.Writer, indent string, colors int) e
409409
v := reflect.ValueOf(*decoder)
410410
tokenState := v.FieldByName("tokenState").Int()
411411

412-
switch tokenState {
413-
case jsonTokenObjectColon:
414-
suffix = ": "
415-
case jsonTokenObjectComma:
416-
suffix = "," + newline + strings.Repeat(indent, level)
417-
case jsonTokenArrayComma:
418-
suffix = "," + newline + strings.Repeat(indent, level)
419-
}
420-
421412
switch tokenType := token.(type) {
422413
case json.Delim:
423414
switch rune(tokenType) {
@@ -455,6 +446,15 @@ func FormatJson(reader io.Reader, writer io.Writer, indent string, colors int) e
455446
_, _ = fmt.Fprintf(writer, "%s%s", prefix, valueColor("null"))
456447
}
457448

449+
switch tokenState {
450+
case jsonTokenObjectColon:
451+
suffix = ": "
452+
case jsonTokenObjectComma:
453+
suffix = "," + newline + strings.Repeat(indent, level)
454+
case jsonTokenArrayComma:
455+
suffix = "," + newline + strings.Repeat(indent, level)
456+
}
457+
458458
prefix = suffix
459459
}
460460

internal/utils/utils_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ func TestFormatHtml(t *testing.T) {
7979

8080
func TestFormatJson(t *testing.T) {
8181
files := map[string]string{
82-
"unformatted.json": "formatted.json",
82+
"unformatted.json": "formatted.json",
83+
"unformatted2.json": "formatted2.json",
8384
}
8485

8586
for unformattedFile, expectedFile := range files {

test/data/json/formatted2.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"fields": {
3+
"fixVersions": [
4+
"1.0.0"
5+
],
6+
"customfield_10473": null,
7+
"customfield_10474": null
8+
}
9+
}

test/data/json/unformatted2.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"fields": {
3+
"fixVersions": [
4+
"1.0.0"
5+
],
6+
"customfield_10473": null,
7+
"customfield_10474": null
8+
}
9+
}

0 commit comments

Comments
 (0)