Skip to content

Commit 358cfc3

Browse files
committed
Merge branch 'master' of https://github.com/json-iterator/go
2 parents c39a632 + e31252f commit 358cfc3

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

feature_reflect_array.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,30 @@ func decoderOfArray(cfg *frozenConfig, prefix string, typ reflect.Type) ValDecod
1313
}
1414

1515
func encoderOfArray(cfg *frozenConfig, prefix string, typ reflect.Type) ValEncoder {
16+
if typ.Len() == 0 {
17+
return emptyArrayEncoder{}
18+
}
1619
encoder := encoderOfType(cfg, prefix+"[array]->", typ.Elem())
1720
if typ.Elem().Kind() == reflect.Map {
1821
encoder = &OptionalEncoder{encoder}
1922
}
2023
return &arrayEncoder{typ, typ.Elem(), encoder}
2124
}
2225

26+
type emptyArrayEncoder struct{}
27+
28+
func (encoder emptyArrayEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
29+
stream.WriteEmptyArray()
30+
}
31+
32+
func (encoder emptyArrayEncoder) EncodeInterface(val interface{}, stream *Stream) {
33+
stream.WriteEmptyArray()
34+
}
35+
36+
func (encoder emptyArrayEncoder) IsEmpty(ptr unsafe.Pointer) bool {
37+
return true
38+
}
39+
2340
type arrayEncoder struct {
2441
arrayType reflect.Type
2542
elemType reflect.Type

jsoniter_fixed_array_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ func Test_encode_fixed_array(t *testing.T) {
1515
should.Equal("[0.1,1]", output)
1616
}
1717

18+
func Test_encode_fixed_array_empty(t *testing.T) {
19+
should := require.New(t)
20+
type FixedArray [0]float64
21+
fixed := FixedArray{}
22+
output, err := MarshalToString(fixed)
23+
should.Nil(err)
24+
should.Equal("[]", output)
25+
}
26+
1827
func Test_encode_fixed_array_of_map(t *testing.T) {
1928
should := require.New(t)
2029
type FixedArray [2]map[string]string

0 commit comments

Comments
 (0)