About @JsonProperty, @JsonGetter and @JsonSetter

5,184 views
Skip to first unread message

Ewout Van Gossum

unread,
Oct 14, 2016, 11:36:10 AM10/14/16
to jackson-user
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 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 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.


Tatu Saloranta

unread,
Oct 14, 2016, 11:45:17 AM10/14/16
to jackso...@googlegroups.com
You can think of `@JsonGetter` and `@JsonSetter` mostly as old aliases for `@JsonProperty`. So you shouldn't have to them for anything.

And yes, annotated getter/field for serialization (to access property value), setter/constructor-parameter/field for deserialization (to assign).

Beyond this, there is also inference based from related (by name) accessors, to avoid having to use annotation on both getter and setter. This can and does bring in non-annotated accessors, unless further overridden by @JsonIgnore (or `mode` of @JsonProperty set to READ_ONLY or WRITE_ONLY).

I can't say much more about your example since you left out the class definition which would be necessary to know exact details.

-+ Tatu +-



--
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+unsubscribe@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages