com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer Java Examples

The following examples show how to use com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer. 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
Source File: DefaultJsonService.java    From imagej-server with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs and initializes a JsonService with an {@link ObjectService}.
 * 
 * @param objectService
 */
public DefaultJsonService(final ObjectService objectService) {

	idToObjDeserializer = new UntypedObjectDeserializer(null, null) {

		@Override
		public Object deserialize(JsonParser p, DeserializationContext ctxt)
			throws IOException, JsonProcessingException
		{
			final Object obj = super.deserialize(p, ctxt);
			if (!(obj instanceof String && ((String) obj).startsWith("object:")))
				return obj;
			final String id = (String) obj;
			if (!objectService.contains(id)) {
				throw new JsonMappingException(p, "Object does not exist");
			}
			final ObjectInfo info = objectService.find(id);
			info.updateLastUsed();
			return info.getObject();
		}
	};

	final JsonSerializer<Object> objToIdSerializer =
		new JsonSerializer<Object>()
	{

			@Override
			public void serialize(Object value, JsonGenerator gen,
				SerializerProvider serializers) throws IOException,
				JsonProcessingException
		{
				gen.writeString(objectService.register(value, "DefaultJsonService"));
			}

		};

	final SimpleModule objToIdModule = new SimpleModule();
	objToIdModule.setSerializerModifier(new BeanSerializerModifier() {

		@Override
		public JsonSerializer<?> modifySerializer(SerializationConfig config,
			BeanDescription beanDesc, JsonSerializer<?> serializer)
		{
			if (Mixins.support(beanDesc.getBeanClass())) return serializer;
			// If the serialized class is unknown (i.e. serialized using the general
			// BeanSerializer) or should not be serialized (i.e. complicated class
			// implemented interfaces such as Iterable), would be serialized as an
			// ID.
			if (serializer instanceof BeanSerializer) return objToIdSerializer;
			if (notSerialized(beanDesc.getBeanClass())) return objToIdSerializer;
			return serializer;

		}
	});
	objToIdMapper = new ObjectMapper();
	objToIdMapper.registerModule(objToIdModule);

	// register Jackson MixIns to obtain better json output format for some
	// specific types
	Mixins.registerMixIns(objToIdMapper);
}