Java Code Examples for com.fasterxml.jackson.databind.ser.Serializers

The following examples show how to use com.fasterxml.jackson.databind.ser.Serializers. These examples are extracted from open source projects. 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
@Override
@SuppressWarnings("unchecked")
public JsonSerializer<Object> createSerializer(SerializerProvider prov, JavaType origType)
        throws JsonMappingException
{
    for (Serializers serializers : customSerializers()) {
        JsonSerializer<?> ser = serializers.findSerializer(prov.getConfig(), origType, null);
        if (ser != null) {
            return (JsonSerializer<Object>) ser;
        }
    }
    throw new IllegalArgumentException("No explicitly configured serializer for " + origType);
}
 
Example 2
@Test
public void modules() {
	NumberSerializer serializer1 = new NumberSerializer(Integer.class);
	SimpleModule module = new SimpleModule();
	module.addSerializer(Integer.class, serializer1);
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules(module).build();
	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(serializer1, serializers.findSerializer(null, SimpleType.construct(Integer.class), null));
}
 
Example 3
@Test
@SuppressWarnings("unchecked")
public void modulesToInstallByClass() {
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
			.modulesToInstall(CustomIntegerModule.class)
			.build();
	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(CustomIntegerSerializer.class,
			serializers.findSerializer(null, SimpleType.construct(Integer.class), null).getClass());
}
 
Example 4
@Test
public void modulesToInstallByInstance() {
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
			.modulesToInstall(new CustomIntegerModule())
			.build();
	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(CustomIntegerSerializer.class,
			serializers.findSerializer(null, SimpleType.construct(Integer.class), null).getClass());
}
 
Example 5
@Test
public void serializerByType() {
	JsonSerializer<Number> serializer = new NumberSerializer(Integer.class);
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
			.modules(new ArrayList<>())  // Disable well-known modules detection
			.serializerByType(Boolean.class, serializer)
			.build();
	assertTrue(getSerializerFactoryConfig(objectMapper).hasSerializers());
	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(serializer, serializers.findSerializer(null, SimpleType.construct(Boolean.class), null));
}
 
Example 6
@Test
public void setModules() {
	NumberSerializer serializer = new NumberSerializer(Integer.class);
	SimpleModule module = new SimpleModule();
	module.addSerializer(Integer.class, serializer);

	this.factory.setModules(Arrays.asList(new Module[]{module}));
	this.factory.afterPropertiesSet();
	ObjectMapper objectMapper = this.factory.getObject();

	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(serializer, serializers.findSerializer(null, SimpleType.construct(Integer.class), null));
}
 
Example 7
@Test
public void modules() {
	NumberSerializer serializer1 = new NumberSerializer(Integer.class);
	SimpleModule module = new SimpleModule();
	module.addSerializer(Integer.class, serializer1);
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules(module).build();
	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(serializer1, serializers.findSerializer(null, SimpleType.construct(Integer.class), null));
}
 
Example 8
@Test
@SuppressWarnings("unchecked")
public void modulesToInstallByClass() {
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
			.modulesToInstall(CustomIntegerModule.class)
			.build();
	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(CustomIntegerSerializer.class,
			serializers.findSerializer(null, SimpleType.construct(Integer.class), null).getClass());
}
 
Example 9
@Test
public void modulesToInstallByInstance() {
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
			.modulesToInstall(new CustomIntegerModule())
			.build();
	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(CustomIntegerSerializer.class,
			serializers.findSerializer(null, SimpleType.construct(Integer.class), null).getClass());
}
 
Example 10
@Test
public void serializerByType() {
	JsonSerializer<Number> serializer = new NumberSerializer(Integer.class);
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
			.modules(new ArrayList<>())  // Disable well-known modules detection
			.serializerByType(Boolean.class, serializer)
			.build();
	assertTrue(getSerializerFactoryConfig(objectMapper).hasSerializers());
	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(serializer, serializers.findSerializer(null, SimpleType.construct(Boolean.class), null));
}
 
Example 11
@Test
public void setModules() {
	NumberSerializer serializer = new NumberSerializer(Integer.class);
	SimpleModule module = new SimpleModule();
	module.addSerializer(Integer.class, serializer);

	this.factory.setModules(Arrays.asList(new Module[]{module}));
	this.factory.afterPropertiesSet();
	ObjectMapper objectMapper = this.factory.getObject();

	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(serializer, serializers.findSerializer(null, SimpleType.construct(Integer.class), null));
}
 
Example 12
Source Project: jfilter   Source File: FilterObjectMapperBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
private boolean serializerExist(Iterable<Serializers> serializers, Class serializer) {
    while (serializers.iterator().hasNext()) {
        if (serializers.iterator().next().getClass().equals(serializer))
            return true;
    }
    return false;
}
 
Example 13
Source Project: lams   Source File: OptionalHandlerFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type,
        BeanDescription beanDesc)
{
    final Class<?> rawType = type.getRawClass();

    if (_jdk7Helper != null) {
        JsonSerializer<?> ser = _jdk7Helper.getSerializerForJavaNioFilePath(rawType);
        if (ser != null) {
            return ser;
        }
    }
    if ((CLASS_DOM_NODE != null) && CLASS_DOM_NODE.isAssignableFrom(rawType)) {
        return (JsonSerializer<?>) instantiate(SERIALIZER_FOR_DOM_NODE);
    }
    String className = rawType.getName();
    String factoryName;
    if (className.startsWith(PACKAGE_PREFIX_JAVAX_XML) || hasSuperClassStartingWith(rawType, PACKAGE_PREFIX_JAVAX_XML)) {
        factoryName = SERIALIZERS_FOR_JAVAX_XML;
    } else {
        return null;
    }

    Object ob = instantiate(factoryName);
    if (ob == null) { // could warn, if we had logging system (j.u.l?)
        return null;
    }
    return ((Serializers) ob).findSerializer(config, type, beanDesc);
}
 
Example 14
@Test
public void modules() {
	NumberSerializer serializer1 = new NumberSerializer(Integer.class);
	SimpleModule module = new SimpleModule();
	module.addSerializer(Integer.class, serializer1);
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules(module).build();
	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(serializer1, serializers.findSerializer(null, SimpleType.construct(Integer.class), null));
}
 
Example 15
@Test
@SuppressWarnings("unchecked")
public void modulesToInstallByClass() {
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modulesToInstall(CustomIntegerModule.class).build();
	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(CustomIntegerSerializer.class, serializers.findSerializer(null, SimpleType.construct(Integer.class), null).getClass());
}
 
Example 16
@Test
public void serializerByType() {
	JsonSerializer<Number> serializer = new NumberSerializer(Integer.class);
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
			.modules(new ArrayList<>())  // Disable well-known modules detection
			.serializerByType(Boolean.class, serializer).build();
	assertTrue(getSerializerFactoryConfig(objectMapper).hasSerializers());
	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(serializer, serializers.findSerializer(null, SimpleType.construct(Boolean.class), null));
}
 
Example 17
@Test
public void setModules() {
	NumberSerializer serializer = new NumberSerializer(Integer.class);
	SimpleModule module = new SimpleModule();
	module.addSerializer(Integer.class, serializer);

	this.factory.setModules(Arrays.asList(new Module[]{module}));
	this.factory.afterPropertiesSet();
	ObjectMapper objectMapper = this.factory.getObject();

	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(serializer, serializers.findSerializer(null, SimpleType.construct(Integer.class), null));
}
 
Example 18
Source Project: immutables   Source File: JacksonCodecs.java    License: Apache License 2.0 5 votes vote down vote up
public static Serializers serializers(final CodecRegistry registry) {
  return new Serializers.Base() {
    @Override
    public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) {
      try {
        Codec<?> codec = registry.get(type.getRawClass());
        return serializer(codec);
      } catch (CodecConfigurationException e) {
        return null;
      }
    }
  };
}
 
Example 19
@Test
public void completeSetup() {
	NopAnnotationIntrospector annotationIntrospector = NopAnnotationIntrospector.instance;
	ObjectMapper objectMapper = new ObjectMapper();

	this.factory.setObjectMapper(objectMapper);
	assertTrue(this.factory.isSingleton());
	assertEquals(ObjectMapper.class, this.factory.getObjectType());

	Map<Class<?>, JsonDeserializer<?>> deserializers = new HashMap<>();
	deserializers.put(Date.class, new DateDeserializer());

	JsonSerializer<Class<?>> serializer1 = new ClassSerializer();
	JsonSerializer<Number> serializer2 = new NumberSerializer(Integer.class);

	// Disable well-known modules detection
	this.factory.setModules(new ArrayList<>());
	this.factory.setSerializers(serializer1);
	this.factory.setSerializersByType(Collections.singletonMap(Boolean.class, serializer2));
	this.factory.setDeserializersByType(deserializers);
	this.factory.setAnnotationIntrospector(annotationIntrospector);

	this.factory.setFeaturesToEnable(SerializationFeature.FAIL_ON_EMPTY_BEANS,
			DeserializationFeature.UNWRAP_ROOT_VALUE,
			JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER,
			JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);

	this.factory.setFeaturesToDisable(MapperFeature.AUTO_DETECT_GETTERS,
			MapperFeature.AUTO_DETECT_FIELDS,
			JsonParser.Feature.AUTO_CLOSE_SOURCE,
			JsonGenerator.Feature.QUOTE_FIELD_NAMES);

	assertFalse(getSerializerFactoryConfig(objectMapper).hasSerializers());
	assertFalse(getDeserializerFactoryConfig(objectMapper).hasDeserializers());

	this.factory.setSerializationInclusion(Include.NON_NULL);
	this.factory.afterPropertiesSet();

	assertSame(objectMapper, this.factory.getObject());
	assertTrue(getSerializerFactoryConfig(objectMapper).hasSerializers());
	assertTrue(getDeserializerFactoryConfig(objectMapper).hasDeserializers());

	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(serializer1, serializers.findSerializer(null, SimpleType.construct(Class.class), null));
	assertSame(serializer2, serializers.findSerializer(null, SimpleType.construct(Boolean.class), null));
	assertNull(serializers.findSerializer(null, SimpleType.construct(Number.class), null));

	assertSame(annotationIntrospector, objectMapper.getSerializationConfig().getAnnotationIntrospector());
	assertSame(annotationIntrospector, objectMapper.getDeserializationConfig().getAnnotationIntrospector());

	assertTrue(objectMapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
	assertTrue(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
	assertTrue(objectMapper.getFactory().isEnabled(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER));
	assertTrue(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS));

	assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
	assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
	assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
	assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
	assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
	assertFalse(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));
	assertSame(Include.NON_NULL, objectMapper.getSerializationConfig().getSerializationInclusion());
}
 
Example 20
@Test
public void completeSetup() {
	NopAnnotationIntrospector annotationIntrospector = NopAnnotationIntrospector.instance;
	ObjectMapper objectMapper = new ObjectMapper();

	this.factory.setObjectMapper(objectMapper);
	assertTrue(this.factory.isSingleton());
	assertEquals(ObjectMapper.class, this.factory.getObjectType());

	Map<Class<?>, JsonDeserializer<?>> deserializers = new HashMap<>();
	deserializers.put(Date.class, new DateDeserializer());

	JsonSerializer<Class<?>> serializer1 = new ClassSerializer();
	JsonSerializer<Number> serializer2 = new NumberSerializer(Integer.class);

	// Disable well-known modules detection
	this.factory.setModules(new ArrayList<>());
	this.factory.setSerializers(serializer1);
	this.factory.setSerializersByType(Collections.singletonMap(Boolean.class, serializer2));
	this.factory.setDeserializersByType(deserializers);
	this.factory.setAnnotationIntrospector(annotationIntrospector);

	this.factory.setFeaturesToEnable(SerializationFeature.FAIL_ON_EMPTY_BEANS,
			DeserializationFeature.UNWRAP_ROOT_VALUE,
			JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER,
			JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);

	this.factory.setFeaturesToDisable(MapperFeature.AUTO_DETECT_GETTERS,
			MapperFeature.AUTO_DETECT_FIELDS,
			JsonParser.Feature.AUTO_CLOSE_SOURCE,
			JsonGenerator.Feature.QUOTE_FIELD_NAMES);

	assertFalse(getSerializerFactoryConfig(objectMapper).hasSerializers());
	assertFalse(getDeserializerFactoryConfig(objectMapper).hasDeserializers());

	this.factory.setSerializationInclusion(Include.NON_NULL);
	this.factory.afterPropertiesSet();

	assertSame(objectMapper, this.factory.getObject());
	assertTrue(getSerializerFactoryConfig(objectMapper).hasSerializers());
	assertTrue(getDeserializerFactoryConfig(objectMapper).hasDeserializers());

	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(serializer1, serializers.findSerializer(null, SimpleType.construct(Class.class), null));
	assertSame(serializer2, serializers.findSerializer(null, SimpleType.construct(Boolean.class), null));
	assertNull(serializers.findSerializer(null, SimpleType.construct(Number.class), null));

	assertSame(annotationIntrospector, objectMapper.getSerializationConfig().getAnnotationIntrospector());
	assertSame(annotationIntrospector, objectMapper.getDeserializationConfig().getAnnotationIntrospector());

	assertTrue(objectMapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
	assertTrue(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
	assertTrue(objectMapper.getFactory().isEnabled(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER));
	assertTrue(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS));

	assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
	assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
	assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
	assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
	assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
	assertFalse(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));
	assertSame(Include.NON_NULL, objectMapper.getSerializationConfig().getSerializationInclusion());
}
 
Example 21
@Test
public void modulesToInstallByInstance() {
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modulesToInstall(new CustomIntegerModule()).build();
	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(CustomIntegerSerializer.class, serializers.findSerializer(null, SimpleType.construct(Integer.class), null).getClass());
}
 
Example 22
@Test
public void completeSetup() {
	NopAnnotationIntrospector annotationIntrospector = NopAnnotationIntrospector.instance;
	ObjectMapper objectMapper = new ObjectMapper();

	this.factory.setObjectMapper(objectMapper);
	assertTrue(this.factory.isSingleton());
	assertEquals(ObjectMapper.class, this.factory.getObjectType());

	Map<Class<?>, JsonDeserializer<?>> deserializers = new HashMap<Class<?>, JsonDeserializer<?>>();
	deserializers.put(Date.class, new DateDeserializer());

	JsonSerializer<Class<?>> serializer1 = new ClassSerializer();
	JsonSerializer<Number> serializer2 = new NumberSerializer(Integer.class);

	// Disable well-known modules detection
	this.factory.setModules(new ArrayList<>());
	this.factory.setSerializers(serializer1);
	this.factory.setSerializersByType(Collections.singletonMap(Boolean.class, serializer2));
	this.factory.setDeserializersByType(deserializers);
	this.factory.setAnnotationIntrospector(annotationIntrospector);

	this.factory.setFeaturesToEnable(SerializationFeature.FAIL_ON_EMPTY_BEANS,
			DeserializationFeature.UNWRAP_ROOT_VALUE,
			JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER,
			JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);

	this.factory.setFeaturesToDisable(MapperFeature.AUTO_DETECT_GETTERS,
			MapperFeature.AUTO_DETECT_FIELDS,
			JsonParser.Feature.AUTO_CLOSE_SOURCE,
			JsonGenerator.Feature.QUOTE_FIELD_NAMES);

	assertFalse(getSerializerFactoryConfig(objectMapper).hasSerializers());
	assertFalse(getDeserializerFactoryConfig(objectMapper).hasDeserializers());

	this.factory.setSerializationInclusion(Include.NON_NULL);
	this.factory.afterPropertiesSet();

	assertSame(objectMapper, this.factory.getObject());
	assertTrue(getSerializerFactoryConfig(objectMapper).hasSerializers());
	assertTrue(getDeserializerFactoryConfig(objectMapper).hasDeserializers());

	Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
	assertSame(serializer1, serializers.findSerializer(null, SimpleType.construct(Class.class), null));
	assertSame(serializer2, serializers.findSerializer(null, SimpleType.construct(Boolean.class), null));
	assertNull(serializers.findSerializer(null, SimpleType.construct(Number.class), null));

	assertSame(annotationIntrospector, objectMapper.getSerializationConfig().getAnnotationIntrospector());
	assertSame(annotationIntrospector, objectMapper.getDeserializationConfig().getAnnotationIntrospector());

	assertTrue(objectMapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
	assertTrue(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
	assertTrue(objectMapper.getFactory().isEnabled(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER));
	assertTrue(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS));

	assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
	assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
	assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
	assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
	assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
	assertFalse(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));
	assertSame(Include.NON_NULL, objectMapper.getSerializationConfig().getSerializationInclusion());
}
 
Example 23
Source Project: lams   Source File: Module.java    License: GNU General Public License v2.0 2 votes vote down vote up
/**
 * Method that module can use to register additional serializers to use for
 * handling types.
 * 
 * @param s Object that can be called to find serializer for types supported
 *   by module (null returned for non-supported types)
 */
public void addSerializers(Serializers s);
 
Example 24
Source Project: lams   Source File: Module.java    License: GNU General Public License v2.0 2 votes vote down vote up
/**
 * Method that module can use to register additional serializers to use for
 * handling Map key values (which are separate from value serializers because
 * they must write <code>JsonToken.FIELD_NAME</code> instead of String value).
 */
public void addKeySerializers(Serializers s);