Example usage for com.fasterxml.jackson.databind JsonSerializer serializeWithType

List of usage examples for com.fasterxml.jackson.databind JsonSerializer serializeWithType

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonSerializer serializeWithType.

Prototype

public void serializeWithType(T paramT, JsonGenerator paramJsonGenerator,
            SerializerProvider paramSerializerProvider, TypeSerializer paramTypeSerializer) 

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  www  . ja va  2s . c  om
            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);
    }
}