1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-15 22:50:24 +02:00

13 Commits

Author SHA1 Message Date
acfec88f7a Merge pull request #422 from JensErat/map-invalid-type
pass nested error in compatible configuration, fixes #388
2019-12-21 11:10:28 +08:00
e88512faf8 Merge pull request #423 from vano144/fix-attachments-on-stream
fix nil attachment on stream in custom encoder on sorted map
2019-12-21 11:09:53 +08:00
b681149eae Merge pull request #424 from aaronbee/sortKeysMapAllocations
Reduce allocations in sortKeysMapEncoder
2019-12-21 11:09:15 +08:00
d1af7639b3 Merge pull request #425 from liggitt/default-max-depth
Revert "Merge pull request #418 from bbrks/configurable_maxDepth"
2019-12-21 11:04:54 +08:00
7c9f8c2d20 Revert "Merge pull request #418 from bbrks/configurable_maxDepth"
This reverts commit 44a7e7340d, reversing
changes made to dc11f49689.
2019-12-19 19:06:29 -05:00
f814d6c0f1 Reduce allocations in sortKeysMapEncoder
Use one buffer for all values.
2019-12-03 11:55:47 -08:00
aba8654400 fix nil attachment on stream in custom encoder on sorted map 2019-11-28 17:39:42 +03:00
a1c9557592 pass nested error in compatible configuration
When invalid types inside a map were marshalled (in general, as soon as
sorted maps have been configured), the error message has not been
propagated out of the map's `subStream`.

Also fix and re-enable the channel test, which now resembles the
behavior of `encoding/json` and tests both default and compatible
configurations.

Signed-off-by: Jens Erat <email@jenserat.de>
2019-11-22 16:56:59 +01:00
44a7e7340d Merge pull request #418 from bbrks/configurable_maxDepth
Add MaxDepth as a config option
2019-11-12 22:47:28 +08:00
2834c7e43c Remove large test values that fail on 32-bit architectures 2019-11-11 16:35:50 +00:00
d296277d5c Adds MaxDepth config option
Defaults to 10,000 to match the existing maxDepth constant everywhetre,
except when using `ConfigCompatibleWithStandardLibrary` - which retains
the limitless depth (and causes a stack overflow).

Added tests for the new config, and also up to jsoniter's stack overflow limit.
2019-11-11 16:13:59 +00:00
dc11f49689 Merge pull request #416 from jarredhawkins/issue-415
Ignore unnamed literals in structs
2019-10-30 08:35:33 +08:00
83f7b825b3 Unnamed struct literals 2019-10-28 23:05:10 -07:00
6 changed files with 79 additions and 10 deletions

View File

@ -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

View File

@ -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, ",")

View File

@ -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)

View File

@ -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()))

View File

@ -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{}
}

View File

@ -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) {