Hello,

I've got a question about the way Jackson works. When you have a class 
annotated with the following JsonAutoDetect:

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


And that only has @JsonProperty on the getters, what is supposed to be the 
expected serialization/deserialization behaviour? What about:

   - using only @JsonGetter (on getters) 
   - uysing only @JsonSetter (on setters)
   
>From testing, it appears that when you use @JsonProperty, a class will get 
serialized and deserialized by the marked getters:

This code:

ObjectMapper objectMapper = new ObjectMapper();
System.out.println("Json Serialize");
String string = objectMapper.writeValueAsString(new MyModelClass2(
        1L, "Foo", "Bar", new Date(), "Baz", new Date(), "Bal", "Bo", "Bo"
));
System.out.println(string);
System.out.println("Json Deserialize");
System.out.println(objectMapper.readValue(string, MyModelClass2.class));


Has the following output:

Json Serialize
{"createdDate":1476429800628,"someNumber":1,"someCondition":"Bo","someName":"Foo"}
Json Deserialize
MyModelClass2{createdBy='null', createdDate=Fri Oct 14 09:23:20 CEST 2016, 
someNumber=1, updatedBy='null', updatedDate=null, someCondition='Bo', 
firstName='null', someName='Foo'}


Which clearly shows that only the marked @JsonProperty properties are used 
for serialization/deserialization. It provides the same output when using 
@JsonGetter instead of @JsonProperty. This seems conflicting with the 
sentences in the javadoc:


   - @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.
   
   - 
   
   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.
   
What am I missing here? From the commentary my expectations were that 
annotated getters would take care of serialization only, and annotated 
setters would take care of deserialization, but this is not the case?

Any help with this is appreciated.

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