Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix MarshalIndent when use SortMapKeys #623

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions api_tests/marshal_indent_test.go
Expand Up @@ -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))
}
1 change: 1 addition & 0 deletions pool.go
Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions reflect_map.go
Expand Up @@ -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() {
Expand Down