Merge pull request #181 from goccy/feature/fix-180

Fix unnecessary indent for empty slice/array
This commit is contained in:
Masaaki Goshima 2021-04-10 15:36:18 +09:00 committed by GitHub
commit ca141f38b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 4 deletions

View File

@ -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))
}
}

View File

@ -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
}

View File

@ -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
}