Search in sources :

Example 1 with ObjectIdGenerator

use of com.fasterxml.jackson.annotation.ObjectIdGenerator in project jackson-databind by FasterXML.

the class DefaultDeserializationContext method findObjectId.

/*
    /**********************************************************
    /* Abstract methods impls, Object Id
    /**********************************************************
     */
@Override
public ReadableObjectId findObjectId(Object id, ObjectIdGenerator<?> gen, ObjectIdResolver resolverType) {
    /* 02-Apr-2015, tatu: As per [databind#742] should allow 'null', similar to how
         *   missing id already works.
         */
    if (id == null) {
        return null;
    }
    final ObjectIdGenerator.IdKey key = gen.key(id);
    if (_objectIds == null) {
        _objectIds = new LinkedHashMap<ObjectIdGenerator.IdKey, ReadableObjectId>();
    } else {
        ReadableObjectId entry = _objectIds.get(key);
        if (entry != null) {
            return entry;
        }
    }
    // Not seen yet, must create entry and configure resolver.
    ObjectIdResolver resolver = null;
    if (_objectIdResolvers == null) {
        _objectIdResolvers = new ArrayList<ObjectIdResolver>(8);
    } else {
        for (ObjectIdResolver res : _objectIdResolvers) {
            if (res.canUseFor(resolverType)) {
                resolver = res;
                break;
            }
        }
    }
    if (resolver == null) {
        resolver = resolverType.newForDeserialization(this);
        _objectIdResolvers.add(resolver);
    }
    ReadableObjectId entry = createReadableObjectId(key);
    entry.setResolver(resolver);
    _objectIds.put(key, entry);
    return entry;
}
Also used : IdKey(com.fasterxml.jackson.annotation.ObjectIdGenerator.IdKey) ObjectIdGenerator(com.fasterxml.jackson.annotation.ObjectIdGenerator) ReadableObjectId(com.fasterxml.jackson.databind.deser.impl.ReadableObjectId) IdKey(com.fasterxml.jackson.annotation.ObjectIdGenerator.IdKey) ObjectIdResolver(com.fasterxml.jackson.annotation.ObjectIdResolver)

Example 2 with ObjectIdGenerator

use of com.fasterxml.jackson.annotation.ObjectIdGenerator in project jackson-databind by FasterXML.

the class BeanSerializerFactory method constructObjectIdHandler.

protected ObjectIdWriter constructObjectIdHandler(SerializerProvider prov, BeanDescription beanDesc, List<BeanPropertyWriter> props) throws JsonMappingException {
    ObjectIdInfo objectIdInfo = beanDesc.getObjectIdInfo();
    if (objectIdInfo == null) {
        return null;
    }
    ObjectIdGenerator<?> gen;
    Class<?> implClass = objectIdInfo.getGeneratorType();
    // Just one special case: Property-based generator is trickier
    if (implClass == ObjectIdGenerators.PropertyGenerator.class) {
        // most special one, needs extra work
        String propName = objectIdInfo.getPropertyName().getSimpleName();
        BeanPropertyWriter idProp = null;
        for (int i = 0, len = props.size(); ; ++i) {
            if (i == len) {
                throw new IllegalArgumentException("Invalid Object Id definition for " + beanDesc.getBeanClass().getName() + ": can not find property with name '" + propName + "'");
            }
            BeanPropertyWriter prop = props.get(i);
            if (propName.equals(prop.getName())) {
                idProp = prop;
                /* Let's force it to be the first property to output
                     * (although it may still get rearranged etc)
                     */
                if (i > 0) {
                    props.remove(i);
                    props.add(0, idProp);
                }
                break;
            }
        }
        JavaType idType = idProp.getType();
        gen = new PropertyBasedObjectIdGenerator(objectIdInfo, idProp);
        // one more thing: must ensure that ObjectIdWriter does not actually write the value:
        return ObjectIdWriter.construct(idType, (PropertyName) null, gen, objectIdInfo.getAlwaysAsId());
    }
    // other types are simpler
    JavaType type = prov.constructType(implClass);
    // Could require type to be passed explicitly, but we should be able to find it too:
    JavaType idType = prov.getTypeFactory().findTypeParameters(type, ObjectIdGenerator.class)[0];
    gen = prov.objectIdGeneratorInstance(beanDesc.getClassInfo(), objectIdInfo);
    return ObjectIdWriter.construct(idType, objectIdInfo.getPropertyName(), gen, objectIdInfo.getAlwaysAsId());
}
Also used : PropertyBasedObjectIdGenerator(com.fasterxml.jackson.databind.ser.impl.PropertyBasedObjectIdGenerator) ObjectIdGenerator(com.fasterxml.jackson.annotation.ObjectIdGenerator) PropertyBasedObjectIdGenerator(com.fasterxml.jackson.databind.ser.impl.PropertyBasedObjectIdGenerator) ObjectIdGenerators(com.fasterxml.jackson.annotation.ObjectIdGenerators) FilteredBeanPropertyWriter(com.fasterxml.jackson.databind.ser.impl.FilteredBeanPropertyWriter)

Example 3 with ObjectIdGenerator

use of com.fasterxml.jackson.annotation.ObjectIdGenerator in project jackson-databind by FasterXML.

the class DefaultSerializerProvider method findObjectId.

/*
    /**********************************************************
    /* Object Id handling
    /**********************************************************
     */
@Override
public WritableObjectId findObjectId(Object forPojo, ObjectIdGenerator<?> generatorType) {
    if (_seenObjectIds == null) {
        _seenObjectIds = _createObjectIdMap();
    } else {
        WritableObjectId oid = _seenObjectIds.get(forPojo);
        if (oid != null) {
            return oid;
        }
    }
    // Not seen yet; must add an entry, return it. For that, we need generator
    ObjectIdGenerator<?> generator = null;
    if (_objectIdGenerators == null) {
        _objectIdGenerators = new ArrayList<ObjectIdGenerator<?>>(8);
    } else {
        for (int i = 0, len = _objectIdGenerators.size(); i < len; ++i) {
            ObjectIdGenerator<?> gen = _objectIdGenerators.get(i);
            if (gen.canUseFor(generatorType)) {
                generator = gen;
                break;
            }
        }
    }
    if (generator == null) {
        generator = generatorType.newForSerialization(this);
        _objectIdGenerators.add(generator);
    }
    WritableObjectId oid = new WritableObjectId(generator);
    _seenObjectIds.put(forPojo, oid);
    return oid;
}
Also used : ObjectIdGenerator(com.fasterxml.jackson.annotation.ObjectIdGenerator) WritableObjectId(com.fasterxml.jackson.databind.ser.impl.WritableObjectId)

Aggregations

ObjectIdGenerator (com.fasterxml.jackson.annotation.ObjectIdGenerator)3 IdKey (com.fasterxml.jackson.annotation.ObjectIdGenerator.IdKey)1 ObjectIdGenerators (com.fasterxml.jackson.annotation.ObjectIdGenerators)1 ObjectIdResolver (com.fasterxml.jackson.annotation.ObjectIdResolver)1 ReadableObjectId (com.fasterxml.jackson.databind.deser.impl.ReadableObjectId)1 FilteredBeanPropertyWriter (com.fasterxml.jackson.databind.ser.impl.FilteredBeanPropertyWriter)1 PropertyBasedObjectIdGenerator (com.fasterxml.jackson.databind.ser.impl.PropertyBasedObjectIdGenerator)1 WritableObjectId (com.fasterxml.jackson.databind.ser.impl.WritableObjectId)1