Search in sources :

Example 26 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project jackson-databind by FasterXML.

the class TestTypeResolution method testGeneric.

public void testGeneric() {
    TypeFactory tf = TypeFactory.defaultInstance();
    // First, via simple sub-class
    JavaType t = tf.constructType(DoubleRange.class);
    JavaType rangeParams = t.findSuperType(Range.class);
    assertEquals(1, rangeParams.containedTypeCount());
    assertEquals(Double.class, rangeParams.containedType(0).getRawClass());
    // then using TypeRef
    t = tf.constructType(new TypeReference<DoubleRange>() {
    });
    rangeParams = t.findSuperType(Range.class);
    assertEquals(1, rangeParams.containedTypeCount());
    assertEquals(Double.class, rangeParams.containedType(0).getRawClass());
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) TypeReference(com.fasterxml.jackson.core.type.TypeReference) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory)

Example 27 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project jackson-databind by FasterXML.

the class BeanUtilTest method testGetDefaultValue.

public void testGetDefaultValue() {
    TypeFactory tf = TypeFactory.defaultInstance();
    // For collection/array/Map types, should give `NOT_EMPTY`:
    assertEquals(JsonInclude.Include.NON_EMPTY, BeanUtil.getDefaultValue(tf.constructType(Map.class)));
    assertEquals(JsonInclude.Include.NON_EMPTY, BeanUtil.getDefaultValue(tf.constructType(List.class)));
    assertEquals(JsonInclude.Include.NON_EMPTY, BeanUtil.getDefaultValue(tf.constructType(Object[].class)));
    // as well as ReferenceTypes, String
    assertEquals(JsonInclude.Include.NON_EMPTY, BeanUtil.getDefaultValue(tf.constructType(AtomicReference.class)));
    assertEquals("", BeanUtil.getDefaultValue(tf.constructType(String.class)));
    // primitive/wrappers have others
    assertEquals(Integer.valueOf(0), BeanUtil.getDefaultValue(tf.constructType(Integer.class)));
    // but POJOs have no real default
    assertNull(BeanUtil.getDefaultValue(tf.constructType(getClass())));
}
Also used : TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory)

Example 28 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project jackson-databind by FasterXML.

the class DeprecatedTypeHandling1102Test method testDeprecatedTypeResolution.

@SuppressWarnings("deprecation")
public void testDeprecatedTypeResolution() throws Exception {
    TypeFactory tf = MAPPER.getTypeFactory();
    // first, with real (if irrelevant) context
    JavaType t = tf.constructType(Point.class, getClass());
    assertEquals(Point.class, t.getRawClass());
    // and then missing context
    JavaType t2 = tf.constructType(Point.class, (Class<?>) null);
    assertEquals(Point.class, t2.getRawClass());
    JavaType ctxt = tf.constructType(getClass());
    JavaType t3 = tf.constructType(Point.class, ctxt);
    assertEquals(Point.class, t3.getRawClass());
}
Also used : TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory)

Example 29 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project spring-framework by spring-projects.

the class Jackson2JsonEncoder method encodeValue.

private DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory, ResolvableType type, Map<String, Object> hints) {
    TypeFactory typeFactory = this.mapper.getTypeFactory();
    JavaType javaType = typeFactory.constructType(type.getType());
    if (type.isInstance(value)) {
        javaType = getJavaType(type.getType(), null);
    }
    ObjectWriter writer;
    Class<?> jsonView = (Class<?>) hints.get(AbstractJackson2Codec.JSON_VIEW_HINT);
    if (jsonView != null) {
        writer = this.mapper.writerWithView(jsonView);
    } else {
        writer = this.mapper.writer();
    }
    if (javaType != null && javaType.isContainerType()) {
        writer = writer.forType(javaType);
    }
    Boolean sse = (Boolean) hints.get(ServerSentEventHttpMessageWriter.SSE_CONTENT_HINT);
    SerializationConfig config = writer.getConfig();
    if (Boolean.TRUE.equals(sse) && config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        writer = writer.with(this.ssePrettyPrinter);
    }
    DataBuffer buffer = bufferFactory.allocateBuffer();
    OutputStream outputStream = buffer.asOutputStream();
    try {
        writer.writeValue(outputStream, value);
    } catch (IOException ex) {
        throw new CodecException("Error while writing the data", ex);
    }
    return buffer;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) OutputStream(java.io.OutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) CodecException(org.springframework.core.codec.CodecException) IOException(java.io.IOException) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 30 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project EnrichmentMapApp by BaderLab.

the class PropertiesJsonDeserializer method readValue.

public static Object readValue(final String key, final String input, final ObjectMapper mapper, final AbstractChart<?> chart) {
    Object value = null;
    final Class<?> type = chart.getSettingType(key);
    if (type != null) {
        final TypeFactory typeFactory = mapper.getTypeFactory();
        try {
            if (type == Array.class) {
                final Class<?> elementType = chart.getSettingElementType(key);
                if (elementType != null) {
                    final ArrayType arrType = typeFactory.constructArrayType(elementType);
                    if (mapper.canDeserialize(arrType))
                        value = mapper.readValue(input, arrType);
                }
            } else if (List.class.isAssignableFrom(type)) {
                final Class<?> elementType = chart.getSettingElementType(key);
                if (elementType != null) {
                    final CollectionType collType = typeFactory.constructCollectionType(List.class, elementType);
                    if (mapper.canDeserialize(collType))
                        value = mapper.readValue(input, collType);
                }
            } else {
                final JavaType simpleType = typeFactory.constructSimpleType(type, new JavaType[] {});
                if (mapper.canDeserialize(simpleType))
                    value = mapper.readValue(input, simpleType);
            }
        } catch (Exception e) {
            logger.error("Cannot parse JSON field " + key, e);
        }
    }
    return value;
}
Also used : ArrayType(com.fasterxml.jackson.databind.type.ArrayType) JavaType(com.fasterxml.jackson.databind.JavaType) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) List(java.util.List) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)34 JavaType (com.fasterxml.jackson.databind.JavaType)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 IOException (java.io.IOException)8 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)4 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)4 MapType (com.fasterxml.jackson.databind.type.MapType)4 List (java.util.List)4 ArrayList (java.util.ArrayList)3 MetricsModule (com.codahale.metrics.json.MetricsModule)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 PropertyNamingStrategy (com.fasterxml.jackson.databind.PropertyNamingStrategy)2 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)2 ArrayType (com.fasterxml.jackson.databind.type.ArrayType)2 GuavaModule (com.fasterxml.jackson.datatype.guava.GuavaModule)2 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)2 JodaModule (com.fasterxml.jackson.datatype.joda.JodaModule)2 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)2 Query (io.druid.query.Query)2