Class JacksonUniqueIdGenerator

  • All Implemented Interfaces:
    Serializable

    public final class JacksonUniqueIdGenerator
    extends com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
    Exists so that complex data models (such as TSP chaining) can be serialized/deserialized. These complexities include:
    • Serializing maps where keys are themselves serialized objects that need to be referenced later.
    • Serializing polymorphic types.
    • Serializing self-referential and/or recursive types.
    Jackson can easily handle any of these problems individually, but struggles when they are all combined.

    This class and other classes in this package aim to solve those issues by introducing a new ID field on all serialized objects, typically called "@id". This field is used exclusively for referencing objects in the serialized JSON, it never enters the Java data model. Therefore it is not related to AbstractPersistable.getId(), which is the actual object ID used in the Java examples. See Vehicle Routing example to learn how to use this pattern.

    For use cases without these advanced needs, the less complex way of using JsonIdentityInfo with ObjectIdGenerators.PropertyGenerator is preferred. See Cloud Balancing example to learn how to use this pattern.

    The implementation is similar in principle to ObjectIdGenerators.UUIDGenerator, but without the long and undescriptive UUIDs. Works only for children of AbstractPersistable. No two such classes must have the same Class.getSimpleName().

    See Also:
    KeySerializer, AbstractKeyDeserializer, AbstractJsonSolutionFileIO, Serialized Form
    • Constructor Detail

      • JacksonUniqueIdGenerator

        public JacksonUniqueIdGenerator()
    • Method Detail

      • getScope

        public Class<?> getScope()
        Specified by:
        getScope in class com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
      • canUseFor

        public boolean canUseFor​(com.fasterxml.jackson.annotation.ObjectIdGenerator<?> gen)
        Specified by:
        canUseFor in class com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
      • forScope

        public com.fasterxml.jackson.annotation.ObjectIdGenerator<String> forScope​(Class<?> scope)
        Specified by:
        forScope in class com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
      • newForSerialization

        public com.fasterxml.jackson.annotation.ObjectIdGenerator<String> newForSerialization​(Object context)
        Specified by:
        newForSerialization in class com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
      • key

        public com.fasterxml.jackson.annotation.ObjectIdGenerator.IdKey key​(Object key)
        Specified by:
        key in class com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
      • generateId

        public String generateId​(Object forPojo)
        Specified by:
        generateId in class com.fasterxml.jackson.annotation.ObjectIdGenerator<String>