com.fasterxml.jackson.databind.ser.std.NullSerializer Java Examples

The following examples show how to use com.fasterxml.jackson.databind.ser.std.NullSerializer. 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: FilterableBeanSerializerModifier.java    From caravan with Apache License 2.0 4 votes vote down vote up
@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
    FilterableType filterableType = _config.getFilterableType(beanDesc.getBeanClass());
    if (filterableType == null)
        return beanProperties;

    for (final BeanPropertyWriter beanPropertyWriter : beanProperties) {
        final FilterableProperty property = filterableType.getProperty(beanPropertyWriter.getName());
        if (property == null || property.getSerializationFilter() == null)
            continue;

        beanPropertyWriter.assignSerializer(new StdSerializer<Object>(Object.class) {

            private static final long serialVersionUID = 1L;

            @Override
            public void serialize(Object value, JsonGenerator gen, final SerializerProvider provider) throws IOException {
                JsonSerializer<Object> serializer = ConcurrentHashMapValues.getOrAdd(_customSerializers, beanPropertyWriter.getType(),
                        new Func<JsonSerializer<Object>>() {
                            @Override
                            public JsonSerializer<Object> execute() {
                                try {
                                    return BeanSerializerFactory.instance.createSerializer(provider, beanPropertyWriter.getType());
                                } catch (Throwable ex) {
                                    _logger.error("Error occurred when creating custom serializer for type: " + beanPropertyWriter.getType(), ex);
                                    return NullSerializer.instance;
                                }
                            }
                        });

                value = property.getSerializationFilter().filter(property, value);
                if (value == null) {
                    gen.writeNull();
                    return;
                }

                serializer.serialize(value, gen, provider);
            }
        });

        _logger.info("Will apply filter {} for property: {}.", property.getSerializationFilter(), property.name());
    }

    return beanProperties;
}
 
Example #2
Source File: EntitySerializer.java    From FROST-Server with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void serializeFieldTyped(
        Entity entity,
        JsonGenerator gen,
        SerializerProvider serializers,
        BeanDescription beanDescription,
        BeanPropertyDefinition beanPropertyDefinition,
        TypeSerializer typeSerializerBase) {
    TypeSerializer typeSerializer = typeSerializerBase;
    try {
        if (typeSerializer == null) {
            typeSerializer = serializers.findTypeSerializer(serializers.constructType(beanPropertyDefinition.getAccessor().getRawType()));
        }
        if (typeSerializer == null) {
            // if not static type if available use dynamic type if available
            Object propertyValue = beanPropertyDefinition.getAccessor().getValue(entity);
            if (propertyValue != null) {
                typeSerializer = serializers.findTypeSerializer(serializers.constructType(propertyValue.getClass()));
            }
        }

        JsonInclude.Value inclusion = beanPropertyDefinition.findInclusion();
        JsonInclude.Value defaultInclusion = serializers.getConfig().getDefaultPropertyInclusion();
        JsonInclude.Value usedInclusion = defaultInclusion.withOverrides(inclusion);
        BeanPropertyWriter bpw = new BeanPropertyWriter(
                beanPropertyDefinition,
                beanPropertyDefinition.getAccessor(),
                beanDescription.getClassAnnotations(),
                beanPropertyDefinition.getAccessor().getType(),
                null, // will be searched automatically
                typeSerializer, // will not be searched automatically
                beanPropertyDefinition.getAccessor().getType(),
                suppressNulls(usedInclusion),
                suppressableValue(serializers.getConfig().getDefaultPropertyInclusion()),
                null);
        if (!bpw.willSuppressNulls()) {
            bpw.assignNullSerializer(NullSerializer.instance);
        }
        bpw.serializeAsField(entity, gen, serializers);
    } catch (Exception ex) {
        LOGGER.error("Failed to serialise entity", ex);
    }
}