Skip to content

Commit 217c073

Browse files
committed
#110 if @JsonProperty is marked on field, ignore getter/setter
1 parent 8a4d849 commit 217c073

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ private static void decodingDeduplicate(ClassDescriptor desc) {
128128
ArrayList<Binding> iteratingSetters = new ArrayList<Binding>(desc.setters);
129129
Collections.reverse(iteratingSetters);
130130
for (Binding setter : iteratingSetters) {
131+
if (setter.fromNames.length == 0) {
132+
continue;
133+
}
131134
Binding existing = byFieldName.get(setter.name);
132135
if (existing != null) {
133136
existing.fromNames = new String[0];
@@ -168,6 +171,9 @@ private static void encodingDeduplicate(ClassDescriptor desc) {
168171
byFieldName.put(field.name, field);
169172
}
170173
for (Binding getter : new ArrayList<Binding>(desc.getters)) {
174+
if (getter.toNames.length == 0) {
175+
continue;
176+
}
171177
Binding existing = byFieldName.get(getter.name);
172178
if (existing != null) {
173179
existing.toNames = new String[0];

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,15 @@ private void updateBindings(ClassDescriptor desc) {
420420
if (annotated && binding.field != null) {
421421
if (desc.setters != null) {
422422
for (Binding setter : desc.setters) {
423-
if (binding.name.equals(setter.name)) {
424-
throw new JsonException("annotation should be marked on getter/setter for field: " + binding.name);
423+
if (binding.field.getName().equals(setter.name)) {
424+
setter.fromNames = new String[0];
425+
setter.toNames = new String[0];
426+
}
427+
}
428+
for (Binding getter : desc.getters) {
429+
if (binding.field.getName().equals(getter.name)) {
430+
getter.fromNames = new String[0];
431+
getter.toNames = new String[0];
425432
}
426433
}
427434
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,10 @@ public void setField(int field) {
178178
}
179179
}
180180

181-
public void test_should_throw_exception_when_json_property_on_field_when_getter_and_setter_present() {
181+
public void test_field_and_getter_setter() {
182182
String input = "{\"hello\":100}";
183-
try {
184-
JsonIterator.deserialize(input, TestObject11.class);
185-
fail();
186-
} catch (JsonException e) {
187-
}
183+
TestObject11 obj = JsonIterator.deserialize(input, TestObject11.class);
184+
assertEquals(100, obj.field);
188185
}
186+
189187
}

0 commit comments

Comments
 (0)