Skip to content

Commit f32a307

Browse files
authored
fix: fixed a problem that MarshalIndent does not work when UnorderedMap is specified (#435)
1 parent 2ef15e7 commit f32a307

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

encode_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,6 +2630,22 @@ func TestCustomMarshalForMapKey(t *testing.T) {
26302630
assertEq(t, "custom map key", string(expected), string(got))
26312631
}
26322632

2633+
func TestIssue417(t *testing.T) {
2634+
x := map[string]string{
2635+
"b": "b",
2636+
"a": "a",
2637+
}
2638+
b, err := json.MarshalIndentWithOption(x, "", " ", json.UnorderedMap())
2639+
assertErr(t, err)
2640+
2641+
var y map[string]string
2642+
err = json.Unmarshal(b, &y)
2643+
assertErr(t, err)
2644+
2645+
assertEq(t, "key b", "b", y["b"])
2646+
assertEq(t, "key a", "a", y["a"])
2647+
}
2648+
26332649
func TestIssue426(t *testing.T) {
26342650
type I interface {
26352651
Foo()

internal/encoder/vm_color_indent/util.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func appendNullComma(ctx *encoder.RuntimeContext, b []byte) []byte {
189189
}
190190

191191
func appendColon(_ *encoder.RuntimeContext, b []byte) []byte {
192-
return append(b, ':', ' ')
192+
return append(b[:len(b)-2], ':', ' ')
193193
}
194194

195195
func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte {
@@ -229,8 +229,9 @@ func appendEmptyObject(_ *encoder.RuntimeContext, b []byte) []byte {
229229

230230
func appendObjectEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
231231
last := len(b) - 1
232-
b[last] = '\n'
233-
b = appendIndent(ctx, b, code.Indent-1)
232+
// replace comma to newline
233+
b[last-1] = '\n'
234+
b = appendIndent(ctx, b[:last], code.Indent)
234235
return append(b, '}', ',', '\n')
235236
}
236237

internal/encoder/vm_indent/util.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func appendNullComma(_ *encoder.RuntimeContext, b []byte) []byte {
133133
}
134134

135135
func appendColon(_ *encoder.RuntimeContext, b []byte) []byte {
136-
return append(b, ':', ' ')
136+
return append(b[:len(b)-2], ':', ' ')
137137
}
138138

139139
func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte {
@@ -173,8 +173,9 @@ func appendEmptyObject(_ *encoder.RuntimeContext, b []byte) []byte {
173173

174174
func appendObjectEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
175175
last := len(b) - 1
176-
b[last] = '\n'
177-
b = appendIndent(ctx, b, code.Indent-1)
176+
// replace comma to newline
177+
b[last-1] = '\n'
178+
b = appendIndent(ctx, b[:last], code.Indent)
178179
return append(b, '}', ',', '\n')
179180
}
180181

0 commit comments

Comments
 (0)