question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Odd Boolean serialization when is-get and get method are both present

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, May 4, 2022

@SimoneGiusso right. It was good to verify this, given that I had forgotten what I had earlier implemented 😃

1reaction
cowtowncodercommented, Apr 30, 2022

Ok, looked into this only briefly and realized that the precedence is defined as per a comment in POJOPRopertyBuilder method getGetter() when selecting amongst possibly multiple candidates:

            /* 30-May-2014, tatu: Three levels of precedence:
             * 
             * 1. Regular getters ("getX")
             * 2. Is-getters ("isX")
             * 3. Implicit, possible getters ("x")
             */

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found