com.fasterxml.jackson.databind.ser.BeanSerializer Java Examples

The following examples show how to use com.fasterxml.jackson.databind.ser.BeanSerializer. 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: Jackson2Parser.java    From typescript-generator with MIT License 6 votes vote down vote up
private BeanHelper getBeanHelper(Class<?> beanClass) {
    if (beanClass == null) {
        return null;
    }
    if (beanClass == Enum.class) {
        return null;
    }
    try {
        final DefaultSerializerProvider.Impl serializerProvider1 = (DefaultSerializerProvider.Impl) objectMapper.getSerializerProvider();
        final DefaultSerializerProvider.Impl serializerProvider2 = serializerProvider1.createInstance(objectMapper.getSerializationConfig(), objectMapper.getSerializerFactory());
        final JavaType simpleType = objectMapper.constructType(beanClass);
        final JsonSerializer<?> jsonSerializer = BeanSerializerFactory.instance.createSerializer(serializerProvider2, simpleType);
        if (jsonSerializer == null) {
            return null;
        }
        if (jsonSerializer instanceof BeanSerializer) {
            return new BeanHelper((BeanSerializer) jsonSerializer);
        } else {
            return null;
        }
    } catch (JsonMappingException e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: FlatteningSerializer.java    From botbuilder-java with MIT License 5 votes vote down vote up
/**
 * Gets a module wrapping this serializer as an adapter for the Jackson
 * ObjectMapper.
 *
 * @param mapper the object mapper for default serializations
 * @return a simple module to be plugged onto Jackson ObjectMapper.
 */
public static SimpleModule getModule(final ObjectMapper mapper) {
    SimpleModule module = new SimpleModule();
    module.setSerializerModifier(new BeanSerializerModifier() {
        @Override
        public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
            if (BeanSerializer.class.isAssignableFrom(serializer.getClass())) {
                return new FlatteningSerializer(beanDesc.getBeanClass(), serializer, mapper);
            }
            return serializer;
        }
    });
    return module;
}
 
Example #3
Source File: FlatteningSerializer.java    From autorest-clientruntime-for-java with MIT License 5 votes vote down vote up
/**
 * Gets a module wrapping this serializer as an adapter for the Jackson
 * ObjectMapper.
 *
 * @param mapper the object mapper for default serializations
 * @return a simple module to be plugged onto Jackson ObjectMapper.
 */
public static SimpleModule getModule(final ObjectMapper mapper) {
    SimpleModule module = new SimpleModule();
    module.setSerializerModifier(new BeanSerializerModifier() {
        @Override
        public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
            if (BeanSerializer.class.isAssignableFrom(serializer.getClass())) {
                return new FlatteningSerializer(beanDesc.getBeanClass(), serializer, mapper);
            }
            return serializer;
        }
    });
    return module;
}
 
Example #4
Source File: JsonUtils.java    From warp10-platform with Apache License 2.0 5 votes vote down vote up
@Override
public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
  if (serializer instanceof UnknownSerializer || serializer instanceof BeanSerializer) {
    return customEncodersSerializer;
  } else {
    return serializer;
  }
}
 
Example #5
Source File: Jackson2Parser.java    From typescript-generator with MIT License 4 votes vote down vote up
public BeanHelper(BeanSerializer src) {
    super(src);
}
 
Example #6
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);
}