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

The following examples show how to use com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer. 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: ManagementConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.
            json().
            //propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE).
            propertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE).
            //featuresToEnable(SerializationFeature.INDENT_OUTPUT).
            //featuresToEnable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY).
            build();
    SimpleModule module = new SimpleModule();
    module.addSerializer(Set.class,
            new StdDelegatingSerializer(Set.class, new StdConverter<Set, List>() {
                @Override
                public List convert(Set value) {
                    LinkedList list = new LinkedList(value);
                    Collections.sort(list);
                    return list;
                }
            })
    );
    objectMapper.registerModule(module);
    HttpMessageConverter c = new MappingJackson2HttpMessageConverter(
            objectMapper
    );
    converters.add(c);
}
 
Example #2
Source File: ManagementConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.
            json().
            //propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE).
            propertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE).
            //featuresToEnable(SerializationFeature.INDENT_OUTPUT).
            //featuresToEnable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY).
            build();
    SimpleModule module = new SimpleModule();
    module.addSerializer(Set.class,
            new StdDelegatingSerializer(Set.class, new StdConverter<Set, List>() {
                @Override
                public List convert(Set value) {
                    LinkedList list = new LinkedList(value);
                    Collections.sort(list);
                    return list;
                }
            })
    );
    objectMapper.registerModule(module);
    HttpMessageConverter c = new MappingJackson2HttpMessageConverter(
            objectMapper
    );
    converters.add(c);
}
 
Example #3
Source File: ManagementConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.
            json().
            //propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE).
            propertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE).
            //featuresToEnable(SerializationFeature.INDENT_OUTPUT).
            //featuresToEnable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY).
            build();
    SimpleModule module = new SimpleModule();
    module.addSerializer(Set.class,
            new StdDelegatingSerializer(Set.class, new StdConverter<Set, List>() {
                @Override
                public List convert(Set value) {
                    LinkedList list = new LinkedList(value);
                    Collections.sort(list);
                    return list;
                }
            })
    );
    objectMapper.registerModule(module);
    HttpMessageConverter c = new MappingJackson2HttpMessageConverter(
            objectMapper
    );
    converters.add(c);
}
 
Example #4
Source File: GuavaSerializers.java    From jackson-datatypes-collections with Apache License 2.0 5 votes vote down vote up
@Override
public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type,
        BeanDescription beanDesc, JsonFormat.Value formatOverrides)
{
    Class<?> raw = type.getRawClass();
    if (RangeSet.class.isAssignableFrom(raw)) {
        return new RangeSetSerializer();
    }
    if (Range.class.isAssignableFrom(raw)) {
        return new RangeSerializer(_findDeclared(type, Range.class));
    }
    if (Table.class.isAssignableFrom(raw)) {
        return new TableSerializer(_findDeclared(type, Table.class));
    }
    if (HostAndPort.class.isAssignableFrom(raw)) {
        return ToStringSerializer.instance;
    }
    if (InternetDomainName.class.isAssignableFrom(raw)) {
        return ToStringSerializer.instance;
    }
    // not sure how useful, but why not?
    if (CacheBuilderSpec.class.isAssignableFrom(raw) || CacheBuilder.class.isAssignableFrom(raw)) {
        return ToStringSerializer.instance;
    }
    if (HashCode.class.isAssignableFrom(raw)) {
        return ToStringSerializer.instance;
    }
    if (FluentIterable.class.isAssignableFrom(raw)) {
        JavaType iterableType = _findDeclared(type, Iterable.class);
        return new StdDelegatingSerializer(FluentConverter.instance, iterableType, null, null);
    }
    return null;
}
 
Example #5
Source File: BeanSerializerFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Main serializer constructor method. We will have to be careful
 * with respect to ordering of various method calls: essentially
 * we want to reliably figure out which classes are standard types,
 * and which are beans. The problem is that some bean Classes may
 * implement standard interfaces (say, {@link java.lang.Iterable}.
 *<p>
 * Note: sub-classes may choose to complete replace implementation,
 * if they want to alter priority of serializer lookups.
 */
@Override
@SuppressWarnings("unchecked")
public JsonSerializer<Object> createSerializer(SerializerProvider prov,
        JavaType origType)
    throws JsonMappingException
{
    // Very first thing, let's check if there is explicit serializer annotation:
    final SerializationConfig config = prov.getConfig();
    BeanDescription beanDesc = config.introspect(origType);
    JsonSerializer<?> ser = findSerializerFromAnnotation(prov, beanDesc.getClassInfo());
    if (ser != null) {
        return (JsonSerializer<Object>) ser;
    }
    boolean staticTyping;
    // Next: we may have annotations that further indicate actual type to use (a super type)
    final AnnotationIntrospector intr = config.getAnnotationIntrospector();
    JavaType type;

    if (intr == null) {
        type = origType;
    } else {
        try {
            type = intr.refineSerializationType(config, beanDesc.getClassInfo(), origType);
        } catch (JsonMappingException e) {
            return prov.reportBadTypeDefinition(beanDesc, e.getMessage());
        }
    }
    if (type == origType) { // no changes, won't force static typing
        staticTyping = false;
    } else { // changes; assume static typing; plus, need to re-introspect if class differs
        staticTyping = true;
        if (!type.hasRawClass(origType.getRawClass())) {
            beanDesc = config.introspect(type);
        }
    }
    // Slight detour: do we have a Converter to consider?
    Converter<Object,Object> conv = beanDesc.findSerializationConverter();
    if (conv == null) { // no, simple
        return (JsonSerializer<Object>) _createSerializer2(prov, type, beanDesc, staticTyping);
    }
    JavaType delegateType = conv.getOutputType(prov.getTypeFactory());
    
    // One more twist, as per [databind#288]; probably need to get new BeanDesc
    if (!delegateType.hasRawClass(type.getRawClass())) {
        beanDesc = config.introspect(delegateType);
        // [#359]: explicitly check (again) for @JsonSerializer...
        ser = findSerializerFromAnnotation(prov, beanDesc.getClassInfo());
    }
    // [databind#731]: Should skip if nominally java.lang.Object
    if (ser == null && !delegateType.isJavaLangObject()) {
        ser = _createSerializer2(prov, delegateType, beanDesc, true);
    }
    return new StdDelegatingSerializer(conv, delegateType, ser);
}
 
Example #6
Source File: DeterministicObjectMapper.java    From StubbornJava with MIT License 4 votes vote down vote up
@Override
protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
                                               JavaType delegateType, JsonSerializer<?> delegateSerializer)
{
    return new StdDelegatingSerializer(converter, delegateType, delegateSerializer);
}
 
Example #7
Source File: DeterministicObjectMapper.java    From StubbornJava with MIT License 4 votes vote down vote up
@Override
protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
                                               JavaType delegateType, JsonSerializer<?> delegateSerializer)
{
    return new StdDelegatingSerializer(converter, delegateType, delegateSerializer);
}
 
Example #8
Source File: RangeSetSerializer.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Override
protected @Nonnull StdDelegatingSerializer withDelegate(
    Converter<Object, ?> converter, JavaType delegateType, JsonSerializer<?> delegateSerializer) {
  return this;
}