Unexpected behavior of FasterXML Jackson

Today I discovered another *) unexpected behavior of FasterXML Jackson in Java.

I was wondering about why a Boolean field provided as part of a JSON serialization of a class was always null, no matter if it was true or false in the JSON.

{ "xyz": true}
{ "xyz": false}

but xyz remained to be null. The class was like

class Abc {
    Boolean xyz;    Boolean xyz() { return xyz; }
}

Finally I found out that the method which returns the field xyz must named getXyz() not otherwise! The related setter must be named setXyz(Boolean b)!

Or you could annotate the field:

class Abc {
    @JsonProperty
    Boolean xyz;

This Q&A on Stackoverflow tells how to name accessor methods right  – thanks to Sotirios Delimanolis.

*) The other unexpected behavior I stumble upon was that it did not only write all fields of the class to JSON – but called also all getter methods and put added their results also to the JSON making a mess of it.

For my understanding of serialization such a method should read the field names of the class and write them to JSON followed by the value of the field in the current object. It should NOT deal with any methods!

Veröffentlicht in Java, json, serialization. Kommentare deaktiviert für Unexpected behavior of FasterXML Jackson
%d Bloggern gefällt das: