diff --git a/src/main/java/com/jsoniter/spi/Config.java b/src/main/java/com/jsoniter/spi/Config.java index 12878d91..71586627 100644 --- a/src/main/java/com/jsoniter/spi/Config.java +++ b/src/main/java/com/jsoniter/spi/Config.java @@ -377,8 +377,10 @@ private void detectCtor(ClassDescriptor desc) { private void updateBindings(ClassDescriptor desc) { boolean globalOmitDefault = JsoniterSpi.getCurrentConfig().omitDefaultValue(); for (Binding binding : desc.allBindings()) { + boolean annotated = false; JsonIgnore jsonIgnore = getJsonIgnore(binding.annotations); if (jsonIgnore != null) { + annotated = true; if (jsonIgnore.ignoreDecoding()) { binding.fromNames = new String[0]; } @@ -389,6 +391,7 @@ private void updateBindings(ClassDescriptor desc) { // map JsonUnwrapper is not getter JsonUnwrapper jsonUnwrapper = getJsonUnwrapper(binding.annotations); if (jsonUnwrapper != null) { + annotated = true; binding.fromNames = new String[0]; binding.toNames = new String[0]; } @@ -397,20 +400,30 @@ private void updateBindings(ClassDescriptor desc) { } JsonProperty jsonProperty = getJsonProperty(binding.annotations); if (jsonProperty != null) { + annotated = true; updateBindingWithJsonProperty(binding, jsonProperty); } if (getAnnotation(binding.annotations, JsonMissingProperties.class) != null) { + annotated = true; // this binding will not bind from json // instead it will be set by jsoniter with missing property names binding.fromNames = new String[0]; desc.onMissingProperties = binding; } if (getAnnotation(binding.annotations, JsonExtraProperties.class) != null) { + annotated = true; // this binding will not bind from json // instead it will be set by jsoniter with extra properties binding.fromNames = new String[0]; desc.onExtraProperties = binding; } + if (annotated && binding.field != null) { + for (Binding setter : desc.setters) { + if (binding.name.equals(setter.name)) { + throw new JsonException("annotation should be marked on getter/setter for field: " + binding.name); + } + } + } } } diff --git a/src/test/java/com/jsoniter/TestAnnotationJsonProperty.java b/src/test/java/com/jsoniter/TestAnnotationJsonProperty.java index 68747852..aca31f7e 100644 --- a/src/test/java/com/jsoniter/TestAnnotationJsonProperty.java +++ b/src/test/java/com/jsoniter/TestAnnotationJsonProperty.java @@ -5,6 +5,7 @@ import com.jsoniter.annotation.JsonProperty; import com.jsoniter.fuzzy.StringIntDecoder; import com.jsoniter.output.JsonStream; +import com.jsoniter.spi.JsonException; import junit.framework.TestCase; import java.io.IOException; @@ -158,4 +159,26 @@ public void test_creator_with_json_property() { assertEquals(100, obj.field); assertEquals("{\"field\":100}", JsonStream.serialize(obj)); } + + public static class TestObject11 { + @JsonProperty("hello") + public int field; + + public int getField() { + return field; + } + + public void setField(int field) { + this.field = field; + } + } + + public void test_should_throw_exception_when_json_property_on_field_when_getter_and_setter_present() { + String input = "{\"hello\":100}"; + try { + JsonIterator.deserialize(input, TestObject11.class); + fail(); + } catch (JsonException e) { + } + } }