From ae56d8371d489b15e2a948d43b2049e083d21123 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sat, 10 Apr 2021 15:25:02 +0900 Subject: [PATCH] Fix unnecessary indent --- encode_test.go | 42 ++++++++++++++++++++++++ internal/encoder/vm_escaped_indent/vm.go | 2 -- internal/encoder/vm_indent/vm.go | 2 -- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/encode_test.go b/encode_test.go index 1e1b3c5..54fbddf 100644 --- a/encode_test.go +++ b/encode_test.go @@ -1805,3 +1805,45 @@ func TestIssue104(t *testing.T) { t.Fatalf("expect %q but got %q", string(expect), string(got)) } } + +func TestIssue180(t *testing.T) { + v := struct { + T struct { + T1 bool `json:"t1"` + T2 float64 `json:"t2"` + T3 string `json:"t3"` + T4 []string `json:"t4"` + T5 *struct{} `json:"t5"` + T6 interface{} `json:"t6"` + T7 [][]string `json:"t7"` + } `json:"t"` + }{ + T: struct { + T1 bool `json:"t1"` + T2 float64 `json:"t2"` + T3 string `json:"t3"` + T4 []string `json:"t4"` + T5 *struct{} `json:"t5"` + T6 interface{} `json:"t6"` + T7 [][]string `json:"t7"` + }{ + T4: []string{}, + T7: [][]string{ + []string{""}, + []string{"hello", "world"}, + []string{}, + }, + }, + } + b1, err := stdjson.MarshalIndent(v, "", "\t") + if err != nil { + t.Fatal(err) + } + b2, err := json.MarshalIndent(v, "", "\t") + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(b1, b2) { + t.Fatalf("failed to equal encoded result: expected %s but got %s", string(b1), string(b2)) + } +} diff --git a/internal/encoder/vm_escaped_indent/vm.go b/internal/encoder/vm_escaped_indent/vm.go index b221382..23f2cbf 100644 --- a/internal/encoder/vm_escaped_indent/vm.go +++ b/internal/encoder/vm_escaped_indent/vm.go @@ -338,7 +338,6 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next store(ctxptr, code.Idx, uintptr(slice.Data)) } else { - b = appendIndent(ctx, b, code.Indent) b = append(b, '[', ']', ',', '\n') code = code.End.Next } @@ -385,7 +384,6 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next store(ctxptr, code.Idx, p) } else { - b = appendIndent(ctx, b, code.Indent) b = append(b, '[', ']', ',', '\n') code = code.End.Next } diff --git a/internal/encoder/vm_indent/vm.go b/internal/encoder/vm_indent/vm.go index fdad09e..4406462 100644 --- a/internal/encoder/vm_indent/vm.go +++ b/internal/encoder/vm_indent/vm.go @@ -338,7 +338,6 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next store(ctxptr, code.Idx, uintptr(slice.Data)) } else { - b = appendIndent(ctx, b, code.Indent) b = append(b, '[', ']', ',', '\n') code = code.End.Next } @@ -385,7 +384,6 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next store(ctxptr, code.Idx, p) } else { - b = appendIndent(ctx, b, code.Indent) b = append(b, '[', ']', ',', '\n') code = code.End.Next }