Example usage for com.fasterxml.jackson.databind.ser.impl PropertySerializerMap serializerFor

List of usage examples for com.fasterxml.jackson.databind.ser.impl PropertySerializerMap serializerFor

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.ser.impl PropertySerializerMap serializerFor.

Prototype

public abstract JsonSerializer<Object> serializerFor(Class<?> paramClass);

Source Link

Usage

From source file:de.ks.flatadocdb.defaults.json.RelationCollectionPropertyWriter.java

@Override
public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
    Object value = (_accessorMethod == null) ? _field.get(bean) : _accessorMethod.invoke(bean);
    if (value instanceof Collection) {
        Collection<String> ids;
        if (value instanceof List) {
            ids = new ArrayList<>();
        } else {//from w  w w.  j a  v  a2s.com
            ids = new HashSet<>();
        }
        for (Object object : ((Collection) value)) {
            if (object != null) {
                EntityDescriptor entityDescriptor = metaModel.getEntityDescriptor(object.getClass());
                String id = entityDescriptor.getId(object);
                log.trace("Replaced object {} of collection by relation id {}.", object, id);
                ids.add(id);
            }
        }
        value = ids;
    } else if (value != null) {
        EntityDescriptor entityDescriptor = metaModel.getEntityDescriptor(value.getClass());
        String id = entityDescriptor.getId(value);
        value = id;
        log.trace("Replaced single object {} by relation id {}.", value, id);
    }
    // Null handling is bit different, check that first
    if (value == null) {
        if (_nullSerializer != null) {
            gen.writeFieldName(_name);
            _nullSerializer.serialize(null, gen, prov);
        }
        return;
    }
    // then find serializer to use
    JsonSerializer<Object> ser = _serializer;
    if (ser == null) {
        Class<?> cls = value.getClass();
        PropertySerializerMap m = _dynamicSerializers;
        ser = m.serializerFor(cls);
        if (ser == null) {
            ser = _findAndAddDynamic(m, cls, prov);
        }
    }
    // and then see if we must suppress certain values (default, empty)
    if (_suppressableValue != null) {
        if (MARKER_FOR_EMPTY == _suppressableValue) {
            if (ser.isEmpty(prov, value)) {
                return;
            }
        } else if (_suppressableValue.equals(value)) {
            return;
        }
    }
    // For non-nulls: simple check for direct cycles
    if (value == bean) {
        // three choices: exception; handled by call; or pass-through
        if (_handleSelfReference(bean, gen, prov, ser)) {
            return;
        }
    }
    gen.writeFieldName(_name);
    if (_typeSerializer == null) {
        ser.serialize(value, gen, prov);
    } else {
        ser.serializeWithType(value, gen, prov, _typeSerializer);
    }
}