Skip to content

Commit b5be533

Browse files
committed
fix #107 annotation should be marked on getter/setter if present
1 parent 0061987 commit b5be533

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Diff for: src/main/java/com/jsoniter/spi/Config.java

+13
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,10 @@ private void detectCtor(ClassDescriptor desc) {
377377
private void updateBindings(ClassDescriptor desc) {
378378
boolean globalOmitDefault = JsoniterSpi.getCurrentConfig().omitDefaultValue();
379379
for (Binding binding : desc.allBindings()) {
380+
boolean annotated = false;
380381
JsonIgnore jsonIgnore = getJsonIgnore(binding.annotations);
381382
if (jsonIgnore != null) {
383+
annotated = true;
382384
if (jsonIgnore.ignoreDecoding()) {
383385
binding.fromNames = new String[0];
384386
}
@@ -389,6 +391,7 @@ private void updateBindings(ClassDescriptor desc) {
389391
// map JsonUnwrapper is not getter
390392
JsonUnwrapper jsonUnwrapper = getJsonUnwrapper(binding.annotations);
391393
if (jsonUnwrapper != null) {
394+
annotated = true;
392395
binding.fromNames = new String[0];
393396
binding.toNames = new String[0];
394397
}
@@ -397,20 +400,30 @@ private void updateBindings(ClassDescriptor desc) {
397400
}
398401
JsonProperty jsonProperty = getJsonProperty(binding.annotations);
399402
if (jsonProperty != null) {
403+
annotated = true;
400404
updateBindingWithJsonProperty(binding, jsonProperty);
401405
}
402406
if (getAnnotation(binding.annotations, JsonMissingProperties.class) != null) {
407+
annotated = true;
403408
// this binding will not bind from json
404409
// instead it will be set by jsoniter with missing property names
405410
binding.fromNames = new String[0];
406411
desc.onMissingProperties = binding;
407412
}
408413
if (getAnnotation(binding.annotations, JsonExtraProperties.class) != null) {
414+
annotated = true;
409415
// this binding will not bind from json
410416
// instead it will be set by jsoniter with extra properties
411417
binding.fromNames = new String[0];
412418
desc.onExtraProperties = binding;
413419
}
420+
if (annotated && binding.field != null) {
421+
for (Binding setter : desc.setters) {
422+
if (binding.name.equals(setter.name)) {
423+
throw new JsonException("annotation should be marked on getter/setter for field: " + binding.name);
424+
}
425+
}
426+
}
414427
}
415428
}
416429

Diff for: src/test/java/com/jsoniter/TestAnnotationJsonProperty.java

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.jsoniter.annotation.JsonProperty;
66
import com.jsoniter.fuzzy.StringIntDecoder;
77
import com.jsoniter.output.JsonStream;
8+
import com.jsoniter.spi.JsonException;
89
import junit.framework.TestCase;
910

1011
import java.io.IOException;
@@ -158,4 +159,26 @@ public void test_creator_with_json_property() {
158159
assertEquals(100, obj.field);
159160
assertEquals("{\"field\":100}", JsonStream.serialize(obj));
160161
}
162+
163+
public static class TestObject11 {
164+
@JsonProperty("hello")
165+
public int field;
166+
167+
public int getField() {
168+
return field;
169+
}
170+
171+
public void setField(int field) {
172+
this.field = field;
173+
}
174+
}
175+
176+
public void test_should_throw_exception_when_json_property_on_field_when_getter_and_setter_present() {
177+
String input = "{\"hello\":100}";
178+
try {
179+
JsonIterator.deserialize(input, TestObject11.class);
180+
fail();
181+
} catch (JsonException e) {
182+
}
183+
}
161184
}

0 commit comments

Comments
 (0)