Skip to content

fix nil attachment on stream in custom encoder on sorted map #423

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

Merged
merged 1 commit into from
Dec 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions extension_tests/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"bytes"
"fmt"
"github.com/json-iterator/go"
"github.com/stretchr/testify/require"
"strconv"
Expand Down Expand Up @@ -47,6 +48,38 @@ func Test_customize_byte_array_encoder(t *testing.T) {
should.Equal(`"abc"`, str)
}

type CustomEncoderAttachmentTestStruct struct {
Value int32 `json:"value"`
}

type CustomEncoderAttachmentTestStructEncoder struct {}

func (c *CustomEncoderAttachmentTestStructEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
attachVal, ok := stream.Attachment.(int)
stream.WriteRaw(`"`)
stream.WriteRaw(fmt.Sprintf("%t %d", ok, attachVal))
stream.WriteRaw(`"`)
}

func (c *CustomEncoderAttachmentTestStructEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return false
}

func Test_custom_encoder_attachment(t *testing.T) {

jsoniter.RegisterTypeEncoder("test.CustomEncoderAttachmentTestStruct", &CustomEncoderAttachmentTestStructEncoder{})
expectedValue := 17
should := require.New(t)
buf := &bytes.Buffer{}
stream := jsoniter.NewStream(jsoniter.Config{SortMapKeys: true}.Froze(), buf, 4096)
stream.Attachment = expectedValue
val := map[string]CustomEncoderAttachmentTestStruct{"a": {}}
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
should.Equal("{\"a\":\"true 17\"}", buf.String())
}

func Test_customize_field_decoder(t *testing.T) {
type Tom struct {
field1 string
Expand Down
1 change: 1 addition & 0 deletions reflect_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
stream.WriteObjectStart()
mapIter := encoder.mapType.UnsafeIterate(ptr)
subStream := stream.cfg.BorrowStream(nil)
subStream.Attachment = stream.Attachment
subIter := stream.cfg.BorrowIterator(nil)
keyValues := encodedKeyValues{}
for mapIter.HasNext() {
Expand Down
1 change: 1 addition & 0 deletions reflect_struct_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ type stringModeStringEncoder struct {

func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
tempStream := encoder.cfg.BorrowStream(nil)
tempStream.Attachment = stream.Attachment
defer encoder.cfg.ReturnStream(tempStream)
encoder.elemEncoder.Encode(ptr, tempStream)
stream.WriteString(string(tempStream.Buffer()))
Expand Down