You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-06-15 22:50:24 +02:00
Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
acfec88f7a | |||
e88512faf8 | |||
b681149eae | |||
d1af7639b3 | |||
7c9f8c2d20 | |||
f814d6c0f1 | |||
aba8654400 | |||
a1c9557592 | |||
44a7e7340d | |||
2834c7e43c | |||
d296277d5c | |||
dc11f49689 | |||
83f7b825b3 |
@ -2,6 +2,7 @@ package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/stretchr/testify/require"
|
||||
"strconv"
|
||||
@ -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
|
||||
|
@ -341,7 +341,7 @@ func describeStruct(ctx *ctx, typ reflect2.Type) *StructDescriptor {
|
||||
if ctx.onlyTaggedField && !hastag && !field.Anonymous() {
|
||||
continue
|
||||
}
|
||||
if tag == "-" {
|
||||
if tag == "-" || field.Name() == "_" {
|
||||
continue
|
||||
}
|
||||
tagParts := strings.Split(tag, ",")
|
||||
|
@ -290,16 +290,17 @@ 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() {
|
||||
subStream.buf = make([]byte, 0, 64)
|
||||
key, elem := mapIter.UnsafeNext()
|
||||
subStreamIndex := subStream.Buffered()
|
||||
encoder.keyEncoder.Encode(key, subStream)
|
||||
if subStream.Error != nil && subStream.Error != io.EOF && stream.Error == nil {
|
||||
stream.Error = subStream.Error
|
||||
}
|
||||
encodedKey := subStream.Buffer()
|
||||
encodedKey := subStream.Buffer()[subStreamIndex:]
|
||||
subIter.ResetBytes(encodedKey)
|
||||
decodedKey := subIter.ReadString()
|
||||
if stream.indention > 0 {
|
||||
@ -310,7 +311,7 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
encoder.elemEncoder.Encode(elem, subStream)
|
||||
keyValues = append(keyValues, encodedKV{
|
||||
key: decodedKey,
|
||||
keyValue: subStream.Buffer(),
|
||||
keyValue: subStream.Buffer()[subStreamIndex:],
|
||||
})
|
||||
}
|
||||
sort.Sort(keyValues)
|
||||
@ -320,6 +321,9 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
}
|
||||
stream.Write(keyValue.keyValue)
|
||||
}
|
||||
if subStream.Error != nil && stream.Error == nil {
|
||||
stream.Error = subStream.Error
|
||||
}
|
||||
stream.WriteObjectEnd()
|
||||
stream.cfg.ReturnStream(subStream)
|
||||
stream.cfg.ReturnIterator(subIter)
|
||||
|
@ -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()))
|
||||
|
@ -60,6 +60,7 @@ func init() {
|
||||
(*SameLevel2NoTags)(nil),
|
||||
(*SameLevel2Tagged)(nil),
|
||||
(*EmbeddedPtr)(nil),
|
||||
(*UnnamedLiteral)(nil),
|
||||
)
|
||||
}
|
||||
|
||||
@ -231,3 +232,7 @@ type EmbeddedPtrOption struct {
|
||||
type EmbeddedPtr struct {
|
||||
EmbeddedPtrOption `json:","`
|
||||
}
|
||||
|
||||
type UnnamedLiteral struct {
|
||||
_ struct{}
|
||||
}
|
||||
|
@ -103,18 +103,44 @@ func Test_invalid_float(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_chan(t *testing.T) {
|
||||
t.Skip("do not support chan")
|
||||
|
||||
type TestObject struct {
|
||||
MyChan chan bool
|
||||
MyField int
|
||||
}
|
||||
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
str, err := json.Marshal(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(``, str)
|
||||
|
||||
t.Run("Encode channel", func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
str, err := jsoniter.Marshal(obj)
|
||||
should.NotNil(err)
|
||||
should.Nil(str)
|
||||
})
|
||||
|
||||
t.Run("Encode channel using compatible configuration", func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
str, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(obj)
|
||||
should.NotNil(err)
|
||||
should.Nil(str)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_invalid_in_map(t *testing.T) {
|
||||
testMap := map[string]interface{}{"chan": make(chan interface{})}
|
||||
|
||||
t.Run("Encode map with invalid content", func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
str, err := jsoniter.Marshal(testMap)
|
||||
should.NotNil(err)
|
||||
should.Nil(str)
|
||||
})
|
||||
|
||||
t.Run("Encode map with invalid content using compatible configuration", func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
str, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(testMap)
|
||||
should.NotNil(err)
|
||||
should.Nil(str)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_invalid_number(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user