Skip to content

Commit

Permalink
fix #107 annotation should be marked on getter/setter if present
Browse files Browse the repository at this point in the history
  • Loading branch information
taowen committed Oct 12, 2017
1 parent 0061987 commit b5be533
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/jsoniter/spi/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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];
}
Expand All @@ -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);
}
}
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/jsoniter/TestAnnotationJsonProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
}
}
}

0 comments on commit b5be533

Please sign in to comment.