Skip to content

Commit a55ed53

Browse files
committed
fix #152 json wrapper should not be recognized as json setter
1 parent 72f70af commit a55ed53

File tree

5 files changed

+63
-15
lines changed

5 files changed

+63
-15
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ private Object decode_(JsonIterator iter) throws Exception {
247247
for (Binding setter : desc.setters) {
248248
Object val = temp[setter.idx];
249249
if (val != NOT_SET) {
250+
System.out.println(setter.method);
251+
System.out.println(val.getClass());
250252
setter.method.invoke(obj, val);
251253
}
252254
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ private void detectWrappers(ClassDescriptor desc, List<Method> allMethods) {
277277
}
278278
Annotation[][] annotations = method.getParameterAnnotations();
279279
String[] paramNames = getParamNames(method, annotations.length);
280+
Iterator<Binding> iter = desc.setters.iterator();
281+
while(iter.hasNext()) {
282+
if (method.equals(iter.next().method)) {
283+
iter.remove();
284+
}
285+
}
280286
if (JsonWrapperType.BINDING.equals(jsonWrapper.value())) {
281287
WrapperDescriptor wrapper = new WrapperDescriptor();
282288
wrapper.method = method;

src/test/java/com/jsoniter/TestAnnotation.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,6 @@ public class TestAnnotation extends TestCase {
1414
// JsonIterator.setMode(DecodingMode.REFLECTION_MODE);
1515
}
1616

17-
public static class TestObject2 {
18-
private int field1;
19-
20-
@JsonCreator
21-
public TestObject2(@JsonProperty("field1") int field1) {
22-
this.field1 = field1;
23-
}
24-
}
25-
26-
public void test_ctor() throws IOException {
27-
JsonIterator iter = JsonIterator.parse("{'field1': 100}".replace('\'', '"'));
28-
TestObject2 obj = iter.read(TestObject2.class);
29-
assertEquals(100, obj.field1);
30-
}
31-
3217
public static class TestObject4 {
3318

3419
private int field1;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.jsoniter;
2+
3+
import com.jsoniter.annotation.JsonCreator;
4+
import com.jsoniter.annotation.JsonIgnore;
5+
import com.jsoniter.annotation.JsonProperty;
6+
import com.jsoniter.annotation.JsonWrapper;
7+
import com.jsoniter.any.Any;
8+
import junit.framework.TestCase;
9+
10+
import java.io.IOException;
11+
import java.util.Properties;
12+
13+
public class TestAnnotationJsonCreator extends TestCase {
14+
15+
16+
public static class TestObject2 {
17+
private int field1;
18+
19+
@JsonCreator
20+
public TestObject2(@JsonProperty("field1") int field1) {
21+
this.field1 = field1;
22+
}
23+
}
24+
25+
public void test_ctor() throws IOException {
26+
JsonIterator iter = JsonIterator.parse("{'field1': 100}".replace('\'', '"'));
27+
TestObject2 obj = iter.read(TestObject2.class);
28+
assertEquals(100, obj.field1);
29+
}
30+
31+
public static class TestObject {
32+
33+
@JsonIgnore
34+
private final String id;
35+
@JsonIgnore
36+
private final Properties properties;
37+
38+
@JsonCreator
39+
public TestObject(@JsonProperty("name") final String name) {
40+
this.id = name;
41+
properties = new Properties();
42+
}
43+
44+
@JsonWrapper
45+
public void setProperties(@JsonProperty("props") final Any props) {
46+
// Set props
47+
}
48+
}
49+
50+
public void test_ctor_and_setter_binding() throws IOException {
51+
JsonIterator iter = JsonIterator.parse("{\"name\": \"test\", \"props\": {\"val\": \"42\"}}");
52+
iter.read(TestObject.class);
53+
}
54+
}

src/test/java/com/jsoniter/suite/AllTestCases.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
TestAnnotationJsonWrapper.class,
2323
TestAnnotationJsonUnwrapper.class,
2424
TestAnnotation.class,
25+
TestAnnotationJsonCreator.class,
2526
com.jsoniter.output.TestGenerics.class,
2627
TestCustomizeType.class, TestDemo.class,
2728
TestExisting.class, TestGenerics.class, TestGenerics.class, TestIO.class,

0 commit comments

Comments
 (0)