Search in sources :

Example 1 with SerializerFactory

use of com.fasterxml.jackson.databind.ser.SerializerFactory in project spring-framework by spring-projects.

the class MappingJackson2XmlViewTests method renderWithCustomSerializerLocatedByFactory.

@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
    SerializerFactory factory = new DelegatingSerializerFactory(null);
    XmlMapper mapper = new XmlMapper();
    mapper.setSerializerFactory(factory);
    view.setObjectMapper(mapper);
    Object bean = new TestBeanSimple();
    Map<String, Object> model = new HashMap<>();
    model.put("foo", bean);
    view.render(model, request, response);
    String result = response.getContentAsString();
    assertTrue(result.length() > 0);
    assertTrue(result.contains("custom</testBeanSimple>"));
    validateResult();
}
Also used : SerializerFactory(com.fasterxml.jackson.databind.ser.SerializerFactory) BeanSerializerFactory(com.fasterxml.jackson.databind.ser.BeanSerializerFactory) HashMap(java.util.HashMap) ScriptableObject(org.mozilla.javascript.ScriptableObject) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Test(org.junit.Test)

Example 2 with SerializerFactory

use of com.fasterxml.jackson.databind.ser.SerializerFactory in project spring-framework by spring-projects.

the class MappingJackson2JsonViewTests method renderWithCustomSerializerLocatedByFactory.

@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
    SerializerFactory factory = new DelegatingSerializerFactory(null);
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializerFactory(factory);
    view.setObjectMapper(mapper);
    Object bean = new TestBeanSimple();
    Map<String, Object> model = new HashMap<>();
    model.put("foo", bean);
    model.put("bar", new TestChildBean());
    view.render(model, request, response);
    String result = response.getContentAsString();
    assertTrue(result.length() > 0);
    assertTrue(result.contains("\"foo\":{\"testBeanSimple\":\"custom\"}"));
    validateResult();
}
Also used : SerializerFactory(com.fasterxml.jackson.databind.ser.SerializerFactory) BeanSerializerFactory(com.fasterxml.jackson.databind.ser.BeanSerializerFactory) HashMap(java.util.HashMap) ScriptableObject(org.mozilla.javascript.ScriptableObject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with SerializerFactory

use of com.fasterxml.jackson.databind.ser.SerializerFactory in project spring-boot by spring-projects.

the class DataSourceJsonSerializationTests method serializerFactory.

@Test
public void serializerFactory() throws Exception {
    DataSource dataSource = new DataSource();
    SerializerFactory factory = BeanSerializerFactory.instance.withSerializerModifier(new GenericSerializerModifier());
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializerFactory(factory);
    String value = mapper.writeValueAsString(dataSource);
    assertThat(value.contains("\"url\":")).isTrue();
}
Also used : SerializerFactory(com.fasterxml.jackson.databind.ser.SerializerFactory) BeanSerializerFactory(com.fasterxml.jackson.databind.ser.BeanSerializerFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataSource(org.apache.tomcat.jdbc.pool.DataSource) Test(org.junit.Test)

Example 4 with SerializerFactory

use of com.fasterxml.jackson.databind.ser.SerializerFactory in project spring-boot by spring-projects.

the class ConfigurationPropertiesReportEndpoint method applySerializationModifier.

/**
	 * Ensure only bindable and non-cyclic bean properties are reported.
	 * @param mapper the object mapper
	 */
private void applySerializationModifier(ObjectMapper mapper) {
    SerializerFactory factory = BeanSerializerFactory.instance.withSerializerModifier(new GenericSerializerModifier());
    mapper.setSerializerFactory(factory);
}
Also used : SerializerFactory(com.fasterxml.jackson.databind.ser.SerializerFactory) BeanSerializerFactory(com.fasterxml.jackson.databind.ser.BeanSerializerFactory)

Aggregations

BeanSerializerFactory (com.fasterxml.jackson.databind.ser.BeanSerializerFactory)4 SerializerFactory (com.fasterxml.jackson.databind.ser.SerializerFactory)4 Test (org.junit.Test)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HashMap (java.util.HashMap)2 ScriptableObject (org.mozilla.javascript.ScriptableObject)2 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1 DataSource (org.apache.tomcat.jdbc.pool.DataSource)1