@JsonSetter affects serialization too
See original GitHub issueAccording to the docs, @JsonSetter
is supposed to affect only deserialization and allow for “asymmetric naming”:
(…) this annotation was briefly deprecated for version 1.5; but has since been un-deprecated to both allow for asymmetric naming (possibly different name when reading and writing JSON) (…)
But it’s affecting serialization too. It’s behaving like @JsonProperty
.
Jackson versions used:
+- com.fasterxml.jackson.core:jackson-core:jar:2.8.3:compile
+- com.fasterxml.jackson.core:jackson-databind:jar:2.8.3:compile
+- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.3:compile
\- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.8.3:compile
+- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.8.3:compile
\- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.8.3:compile
Unit test:
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.databind.ObjectMapper;
class A {
private Integer id;
public A() {
}
public A(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
@JsonSetter("number")
public void setId(Integer id) {
this.id = id;
}
/*
* Workaround:
*
* Change the name of the method above (setId) to something else and the bug goes away.
*
*/
}
public class JsonSetterTest {
public <T> String toJSON(T to)
{
final ObjectMapper mapper = new ObjectMapper();
try
{
return mapper.writeValueAsString(to);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
@Test
public void toJSON() {
A a = new A(1);
String json = toJSON(a);
System.out.println(json);
assertTrue(json.contains("id"));
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How does jackson @JsonGetter and @JsonSetter work
Documentation may be wrong; @JsonSetter does not only affect deserialization. While it can indeed be used for asymmetric naming (similar to @ ...
Read more >Jackson Annotation Examples - Baeldung
When we serialize an instance of this entity, we get all the key-values in the Map as standard, plain properties:
Read more >Jackson Annotations - Jenkov.com
The Jackson annotation @JsonValue tells Jackson that Jackson should not attempt to serialize the object itself, but rather call a method on the ......
Read more >JSON Tutorial – Jackson Annotations – Part 2
In this post we will continue to discuss how to Jackson Annotations are used and the affects they have on the serialization and ......
Read more >All You Need To Know About JSON Parsing With Jackson
Basic JSON Serialization and Deserialization with Jackson ... We can choose to parse the JSON to a Java Map , which is very...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t understand… Why would
@JsonSetter
have anything to do with serialization? And why would its behavior change depending on the name of the method it is attached to?@tfga SInce you feel the need to ask such questions, you really, should to read how system works. Serialization and deserialization are closely tied, and naming is something that concerns both. And existing name of method is of crucial importance: this is the implicit name used to connect things. Without such linkage (between
setX()
andgetX()
, or plainT name()
/name(T value)
) more annotations would be used for renaming. While this is possible theoretically this is approach Jackson has taken. You are free to disagree with that approach, but you do not get define it.It is true that there is redundancy currently in that
@JsonSetter
is not strictly necessary (any more, at least), but there is no meaningful way it could or should only affect deserialization, given that property discovery, including naming, is a shared concern between read and write sides.And at this point this is as much time as I will spend on explaining the system.
As per my earlier note, yes, result is as expected. Implicit
id
property has been renamed asnumber
, due to annotation. The way to prevent that is to use another annotation. Use case here is a minority use case compared to the case where renaming is expected to affect all accessors.