1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-20 11:28:49 +02:00

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>
This commit is contained in:
Jens Erat 2019-11-22 16:45:18 +01:00
parent 44a7e7340d
commit a1c9557592
No known key found for this signature in database
GPG Key ID: 8E78E44DFB1B55E9
2 changed files with 35 additions and 6 deletions

View File

@ -320,6 +320,9 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
} }
stream.Write(keyValue.keyValue) stream.Write(keyValue.keyValue)
} }
if subStream.Error != nil && stream.Error == nil {
stream.Error = subStream.Error
}
stream.WriteObjectEnd() stream.WriteObjectEnd()
stream.cfg.ReturnStream(subStream) stream.cfg.ReturnStream(subStream)
stream.cfg.ReturnIterator(subIter) stream.cfg.ReturnIterator(subIter)

View File

@ -103,18 +103,44 @@ func Test_invalid_float(t *testing.T) {
} }
func Test_chan(t *testing.T) { func Test_chan(t *testing.T) {
t.Skip("do not support chan")
type TestObject struct { type TestObject struct {
MyChan chan bool MyChan chan bool
MyField int MyField int
} }
should := require.New(t)
obj := TestObject{} obj := TestObject{}
str, err := json.Marshal(obj)
should.Nil(err) t.Run("Encode channel", func(t *testing.T) {
should.Equal(``, str) 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) { func Test_invalid_number(t *testing.T) {