From 8084dc708932a2ccf81765a28b901425c076e1e6 Mon Sep 17 00:00:00 2001 From: kkHAIKE Date: Tue, 19 Jul 2022 11:16:24 +0800 Subject: [PATCH] fix MarshalIndent when use SortMapKeys --- api_tests/marshal_indent_test.go | 11 +++++++++++ pool.go | 1 + reflect_map.go | 1 + 3 files changed, 13 insertions(+) diff --git a/api_tests/marshal_indent_test.go b/api_tests/marshal_indent_test.go index 926931a8..38f896fb 100644 --- a/api_tests/marshal_indent_test.go +++ b/api_tests/marshal_indent_test.go @@ -34,3 +34,14 @@ func Test_marshal_indent_map(t *testing.T) { should.Nil(err) should.Equal("{\n \"1\": 2\n}", string(output)) } + +func Test_marshal_indent_nested_sorted(t *testing.T) { + should := require.New(t) + obj := map[int]interface{}{ + 1: map[int]int{2: 3}, + } + ji := jsoniter.Config{SortMapKeys: true}.Froze() + output, err := ji.MarshalIndent(obj, "", " ") + should.Nil(err) + should.Equal("{\n \"1\": {\n \"2\": 3\n }\n}", string(output)) +} diff --git a/pool.go b/pool.go index e2389b56..e38b8714 100644 --- a/pool.go +++ b/pool.go @@ -26,6 +26,7 @@ func (cfg *frozenConfig) ReturnStream(stream *Stream) { stream.out = nil stream.Error = nil stream.Attachment = nil + stream.indention = 0 cfg.streamPool.Put(stream) } diff --git a/reflect_map.go b/reflect_map.go index 58296713..9e060b0b 100644 --- a/reflect_map.go +++ b/reflect_map.go @@ -291,6 +291,7 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) { mapIter := encoder.mapType.UnsafeIterate(ptr) subStream := stream.cfg.BorrowStream(nil) subStream.Attachment = stream.Attachment + subStream.indention = stream.indention subIter := stream.cfg.BorrowIterator(nil) keyValues := encodedKeyValues{} for mapIter.HasNext() {