use of com.fasterxml.jackson.databind.SerializationConfig in project spring-framework by spring-projects.
the class AbstractJackson2HttpMessageConverter method writeInternal.
@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
MediaType contentType = outputMessage.getHeaders().getContentType();
JsonEncoding encoding = getJsonEncoding(contentType);
JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
try {
writePrefix(generator, object);
Class<?> serializationView = null;
FilterProvider filters = null;
Object value = object;
JavaType javaType = null;
if (object instanceof MappingJacksonValue) {
MappingJacksonValue container = (MappingJacksonValue) object;
value = container.getValue();
serializationView = container.getSerializationView();
filters = container.getFilters();
}
if (type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) {
javaType = getJavaType(type, null);
}
ObjectWriter objectWriter;
if (serializationView != null) {
objectWriter = this.objectMapper.writerWithView(serializationView);
} else if (filters != null) {
objectWriter = this.objectMapper.writer(filters);
} else {
objectWriter = this.objectMapper.writer();
}
if (javaType != null && javaType.isContainerType()) {
objectWriter = objectWriter.forType(javaType);
}
SerializationConfig config = objectWriter.getConfig();
if (contentType != null && contentType.isCompatibleWith(MediaType.TEXT_EVENT_STREAM) && config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
objectWriter = objectWriter.with(this.ssePrettyPrinter);
}
objectWriter.writeValue(generator, value);
writeSuffix(generator, object);
generator.flush();
} catch (JsonProcessingException ex) {
throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
}
}
use of com.fasterxml.jackson.databind.SerializationConfig in project torodb by torodb.
the class AbstractBackendSerializer method acceptJsonFormatVisitor.
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType type) throws JsonMappingException {
if (visitor == null) {
return;
}
JsonObjectFormatVisitor v = visitor.expectObjectFormat(type);
SerializerProvider prov = visitor.getProvider();
final SerializationConfig config = prov.getConfig();
BeanDescription beanDesc = config.introspect(type);
JsonSubTypes jsonSubTypes;
if (v != null) {
for (BeanPropertyDefinition propDef : beanDesc.findProperties()) {
if (propDef.isExplicitlyIncluded()) {
jsonSubTypes = propDef.getPrimaryMember().getAnnotation(JsonSubTypes.class);
if (jsonSubTypes != null) {
for (JsonSubTypes.Type jsonSubType : jsonSubTypes.value()) {
JavaType subType = TypeFactory.defaultInstance().constructType(jsonSubType.value());
depositSchemaProperty(v, jsonSubType.name(), subType);
}
} else {
depositSchemaProperty(v, propDef.getName(), propDef.getPrimaryMember().getType(beanDesc.bindingsForBeanType()));
}
}
}
}
}
use of com.fasterxml.jackson.databind.SerializationConfig in project jackson-databind by FasterXML.
the class EnumValuesTest method testConstructWithToString.
@SuppressWarnings("unchecked")
public void testConstructWithToString() {
SerializationConfig cfg = MAPPER.getSerializationConfig().with(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
Class<Enum<?>> enumClass = (Class<Enum<?>>) (Class<?>) ABC.class;
EnumValues values = EnumValues.construct(cfg, enumClass);
assertEquals("A", values.serializedValueFor(ABC.A).toString());
assertEquals("b", values.serializedValueFor(ABC.B).toString());
assertEquals("C", values.serializedValueFor(ABC.C).toString());
assertEquals(3, values.values().size());
assertEquals(3, values.internalMap().size());
}
use of com.fasterxml.jackson.databind.SerializationConfig 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.SerializationConfig in project jackson-databind by FasterXML.
the class TestJDKSerialization method testConfigs.
public void testConfigs() throws IOException {
byte[] base = jdkSerialize(MAPPER.getDeserializationConfig().getBaseSettings());
assertNotNull(jdkDeserialize(base));
// first things first: underlying BaseSettings
DeserializationConfig origDC = MAPPER.getDeserializationConfig();
SerializationConfig origSC = MAPPER.getSerializationConfig();
byte[] dcBytes = jdkSerialize(origDC);
byte[] scBytes = jdkSerialize(origSC);
DeserializationConfig dc = jdkDeserialize(dcBytes);
assertNotNull(dc);
assertEquals(dc._deserFeatures, origDC._deserFeatures);
SerializationConfig sc = jdkDeserialize(scBytes);
assertNotNull(sc);
assertEquals(sc._serFeatures, origSC._serFeatures);
}
Aggregations