io.bitsensor.lib.jackson.protobuf.ProtobufDeserializerFactory Maven / Gradle / Ivy
package io.bitsensor.lib.jackson.protobuf;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.Deserializers;
import io.bitsensor.proto.shaded.com.google.protobuf.Message;
import io.bitsensor.proto.shaded.com.google.protobuf.util.JsonFormat;
import io.bitsensor.proto.shaded.com.google.protobuf.util.JsonFormat.Parser;
public class ProtobufDeserializerFactory extends Deserializers.Base {
private final Parser parser;
public ProtobufDeserializerFactory() {
this(JsonFormat.parser());
}
public ProtobufDeserializerFactory(Parser parser) {
this.parser = parser;
}
@Override
@SuppressWarnings("unchecked")
public JsonDeserializer> findBeanDeserializer(JavaType type, DeserializationConfig config, BeanDescription beanDesc)
throws JsonMappingException {
if (Message.class.isAssignableFrom(type.getRawClass())) {
return getDeserializer((Class extends Message>) type.getRawClass(), true);
} else if (Message.Builder.class.isAssignableFrom(type.getRawClass())) {
return getDeserializer((Class extends Message>) type.getRawClass().getDeclaringClass(), false);
} else {
return super.findBeanDeserializer(type, config, beanDesc);
}
}
@SuppressWarnings("unchecked")
private ProtobufDeserializer getDeserializer(Class messageType, boolean build)
throws JsonMappingException {
return new ProtobufDeserializer<>(messageType, build, parser);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy