Skip to content

Commit e88512f

Browse files
authored
Merge pull request #423 from vano144/fix-attachments-on-stream
fix nil attachment on stream in custom encoder on sorted map
2 parents b681149 + aba8654 commit e88512f

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

extension_tests/decoder_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package test
22

33
import (
44
"bytes"
5+
"fmt"
56
"github.com/json-iterator/go"
67
"github.com/stretchr/testify/require"
78
"strconv"
@@ -47,6 +48,38 @@ func Test_customize_byte_array_encoder(t *testing.T) {
4748
should.Equal(`"abc"`, str)
4849
}
4950

51+
type CustomEncoderAttachmentTestStruct struct {
52+
Value int32 `json:"value"`
53+
}
54+
55+
type CustomEncoderAttachmentTestStructEncoder struct {}
56+
57+
func (c *CustomEncoderAttachmentTestStructEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
58+
attachVal, ok := stream.Attachment.(int)
59+
stream.WriteRaw(`"`)
60+
stream.WriteRaw(fmt.Sprintf("%t %d", ok, attachVal))
61+
stream.WriteRaw(`"`)
62+
}
63+
64+
func (c *CustomEncoderAttachmentTestStructEncoder) IsEmpty(ptr unsafe.Pointer) bool {
65+
return false
66+
}
67+
68+
func Test_custom_encoder_attachment(t *testing.T) {
69+
70+
jsoniter.RegisterTypeEncoder("test.CustomEncoderAttachmentTestStruct", &CustomEncoderAttachmentTestStructEncoder{})
71+
expectedValue := 17
72+
should := require.New(t)
73+
buf := &bytes.Buffer{}
74+
stream := jsoniter.NewStream(jsoniter.Config{SortMapKeys: true}.Froze(), buf, 4096)
75+
stream.Attachment = expectedValue
76+
val := map[string]CustomEncoderAttachmentTestStruct{"a": {}}
77+
stream.WriteVal(val)
78+
stream.Flush()
79+
should.Nil(stream.Error)
80+
should.Equal("{\"a\":\"true 17\"}", buf.String())
81+
}
82+
5083
func Test_customize_field_decoder(t *testing.T) {
5184
type Tom struct {
5285
field1 string

reflect_map.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
290290
stream.WriteObjectStart()
291291
mapIter := encoder.mapType.UnsafeIterate(ptr)
292292
subStream := stream.cfg.BorrowStream(nil)
293+
subStream.Attachment = stream.Attachment
293294
subIter := stream.cfg.BorrowIterator(nil)
294295
keyValues := encodedKeyValues{}
295296
for mapIter.HasNext() {

reflect_struct_encoder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ type stringModeStringEncoder struct {
200200

201201
func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
202202
tempStream := encoder.cfg.BorrowStream(nil)
203+
tempStream.Attachment = stream.Attachment
203204
defer encoder.cfg.ReturnStream(tempStream)
204205
encoder.elemEncoder.Encode(ptr, tempStream)
205206
stream.WriteString(string(tempStream.Buffer()))

0 commit comments

Comments
 (0)