Skip to content

Commit b5795de

Browse files
committed
fix #104, JsonWrapper argument should not be mandatory
1 parent 295265a commit b5795de

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

src/main/java/com/jsoniter/ReflectionObjectDecoder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,10 @@ private void applyWrappers(Object[] temp, Object obj) throws Exception {
402402
for (WrapperDescriptor wrapper : desc.bindingTypeWrappers) {
403403
Object[] args = new Object[wrapper.parameters.size()];
404404
for (int i = 0; i < wrapper.parameters.size(); i++) {
405-
args[i] = temp[wrapper.parameters.get(i).idx];
405+
Object arg = temp[wrapper.parameters.get(i).idx];
406+
if (arg != NOT_SET) {
407+
args[i] = arg;
408+
}
406409
}
407410
wrapper.method.invoke(obj, args);
408411
}

src/test/java/com/jsoniter/TestAnnotationJsonWrapper.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jsoniter;
22

3+
import com.jsoniter.annotation.JsonIgnore;
34
import com.jsoniter.annotation.JsonProperty;
45
import com.jsoniter.annotation.JsonWrapper;
56
import com.jsoniter.annotation.JsonWrapperType;
@@ -47,4 +48,30 @@ public void test_key_value() throws IOException {
4748
TestObject2 obj = iter.read(TestObject2.class);
4849
assertEquals(100, obj._field1);
4950
}
51+
52+
public static class AAA {
53+
@JsonProperty("name_1")
54+
public String name;
55+
56+
@JsonIgnore
57+
public String partA;
58+
@JsonIgnore
59+
public String partB;
60+
61+
@JsonWrapper
62+
public void foreignFromJson(@JsonProperty(value = "parts", from ={"p2"}, required = false) String parts) {
63+
if(parts == null){
64+
return;
65+
}
66+
String[] ps = parts.split(",");
67+
partA = ps[0];
68+
partB = ps.length > 1 ? ps[1] : null;
69+
}
70+
}
71+
72+
public void test_issue_104() {
73+
String jsonStr = "{'name':'aaa', 'name_1':'bbb'}".replace('\'', '\"');
74+
AAA aaa = JsonIterator.deserialize(jsonStr, AAA.class);
75+
assertEquals("bbb", aaa.name);
76+
}
5077
}

src/test/java/com/jsoniter/TestGenerics.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.jsoniter;
22

3-
import com.jsoniter.spi.Binding;
4-
import com.jsoniter.spi.ClassDescriptor;
5-
import com.jsoniter.spi.ClassInfo;
6-
import com.jsoniter.spi.TypeLiteral;
3+
import com.jsoniter.spi.*;
74
import junit.framework.TestCase;
85

96
import java.io.IOException;
@@ -78,12 +75,15 @@ public static class Class1<A, B> {
7875
public B[] field2;
7976
public List<B>[] field3;
8077
public List<A[]> field4;
78+
8179
public List<Map<A, List<B>>> getField6() {
8280
return null;
8381
}
82+
8483
public <T> T getField7() {
8584
return null;
8685
}
86+
8787
public void setField8(List<A> a) {
8888
}
8989
}
@@ -113,4 +113,23 @@ public void test_generic_super_class() throws IOException {
113113
assertTrue(fieldDecoderCacheKeys.get("field7").endsWith("decoder.java.lang.Object"));
114114
assertTrue(fieldDecoderCacheKeys.get("field8").endsWith("decoder.java.util.List_java.lang.String"));
115115
}
116+
117+
public static class NetRes<T> {
118+
public int code;
119+
public String desc;
120+
public T results;
121+
}
122+
123+
public static class User {
124+
public String name;
125+
public int age;
126+
}
127+
128+
public void test_issue_103() {
129+
String json = "{'code':1, 'desc':'OK', 'results':{'name':'aaa', 'age':18}}".replace('\'', '\"');
130+
NetRes res = JsonIterator.deserialize(json, new TypeLiteral<NetRes<User>>() {
131+
});
132+
assertEquals(User.class, res.results.getClass());
133+
}
134+
116135
}

0 commit comments

Comments
 (0)