Java Code Examples for com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap#serializerFor()

The following examples show how to use com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap#serializerFor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ObjectArraySerializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void serializeTypedContents(Object[] value, JsonGenerator jgen, SerializerProvider provider) throws IOException
{
    final int len = value.length;
    final TypeSerializer typeSer = _valueTypeSerializer;
    int i = 0;
    Object elem = null;
    try {
        PropertySerializerMap serializers = _dynamicSerializers;
        for (; i < len; ++i) {
            elem = value[i];
            if (elem == null) {
                provider.defaultSerializeNull(jgen);
                continue;
            }
            Class<?> cc = elem.getClass();
            JsonSerializer<Object> serializer = serializers.serializerFor(cc);
            if (serializer == null) {
                serializer = _findAndAddDynamic(serializers, cc, provider);
            }
            serializer.serializeWithType(elem, jgen, provider, typeSer);
        }
    } catch (Exception e) {
        wrapAndThrow(provider, e, elem, i);
    }
}
 
Example 2
Source File: StdKeySerializers.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void serialize(Object value, JsonGenerator g, SerializerProvider provider)
        throws IOException
{
    Class<?> cls = value.getClass();
    PropertySerializerMap m = _dynamicSerializers;
    JsonSerializer<Object> ser = m.serializerFor(cls);
    if (ser == null) {
        ser = _findAndAddDynamic(m, cls, provider);
    }
    ser.serialize(value, g, provider);
}
 
Example 3
Source File: ObjectArraySerializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void serializeContents(Object[] value, JsonGenerator gen, SerializerProvider provider) throws IOException
{
    final int len = value.length;
    if (len == 0) {
        return;
    }
    if (_elementSerializer != null) {
        serializeContentsUsing(value, gen, provider, _elementSerializer);
        return;
    }
    if (_valueTypeSerializer != null) {
        serializeTypedContents(value, gen, provider);
        return;
    }
    int i = 0;
    Object elem = null;
    try {
        PropertySerializerMap serializers = _dynamicSerializers;
        for (; i < len; ++i) {
            elem = value[i];
            if (elem == null) {
                provider.defaultSerializeNull(gen);
                continue;
            }
            Class<?> cc = elem.getClass();
            JsonSerializer<Object> serializer = serializers.serializerFor(cc);
            if (serializer == null) {
                if (_elementType.hasGenericTypes()) {
                    serializer = _findAndAddDynamic(serializers,
                            provider.constructSpecializedType(_elementType, cc), provider);
                } else {
                    serializer = _findAndAddDynamic(serializers, cc, provider);
                }
            }
            serializer.serialize(elem, gen, provider);
        }
    } catch (Exception e) {
        wrapAndThrow(provider, e, elem, i);
    }
}
 
Example 4
Source File: CollectionSerializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void serializeContents(Collection<?> value, JsonGenerator g, SerializerProvider provider) throws IOException
{
    g.setCurrentValue(value);
    if (_elementSerializer != null) {
        serializeContentsUsing(value, g, provider, _elementSerializer);
        return;
    }
    Iterator<?> it = value.iterator();
    if (!it.hasNext()) {
        return;
    }
    PropertySerializerMap serializers = _dynamicSerializers;
    final TypeSerializer typeSer = _valueTypeSerializer;

    int i = 0;
    try {
        do {
            Object elem = it.next();
            if (elem == null) {
                provider.defaultSerializeNull(g);
            } else {
                Class<?> cc = elem.getClass();
                JsonSerializer<Object> serializer = serializers.serializerFor(cc);
                if (serializer == null) {
                    if (_elementType.hasGenericTypes()) {
                        serializer = _findAndAddDynamic(serializers,
                                provider.constructSpecializedType(_elementType, cc), provider);
                    } else {
                        serializer = _findAndAddDynamic(serializers, cc, provider);
                    }
                    serializers = _dynamicSerializers;
                }
                if (typeSer == null) {
                    serializer.serialize(elem, g, provider);
                } else {
                    serializer.serializeWithType(elem, g, provider, typeSer);
                }
            }
            ++i;
        } while (it.hasNext());
    } catch (Exception e) {
        wrapAndThrow(provider, e, value, i);
    }
}
 
Example 5
Source File: BeanPropertyWriter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Method called to access property that this bean stands for, from within
 * given bean, and to serialize it as a JSON Object field using appropriate
 * serializer.
 */
@Override
public void serializeAsField(Object bean, JsonGenerator gen,
        SerializerProvider prov) throws Exception {
    // inlined 'get()'
    final Object value = (_accessorMethod == null) ? _field.get(bean)
            : _accessorMethod.invoke(bean, (Object[]) null);

    // 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);
    }
}
 
Example 6
Source File: BeanPropertyWriter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Alternative to {@link #serializeAsField} that is used when a POJO is
 * serialized as JSON Array; the difference is that no field names are
 * written.
 * 
 * @since 2.3
 */
@Override
public void serializeAsElement(Object bean, JsonGenerator gen,
        SerializerProvider prov) throws Exception {
    // inlined 'get()'
    final Object value = (_accessorMethod == null) ? _field.get(bean)
            : _accessorMethod.invoke(bean, (Object[]) null);
    if (value == null) { // nulls need specialized handling
        if (_nullSerializer != null) {
            _nullSerializer.serialize(null, gen, prov);
        } else { // can NOT suppress entries in tabular output
            gen.writeNull();
        }
        return;
    }
    // otherwise find serializer to use
    JsonSerializer<Object> ser = _serializer;
    if (ser == null) {
        Class<?> cls = value.getClass();
        PropertySerializerMap map = _dynamicSerializers;
        ser = map.serializerFor(cls);
        if (ser == null) {
            ser = _findAndAddDynamic(map, 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)) { // can NOT suppress entries in
                                            // tabular output
                serializeAsPlaceholder(bean, gen, prov);
                return;
            }
        } else if (_suppressableValue.equals(value)) { // can NOT suppress
                                                       // entries in tabular
                                                       // output
            serializeAsPlaceholder(bean, gen, prov);
            return;
        }
    }
    // For non-nulls: simple check for direct cycles
    if (value == bean) {
        if (_handleSelfReference(bean, gen, prov, ser)) {
            return;
        }
    }
    if (_typeSerializer == null) {
        ser.serialize(value, gen, prov);
    } else {
        ser.serializeWithType(value, gen, prov, _typeSerializer);
    }
}
 
Example 7
Source File: VirtualBeanPropertyWriter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception
{
    // NOTE: mostly copied from base class, but off-lined get() access
    final Object value = value(bean, gen, prov);

    if (value == null) {
        if (_nullSerializer != null) {
            gen.writeFieldName(_name);
            _nullSerializer.serialize(null, gen, prov);
        }
        return;
    }
    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);
        }
    }
    if (_suppressableValue != null) {
        if (MARKER_FOR_EMPTY == _suppressableValue) {
            if (ser.isEmpty(prov, value)) {
                return;
            }
        } else if (_suppressableValue.equals(value)) {
            return;
        }
    }
    if (value == bean) { // simple check for direct cycles
        // 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);
    }
}
 
Example 8
Source File: VirtualBeanPropertyWriter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void serializeAsElement(Object bean, JsonGenerator gen, SerializerProvider prov)
    throws Exception
{
    // NOTE: mostly copied from base class, but off-lined get() access
    final Object value = value(bean, gen, prov);

    if (value == null) {
        if (_nullSerializer != null) {
            _nullSerializer.serialize(null, gen, prov);
        } else {
            gen.writeNull();
        }
        return;
    }
    JsonSerializer<Object> ser = _serializer;
    if (ser == null) {
        Class<?> cls = value.getClass();
        PropertySerializerMap map = _dynamicSerializers;
        ser = map.serializerFor(cls);
        if (ser == null) {
            ser = _findAndAddDynamic(map, cls, prov);
        }
    }
    if (_suppressableValue != null) {
        if (MARKER_FOR_EMPTY == _suppressableValue) {
            if (ser.isEmpty(prov, value)) {
                serializeAsPlaceholder(bean, gen, prov);
                return;
            }
        } else if (_suppressableValue.equals(value)) {
            serializeAsPlaceholder(bean, gen, prov);
            return;
        }
    }
    if (value == bean) {
        if (_handleSelfReference(bean, gen, prov, ser)) {
            return;
        }
    }
    if (_typeSerializer == null) {
        ser.serialize(value, gen, prov);
    } else {
        ser.serializeWithType(value, gen, prov, _typeSerializer);
    }
}
 
Example 9
Source File: TypelessCollectionSerializer.java    From spring-boot-data-geode with Apache License 2.0 4 votes vote down vote up
private JsonSerializer<Object> resolveSerializer(SerializerProvider serializerProvider, Class<?> type)
		throws JsonMappingException {

	JsonSerializer<Object> resolvedSerializer = this._elementSerializer;

	if (Objects.isNull(resolvedSerializer)) {

		PropertySerializerMap dynamicSerializers = this._dynamicSerializers;

		resolvedSerializer = dynamicSerializers.serializerFor(type);

		if (Objects.isNull(resolvedSerializer)) {
			resolvedSerializer = Objects.nonNull(this._elementType) && this._elementType.hasGenericTypes()
				? this._findAndAddDynamic(dynamicSerializers,
					constructSpecializedType(serializerProvider, this._elementType, type), serializerProvider)
				: this._findAndAddDynamic(dynamicSerializers, type, serializerProvider);

		}
	}

	return resolvedSerializer;
}
 
Example 10
Source File: ObjectMethodPropertyWriter.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@Override
public final void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception
{
    if (broken) {
        fallbackWriter.serializeAsField(bean, gen, prov);
        return;
    }
    Object value;
    try {
        value = _propertyAccessor.objectGetter(bean, _propertyIndex);
    } catch (Throwable t) {
        _handleProblem(bean, gen, prov, t, false);
        return;
    }
    // Null (etc) handling; copied from super-class impl
    if (value == null) {
        if (_nullSerializer != null) {
            gen.writeFieldName(_fastName);
            _nullSerializer.serialize(null, gen, prov);
        } else if (!_suppressNulls) {
            gen.writeFieldName(_fastName);
            prov.defaultSerializeNullValue(gen);
        }
        return;
    }
    JsonSerializer<Object> ser = _serializer;
    if (ser == null) {
        Class<?> cls = value.getClass();
        PropertySerializerMap map = _dynamicSerializers;
        ser = map.serializerFor(cls);
        if (ser == null) {
            ser = _findAndAddDynamic(map, cls, prov);
        }
    }
    if (_suppressableValue != null) {
        if (MARKER_FOR_EMPTY == _suppressableValue) {
            if (ser.isEmpty(prov, value)) {
                return;
            }
        } else if (_suppressableValue.equals(value)) {
            return;
        }
    }
    if (value == bean) {
        // three choices: exception; handled by call; or pass-through
        if (_handleSelfReference(bean, gen, prov, ser)) {
            return;
        }
    }
    gen.writeFieldName(_fastName);
    if (_typeSerializer == null) {
        ser.serialize(value, gen, prov);
    } else {
        ser.serializeWithType(value, gen, prov, _typeSerializer);
    }
}
 
Example 11
Source File: ObjectMethodPropertyWriter.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@Override
public final void serializeAsElement(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception
{
    if (broken) {
        fallbackWriter.serializeAsElement(bean, gen, prov);
        return;
    }
    Object value;
    try {
        value = _propertyAccessor.objectGetter(bean, _propertyIndex);
    } catch (Throwable t) {
        _handleProblem(bean, gen, prov, t, true);
        return;
    }
    // Null (etc) handling; copied from super-class impl
    if (value == null) {
        if (_nullSerializer != null) {
            _nullSerializer.serialize(null, gen, prov);
        } else if (_suppressNulls) {
            serializeAsPlaceholder(bean, gen, prov);
        } else {
            prov.defaultSerializeNullValue(gen);
        }
        return;
    }
    JsonSerializer<Object> ser = _serializer;
    if (ser == null) {
        Class<?> cls = value.getClass();
        PropertySerializerMap map = _dynamicSerializers;
        ser = map.serializerFor(cls);
        if (ser == null) {
            ser = _findAndAddDynamic(map, cls, prov);
        }
    }
    if (_suppressableValue != null) {
        if (MARKER_FOR_EMPTY == _suppressableValue) {
            if (ser.isEmpty(prov, value)) {
                serializeAsPlaceholder(bean, gen, prov);
                return;
            }
        } else if (_suppressableValue.equals(value)) {
            serializeAsPlaceholder(bean, gen, prov);
            return;
        }
    }
    if (value == bean) {
        // three choices: exception; handled by call; or pass-through
        if (_handleSelfReference(bean, gen, prov, ser)) {
            return;
        }
    }
    if (_typeSerializer == null) {
        ser.serialize(value, gen, prov);
    } else {
        ser.serializeWithType(value, gen, prov, _typeSerializer);
    }
}
 
Example 12
Source File: ObjectFieldPropertyWriter.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@Override
public final void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception
{
    if (broken) {
        fallbackWriter.serializeAsField(bean, gen, prov);
        return;
    }
    Object value;
    try {
        value = _propertyAccessor.objectField(bean, _propertyIndex);
    } catch (Throwable t) {
        _handleProblem(bean, gen, prov, t, false);
        return;
    }
    // Null (etc) handling; copied from super-class impl
    if (value == null) {
        if (_nullSerializer != null) {
            gen.writeFieldName(_fastName);
            _nullSerializer.serialize(null, gen, prov);
        } else if (!_suppressNulls) {
            gen.writeFieldName(_fastName);
            prov.defaultSerializeNullValue(gen);
        }
        return;
    }
    JsonSerializer<Object> ser = _serializer;
    if (ser == null) {
        Class<?> cls = value.getClass();
        PropertySerializerMap map = _dynamicSerializers;
        ser = map.serializerFor(cls);
        if (ser == null) {
            ser = _findAndAddDynamic(map, cls, prov);
        }
    }
    if (_suppressableValue != null) {
        if (MARKER_FOR_EMPTY == _suppressableValue) {
            if (ser.isEmpty(prov, value)) {
                return;
            }
        } else if (_suppressableValue.equals(value)) {
            return;
        }
    }
    if (value == bean) {
        // three choices: exception; handled by call; or pass-through
        if (_handleSelfReference(bean, gen, prov, ser)) {
            return;
        }
    }
    gen.writeFieldName(_fastName);
    if (_typeSerializer == null) {
        ser.serialize(value, gen, prov);
    } else {
        ser.serializeWithType(value, gen, prov, _typeSerializer);
    }
}
 
Example 13
Source File: ObjectFieldPropertyWriter.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@Override
public final void serializeAsElement(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception
{
    if (broken) {
        fallbackWriter.serializeAsElement(bean, gen, prov);
        return;
    }
    Object value;
    try {
        value = _propertyAccessor.objectField(bean, _propertyIndex);
    } catch (Throwable t) {
        _handleProblem(bean, gen, prov, t, true);
        return;
    }
    if (value == null) {
        if (_nullSerializer != null) {
            _nullSerializer.serialize(null, gen, prov);
        } else if (_suppressNulls) {
            serializeAsPlaceholder(bean, gen, prov);
        } else {
            prov.defaultSerializeNullValue(gen);
        }
        return;
    }
    JsonSerializer<Object> ser = _serializer;
    if (ser == null) {
        Class<?> cls = value.getClass();
        PropertySerializerMap map = _dynamicSerializers;
        ser = map.serializerFor(cls);
        if (ser == null) {
            ser = _findAndAddDynamic(map, cls, prov);
        }
    }
    if (_suppressableValue != null) {
        if (MARKER_FOR_EMPTY == _suppressableValue) {
            if (ser.isEmpty(prov, value)) {
                serializeAsPlaceholder(bean, gen, prov);
                return;
            }
        } else if (_suppressableValue.equals(value)) {
            serializeAsPlaceholder(bean, gen, prov);
            return;
        }
    }
    if (value == bean) {
        // three choices: exception; handled by call; or pass-through
        if (_handleSelfReference(bean, gen, prov, ser)) {
            return;
        }
    }
    if (_typeSerializer == null) {
        ser.serialize(value, gen, prov);
    } else {
        ser.serializeWithType(value, gen, prov, _typeSerializer);
    }
}