Skip to content

Commit a1c9557

Browse files
committed
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 protected]>
1 parent 44a7e73 commit a1c9557

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

reflect_map.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
320320
}
321321
stream.Write(keyValue.keyValue)
322322
}
323+
if subStream.Error != nil && stream.Error == nil {
324+
stream.Error = subStream.Error
325+
}
323326
stream.WriteObjectEnd()
324327
stream.cfg.ReturnStream(subStream)
325328
stream.cfg.ReturnIterator(subIter)

value_tests/invalid_test.go

Lines changed: 32 additions & 6 deletions
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)