Hi,

How is @JsonProperty supposed to work? Given the following documentation:


   - @JsonProperty: Marker annotation that can be used to define a 
   non-static method as a "setter" or "getter" for a logical property 
   (depending on its signature), or non-static object field to be used 
   (serialized, deserialized) as a logical property.
   
   - @JsonGetter: Marker annotation that can be used to define a 
   non-static, no-argument value-returning (non-void) method to be used as a 
   "getter" for a logical property, as an alternative to recommended 
   JsonProperty 
   
<http://fasterxml.github.io/jackson-core/javadoc/1.9/org/codehaus/jackson/annotate/JsonProperty.html>
 annotation 
   (which was introduced in version 1.1).
   
   Getter means that when serializing Object instance of class that has 
   this method (possibly inherited from a super class), a call is made through 
   the method, and return value will be serialized as value of the property.
   
   - 
   
   @JsonSetter: Marker annotation that can be used to define a non-static, 
   single-argument method to be used as a "setter" for a logical property as 
   an alternative to recommended JsonProperty 
   
<http://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonProperty.html>
 annotation 
   (which was introduced in version 1.1).
   
   Setter means that when a property with matching name is encountered in 
   JSON content, this method will be used to set value of the property.
   
I would expect that annotated getters take care of serialization, and 
setters of deserialization. But when using a class annotated with:

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE,
        getterVisibility = JsonAutoDetect.Visibility.NONE,
        setterVisibility = JsonAutoDetect.Visibility.NONE,
        isGetterVisibility = JsonAutoDetect.Visibility.NONE)


*The following test:*

ObjectMapper objectMapper = new ObjectMapper();
String string = objectMapper.writeValueAsString(new MyModelClass2(
        1L, "Foo", "Bar", new Date(), "Baz", new Date(), "Bal", "Bo", "Bo"
));
System.out.println(string);
System.out.println(objectMapper.readValue("{\"createdBy\":\"Baz\",\"createdDate\":1475741455220,"
        + 
"\"someNumber\":1,\"updatedBy\":\"Bar\",\"updatedDate\":1475741455220,\"someCondition\":\"Bo\","
        + "\"firstName\":\"Bal\",\"someName\":\"Foo\"}", MyModelClass2.class));


*Has the following output:*

//Serialization
{"createdDate":1476431196458,"someNumber":1,"someCondition":"Bo","someName":"Foo"}
//Deserialization
MyModelClass2{createdBy='null', createdDate=Thu Oct 06 10:10:55 CEST 2016, 
someNumber=1, updatedBy='null', updatedDate=null, someCondition='Bo', 
firstName='null', someName='Foo'}

The class MyModelClass2 only uses @JsonProperty on the getters, but clearly 
the properties are deserialized as well. 

What am I missing here? did I misinterpret the documentation? (I'm using 
Jackson 2.8.3)

Kind Regards,

Ewout.

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to