mirror of https://github.com/goccy/go-json.git
fix: fixed a problem that MarshalIndent does not work when UnorderedMap is specified (#435)
This commit is contained in:
parent
2ef15e72f8
commit
f32a307caf
|
@ -2630,6 +2630,22 @@ func TestCustomMarshalForMapKey(t *testing.T) {
|
|||
assertEq(t, "custom map key", string(expected), string(got))
|
||||
}
|
||||
|
||||
func TestIssue417(t *testing.T) {
|
||||
x := map[string]string{
|
||||
"b": "b",
|
||||
"a": "a",
|
||||
}
|
||||
b, err := json.MarshalIndentWithOption(x, "", " ", json.UnorderedMap())
|
||||
assertErr(t, err)
|
||||
|
||||
var y map[string]string
|
||||
err = json.Unmarshal(b, &y)
|
||||
assertErr(t, err)
|
||||
|
||||
assertEq(t, "key b", "b", y["b"])
|
||||
assertEq(t, "key a", "a", y["a"])
|
||||
}
|
||||
|
||||
func TestIssue426(t *testing.T) {
|
||||
type I interface {
|
||||
Foo()
|
||||
|
|
|
@ -189,7 +189,7 @@ func appendNullComma(ctx *encoder.RuntimeContext, b []byte) []byte {
|
|||
}
|
||||
|
||||
func appendColon(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, ':', ' ')
|
||||
return append(b[:len(b)-2], ':', ' ')
|
||||
}
|
||||
|
||||
func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte {
|
||||
|
@ -229,8 +229,9 @@ func appendEmptyObject(_ *encoder.RuntimeContext, b []byte) []byte {
|
|||
|
||||
func appendObjectEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
|
||||
last := len(b) - 1
|
||||
b[last] = '\n'
|
||||
b = appendIndent(ctx, b, code.Indent-1)
|
||||
// replace comma to newline
|
||||
b[last-1] = '\n'
|
||||
b = appendIndent(ctx, b[:last], code.Indent)
|
||||
return append(b, '}', ',', '\n')
|
||||
}
|
||||
|
||||
|
|
|
@ -133,7 +133,7 @@ func appendNullComma(_ *encoder.RuntimeContext, b []byte) []byte {
|
|||
}
|
||||
|
||||
func appendColon(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, ':', ' ')
|
||||
return append(b[:len(b)-2], ':', ' ')
|
||||
}
|
||||
|
||||
func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte {
|
||||
|
@ -173,8 +173,9 @@ func appendEmptyObject(_ *encoder.RuntimeContext, b []byte) []byte {
|
|||
|
||||
func appendObjectEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
|
||||
last := len(b) - 1
|
||||
b[last] = '\n'
|
||||
b = appendIndent(ctx, b, code.Indent-1)
|
||||
// replace comma to newline
|
||||
b[last-1] = '\n'
|
||||
b = appendIndent(ctx, b[:last], code.Indent)
|
||||
return append(b, '}', ',', '\n')
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue