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());
}
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())));
}
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());
}
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;
}
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;
}
Aggregations