Skip to content

Commit acfec88

Browse files
authored
Merge pull request #422 from JensErat/map-invalid-type
pass nested error in compatible configuration, fixes #388
2 parents e88512f + a1c9557 commit acfec88

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

reflect_map.go

+3
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
321321
}
322322
stream.Write(keyValue.keyValue)
323323
}
324+
if subStream.Error != nil && stream.Error == nil {
325+
stream.Error = subStream.Error
326+
}
324327
stream.WriteObjectEnd()
325328
stream.cfg.ReturnStream(subStream)
326329
stream.cfg.ReturnIterator(subIter)

value_tests/invalid_test.go

+32-6
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,44 @@ func Test_invalid_float(t *testing.T) {
103103
}
104104

105105
func Test_chan(t *testing.T) {
106-
t.Skip("do not support chan")
107-
108106
type TestObject struct {
109107
MyChan chan bool
110108
MyField int
111109
}
112110

113-
should := require.New(t)
114111
obj := TestObject{}
115-
str, err := json.Marshal(obj)
116-
should.Nil(err)
117-
should.Equal(``, str)
112+
113+
t.Run("Encode channel", func(t *testing.T) {
114+
should := require.New(t)
115+
str, err := jsoniter.Marshal(obj)
116+
should.NotNil(err)
117+
should.Nil(str)
118+
})
119+
120+
t.Run("Encode channel using compatible configuration", func(t *testing.T) {
121+
should := require.New(t)
122+
str, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(obj)
123+
should.NotNil(err)
124+
should.Nil(str)
125+
})
126+
}
127+
128+
func Test_invalid_in_map(t *testing.T) {
129+
testMap := map[string]interface{}{"chan": make(chan interface{})}
130+
131+
t.Run("Encode map with invalid content", func(t *testing.T) {
132+
should := require.New(t)
133+
str, err := jsoniter.Marshal(testMap)
134+
should.NotNil(err)
135+
should.Nil(str)
136+
})
137+
138+
t.Run("Encode map with invalid content using compatible configuration", func(t *testing.T) {
139+
should := require.New(t)
140+
str, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(testMap)
141+
should.NotNil(err)
142+
should.Nil(str)
143+
})
118144
}
119145

120146
func Test_invalid_number(t *testing.T) {

0 commit comments

Comments
 (0)