Is it possible to use @JsonIdentityInfo for serialization and ValueInstantiator for deserialization?

997 views
Skip to first unread message

Steve Bread

unread,
Aug 5, 2014, 7:33:59 PM8/5/14
to jackso...@googlegroups.com
Given an Employee and Manager class, I want to serialize Employee as
{
  "manager": <primary key>
}
and be able to set the Manager during deserialization by reading the Manager by its primary key.

I tried the following class definition:

class Employee {
    public Manager manager;
 
    @JsonProperty
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "primaryKey")
    @JsonIdentityReference(alwaysAsId = true)
    public Manager getManager() {
        return manager;
    }

    public void setManager(Manager manager) {
        this.manager = manager;
    }
}

class Manager {
    public Long getPrimaryKey() { ... }
    // No setPrimaryKey()
}

public class ManagerInstantiator extends StdValueInstantiator {
    @Override
    public boolean canCreateFromInt() {
        return true;
    }
    @Override
    public Manager createFromLong(DeserializationContext ctxt, long id) throws IOException, JsonProcessingException {
        return managerService.readManager(id); // managerService initialization not shown
    }
}

Employee serializes correctly but fails during deserialization because the presence of @JsonIdentityInfo looks for a setPrimaryKey method on Manager. I'd like to ignore @JsonIdentityInfo and use the value instantiator instead. If I remove @JsonIdentityInfo then the deserialization works but the serialization is no longer limited to the id. I think one option could be to replace @JsonIdentityInfo with a custom serializer but I wanted to check if there was a way to make it work using @JsonIdentityInfo.

Thanks,
Steve

Tatu Saloranta

unread,
Aug 7, 2014, 3:00:29 PM8/7/14
to jackso...@googlegroups.com
If I understand the question correctly, the answer is no: when using @JsonIdentityInfo, logic for finding instance is divided into two parts; object id is used for invoking object id reader (which then consults mapping of already seen ids to instances; or, if not seen, notes down missing instance for later resolution), or, for full objects (not ids), regular deserialization. In former case, no value deserializer is invoked.

It should be possible (if not convenient) to change the way object id reader works, but I have not actually written custom instances myself.
It may be that what is needed is an annotation to allow easier installation of custom handlers for deserialization: for serialization it is possible to define ObjectIdGenerator, but there is no good counterpart for deserialization.

I think Pascal might have other better suggestions, since he has worked extensively on Object Id handling, including writing implementation for out-of-order (forward-ref) resolution introduced in 2.4.

-+ Tatu +-



--
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 post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages