Switch by to default de-serializer

52 views
Skip to first unread message

Govinda Sakhare

unread,
Jan 9, 2021, 1:09:13 PM1/9/21
to jackson-user
Hi All,

I have written a custom de-serializer to ensure backward compatibility between models and applied it on a field as below. I want the de-serializer to kick-in if the JsonNode is array otherwise I want the default deserializer to deserialize the field. I tried the below approach using the ObjectMapper object but it is not working.

class Person {

    private int id;

   @JsonDeserialize(using = CustomDeserializer.class)
   @JsonAlias("addresses")
    private AddressDetails addressDetails;
     // .Getter setters 
}

but in else part jsonNode.asText() is returning empty string, but debugger is showing child nodes. am I doing something wrong here?

@Override
public AddressDetails deserialize(...) {

    AddressDetails addressDetails = new AddressDetails();
    JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
   AddressDetails addressDetails = null;
    if(jsonNode.isArray()) {
        // construct AddressDetails object
    }
    else {
        ObjectMapper objectMapper = new ObjectMapper();
        addressDetails = objectMapper.readValue(jsonNode.asText(), AddressDetails.class);
     }
     return addressDetails;
}

Thanks
Govinda

Tatu Saloranta

unread,
Jan 9, 2021, 8:08:45 PM1/9/21
to jackson-user
On Sat, Jan 9, 2021 at 10:09 AM Govinda Sakhare
<govindas...@gmail.com> wrote:
>
> Hi All,
>
> I have written a custom de-serializer to ensure backward compatibility between models and applied it on a field as below. I want the de-serializer to kick-in if the JsonNode is array otherwise I want the default deserializer to deserialize the field. I tried the below approach using the ObjectMapper object but it is not working.
>
> class Person {
>
> private int id;
>
> @JsonDeserialize(using = CustomDeserializer.class)
> @JsonAlias("addresses")
> private AddressDetails addressDetails;
> // .Getter setters
> }
>
> but in else part jsonNode.asText() is returning empty string, but debugger is showing child nodes. am I doing something wrong here?

Yes, the problem is that "Jsonnode.asText()" will just return String
value of scalar-valued node, and that is not going to work as input
in most cases (it might sort of work for Boolean and Number values but
nothing else): for Objects and Arrays it simply returns "".
In future you may want to read Javadocs of the methods you call to see
what they are supposed to do, that helps a lot in figuring out how to
do things.

There are couple of ways to "read" from JsonNode (like it was a json
document), however, and the simplest is:

return ((ObjectMapper) parser.getCodec()).treeToValue(jsonNode,
AddressDetails.class);

(note: you usually do not want to do this

ObjectMapper objectMapper = new ObjectMapper();

as constructing a new mapper is very expensive and does not have
configuration settings).

Hope this helps,

-+ Tatu +-

Govinda Sakhare

unread,
Jan 10, 2021, 1:15:44 AM1/10/21
to jackso...@googlegroups.com
Thanks for the Inputs mate.

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAL4a10jsYtehD97n%3DfsL1Xv4yoGH9DKDaJBGF_Yr%3DmsxCZXC5A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages