Odd Boolean serialization when is-get and get method are both present
See original GitHub issueI have noticed that when trying to serialize a Boolean field, when both the is method and get method are present, Jackson will use the get method for serialization. Let’s take this example, and imagine to have an endpoint that returns the Person object by name:
public class Person {
private String name; // Tatu
private Boolean employed; // null
public String getName() {
return name;
}
// Jackson will use this method for serialization over isEmployed. The person endpoint for Tatu
// will return
//
// {
// "name": "Tatu",
// "employed": null
// }
public Boolean getEmployed() {
return employed;
}
// Jackson will use this method for serialization if getEmployed is commented and will return
//
// {
// "name": "Tatu",
// "employed": false
// }
public boolean isEmployed() {
return Boolean.TRUE.equals(employed);
}
}
When Jackson try to serialize the Person object it will use getEmployed()
method to serialize the employed
field. Of course if the getEmployed()
method is removed, it will use the isEmployed()
. I would be nice to have at least this behaviour documented.
I have a demo project, a trivial application using spring boot starter to simulate this behaviour at this link.
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Jackson serialization outputting two json rows for one java ...
It makes sense to me that it should match the Java variable name only, but the contract sadly has both versions, and I...
Read more >Serialize and Deserialize Booleans as Integers With Jackson
In this article, we'll explain how to serialize Boolean values as Integers — plus, numeric strings — and vice versa in Jackson.
Read more >Web Services Description Language (WSDL) Version 2.0 Part 2
They provide visual help for the XML serialization. ... has the value http://www.w3.org/2003/05/soap/mep/soap-response/ then the HTTP method used is GET .
Read more >INTRODUCTION TO JAVA PROGRAMMING
Data and methods operating on the data are wrapped in a single object. Inheritance: Inheritance facilitates the reusability of existing code by building...
Read more >Types to the Rescue: Verification of REST APIs Consumer Code
methods are get, post, put and delete);. 3. u, an URI template that, according to the pre-condition, expands to a valid API endpoint;....
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
@SimoneGiusso right. It was good to verify this, given that I had forgotten what I had earlier implemented 😃
Ok, looked into this only briefly and realized that the precedence is defined as per a comment in
POJOPRopertyBuilder
methodgetGetter()
when selecting amongst possibly multiple candidates:There is also a reference to issue #238 which defined the expected behavior (but one that I had already forgotten 😄 ). One problem with the implementation, however, is that it seems to hard code “get” and “is” prefixes even tho other parts of code allow custom prefixes.
But it looks like “regular” getters do have precedence over “is-getters” at this point (both of which have precedence over other prefixes that customized handlers might provide) and this is to be considered the expected behavior.