Skip to content

Commit

Permalink
Merge pull request #422 from JensErat/map-invalid-type
Browse files Browse the repository at this point in the history
pass nested error in compatible configuration, fixes #388
  • Loading branch information
taowen authored Dec 21, 2019
2 parents e88512f + a1c9557 commit acfec88
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
3 changes: 3 additions & 0 deletions reflect_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,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)
Expand Down
38 changes: 32 additions & 6 deletions value_tests/invalid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit acfec88

Please sign in to comment.