mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
fix nil attachment on stream in custom encoder on sorted map
This commit is contained in:
parent
44a7e7340d
commit
aba8654400
@ -2,6 +2,7 @@ package test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"github.com/json-iterator/go"
|
"github.com/json-iterator/go"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -47,6 +48,38 @@ func Test_customize_byte_array_encoder(t *testing.T) {
|
|||||||
should.Equal(`"abc"`, str)
|
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) {
|
func Test_customize_field_decoder(t *testing.T) {
|
||||||
type Tom struct {
|
type Tom struct {
|
||||||
field1 string
|
field1 string
|
||||||
|
@ -290,6 +290,7 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
|||||||
stream.WriteObjectStart()
|
stream.WriteObjectStart()
|
||||||
mapIter := encoder.mapType.UnsafeIterate(ptr)
|
mapIter := encoder.mapType.UnsafeIterate(ptr)
|
||||||
subStream := stream.cfg.BorrowStream(nil)
|
subStream := stream.cfg.BorrowStream(nil)
|
||||||
|
subStream.Attachment = stream.Attachment
|
||||||
subIter := stream.cfg.BorrowIterator(nil)
|
subIter := stream.cfg.BorrowIterator(nil)
|
||||||
keyValues := encodedKeyValues{}
|
keyValues := encodedKeyValues{}
|
||||||
for mapIter.HasNext() {
|
for mapIter.HasNext() {
|
||||||
|
@ -200,6 +200,7 @@ type stringModeStringEncoder struct {
|
|||||||
|
|
||||||
func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
tempStream := encoder.cfg.BorrowStream(nil)
|
tempStream := encoder.cfg.BorrowStream(nil)
|
||||||
|
tempStream.Attachment = stream.Attachment
|
||||||
defer encoder.cfg.ReturnStream(tempStream)
|
defer encoder.cfg.ReturnStream(tempStream)
|
||||||
encoder.elemEncoder.Encode(ptr, tempStream)
|
encoder.elemEncoder.Encode(ptr, tempStream)
|
||||||
stream.WriteString(string(tempStream.Buffer()))
|
stream.WriteString(string(tempStream.Buffer()))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user