Skip to content

Commit 9aaaa4b

Browse files
json: pass remaining cycle/unsupported-value tests
1 parent 396975c commit 9aaaa4b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

json/codec.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,21 @@ func constructSliceCodec(t reflect.Type, seen seenMap) *codec {
366366
}
367367

368368
inner := constructCodec(e, seen, true)
369+
370+
// If encode/decode functions are nil, that means this is a recursive type,
371+
// and so we can use delayed binding to define self-referential calls.
372+
if inner.encode == nil {
373+
inner.encode = func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
374+
return c.encode(e, b, p)
375+
}
376+
}
377+
378+
if inner.decode == nil {
379+
inner.decode = func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
380+
return c.decode(d, b, p)
381+
}
382+
}
383+
369384
c.encode = constructSliceEncodeFunc(s, t, inner.encode)
370385
c.decode = constructSliceDecodeFunc(s, t, inner.decode)
371386

json/encode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func (e encoder) encodeTime(b []byte, p unsafe.Pointer) ([]byte, error) {
297297

298298
func (e encoder) encodeArray(b []byte, p unsafe.Pointer, n int, size uintptr, t reflect.Type, encode encodeFunc) ([]byte, error) {
299299
if shouldCheckForRefCycle(&e) {
300-
key := cycleKey{ptr: p}
300+
key := cycleKey{ptr: p, len: n}
301301
if hasRefCycle(&e, key) {
302302
return b, refCycleError(t, p)
303303
}

0 commit comments

Comments
 (0)