File tree Expand file tree Collapse file tree
main/java/com/jsoniter/spi Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33import com .jsoniter .CodegenAccess ;
44import com .jsoniter .JsonIterator ;
5+ import com .jsoniter .ValueType ;
56
67import java .io .IOException ;
78
@@ -244,4 +245,21 @@ public double decodeDouble(JsonIterator iter) throws IOException {
244245 return val ;
245246 }
246247 }
248+
249+ class MaybeEmptyArrayDecoder implements Decoder {
250+
251+ @ Override
252+ public Object decode (JsonIterator iter ) throws IOException {
253+ if (iter .whatIsNext () == ValueType .ARRAY ) {
254+ if (iter .readArray ()) {
255+ throw iter .reportError ("MaybeEmptyArrayDecoder" , "this field is object. if input is array, it can only be empty" );
256+ } else {
257+ // empty array parsed as null
258+ return null ;
259+ }
260+ } else {
261+ return iter .read (iter );
262+ }
263+ }
264+ }
247265}
Original file line number Diff line number Diff line change 11package com .jsoniter ;
22
3+ import com .jsoniter .annotation .JsonProperty ;
4+ import com .jsoniter .annotation .JsoniterAnnotationSupport ;
35import com .jsoniter .any .Any ;
6+ import com .jsoniter .spi .Decoder ;
47import com .jsoniter .spi .EmptyExtension ;
58import com .jsoniter .spi .JsoniterSpi ;
69import junit .framework .TestCase ;
@@ -163,4 +166,15 @@ public void test_enum() throws IOException {
163166 obj = JsonIterator .deserialize ("{\" field1\" :\" WOW\" }" , TestObject5 .class );
164167 assertEquals (TestObject5 .MyEnum .WOW , obj .field1 );
165168 }
169+
170+ public static class TestObject6 {
171+ @ JsonProperty (decoder = Decoder .MaybeEmptyArrayDecoder .class )
172+ public Map <String , Object > field1 ;
173+ }
174+
175+ public void test_maybe_empty_array_field () {
176+ JsoniterAnnotationSupport .enable ();
177+ TestObject6 obj = JsonIterator .deserialize ("{\" field1\" :[]}" , TestObject6 .class );
178+ assertNull (obj .field1 );
179+ }
166180}
You can’t perform that action at this time.
0 commit comments