Skip to content

Commit 732312f

Browse files
committed
#97 demonstrate JsonProperty when both field and setter
1 parent ce6f45d commit 732312f

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/test/java/com/jsoniter/TestAnnotationJsonProperty.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.jsoniter.annotation.JsonMissingProperties;
44
import com.jsoniter.annotation.JsonProperty;
55
import com.jsoniter.fuzzy.StringIntDecoder;
6+
import com.jsoniter.output.JsonStream;
67
import junit.framework.TestCase;
78

89
import java.io.IOException;
@@ -121,4 +122,23 @@ public void test_required() throws IOException {
121122
TestObject8 entity = JsonIterator.deserialize(test, TestObject8.class);
122123
assertEquals(200, entity.code2);
123124
}
125+
126+
public static class TestObject9 {
127+
private String field1 = "hello";
128+
129+
public String getField1() {
130+
return field1;
131+
}
132+
133+
@JsonProperty("field-1")
134+
public void setField1(String field1) {
135+
this.field1 = field1;
136+
}
137+
}
138+
139+
public void test_getter_and_setter() throws IOException {
140+
String test ="{\"field-1\":\"hi\"}";
141+
TestObject9 entity = JsonIterator.deserialize(test, TestObject9.class);
142+
assertEquals("hi", entity.getField1());
143+
}
124144
}

src/test/java/com/jsoniter/output/TestAnnotationJsonProperty.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void test_encoder() throws IOException {
4040
public static class TestObject3 {
4141
public String field1 = "hello";
4242

43-
@JsonProperty(to = {"field-1"})
43+
@JsonProperty("field-1")
4444
public String getField1() {
4545
return field1;
4646
}
@@ -50,4 +50,22 @@ public void test_getter() throws IOException {
5050
String output = JsonStream.serialize(new TestObject3());
5151
assertEquals("{\"field-1\":\"hello\"}", output);
5252
}
53+
54+
public static class TestObject4 {
55+
private String field1 = "hello";
56+
57+
@JsonProperty("field-1")
58+
public String getField1() {
59+
return field1;
60+
}
61+
62+
public void setField1(String field1) {
63+
this.field1 = field1;
64+
}
65+
}
66+
67+
public void test_getter_and_setter() throws IOException {
68+
String output = JsonStream.serialize(new TestObject4());
69+
assertEquals("{\"field-1\":\"hello\"}", output);
70+
}
5371
}

0 commit comments

Comments
 (0)