Skip to content

Commit 6e74352

Browse files
committed
fix #132 MaybeEmptyArrayDecoder need to decode the value according to field type
1 parent ea9db31 commit 6e74352

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

src/main/java/com/jsoniter/fuzzy/MaybeEmptyArrayDecoder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
import com.jsoniter.JsonIterator;
44
import com.jsoniter.ValueType;
5+
import com.jsoniter.spi.Binding;
56
import com.jsoniter.spi.Decoder;
67

78
import java.io.IOException;
89

910
public class MaybeEmptyArrayDecoder implements Decoder {
1011

12+
private Binding binding;
13+
14+
public MaybeEmptyArrayDecoder(Binding binding) {
15+
this.binding = binding;
16+
}
17+
1118
@Override
1219
public Object decode(JsonIterator iter) throws IOException {
1320
if (iter.whatIsNext() == ValueType.ARRAY) {
@@ -18,7 +25,7 @@ public Object decode(JsonIterator iter) throws IOException {
1825
return null;
1926
}
2027
} else {
21-
return iter.read(iter);
28+
return iter.read(binding.valueTypeLiteral);
2229
}
2330
}
2431
}

src/main/java/com/jsoniter/spi/Config.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,18 +458,30 @@ private void updateBindingWithJsonProperty(Binding binding, JsonProperty jsonPro
458458
if (jsonProperty.to().length > 0) {
459459
binding.toNames = jsonProperty.to();
460460
}
461-
if (jsonProperty.decoder() != Decoder.class) {
461+
Class decoderClass = jsonProperty.decoder();
462+
if (decoderClass != Decoder.class) {
462463
try {
463-
binding.decoder = jsonProperty.decoder().newInstance();
464+
try {
465+
Constructor decoderCtor = decoderClass.getConstructor(Binding.class);
466+
binding.decoder = (Decoder) decoderCtor.newInstance(binding);
467+
} catch (NoSuchMethodException e) {
468+
binding.decoder = (Decoder) decoderClass.newInstance();
469+
}
464470
} catch (RuntimeException e) {
465471
throw e;
466472
} catch (Exception e) {
467473
throw new JsonException(e);
468474
}
469475
}
470-
if (jsonProperty.encoder() != Encoder.class) {
476+
Class encoderClass = jsonProperty.encoder();
477+
if (encoderClass != Encoder.class) {
471478
try {
472-
binding.encoder = jsonProperty.encoder().newInstance();
479+
try {
480+
Constructor encoderCtor = encoderClass.getConstructor(Binding.class);
481+
binding.encoder = (Encoder) encoderCtor.newInstance(binding);
482+
} catch (NoSuchMethodException e) {
483+
binding.encoder = (Encoder) encoderClass.newInstance();
484+
}
473485
} catch (JsonException e) {
474486
throw e;
475487
} catch (Exception e) {

src/test/java/com/jsoniter/TestObject.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,20 @@ public void test_enum() throws IOException {
184184
assertEquals(TestObject5.MyEnum.WOW, obj.field1);
185185
}
186186

187+
public static class TestObject6_field1 {
188+
public int a;
189+
}
190+
187191
public static class TestObject6 {
188192
@JsonProperty(decoder = MaybeEmptyArrayDecoder.class)
189-
public Map<String, Object> field1;
193+
public TestObject6_field1 field1;
190194
}
191195

192196
public void test_maybe_empty_array_field() {
193197
TestObject6 obj = JsonIterator.deserialize("{\"field1\":[]}", TestObject6.class);
194198
assertNull(obj.field1);
199+
obj = JsonIterator.deserialize("{\"field1\":{\"a\":1}}", TestObject6.class);
200+
assertEquals(1, obj.field1.a);
195201
}
196202

197203
public void test_iterator() {

0 commit comments

Comments
 (0)