ProtoDeserializers.java

  1. package net.morimekta.proto.jackson.adapter;

  2. import com.fasterxml.jackson.databind.BeanDescription;
  3. import com.fasterxml.jackson.databind.DeserializationConfig;
  4. import com.fasterxml.jackson.databind.JavaType;
  5. import com.fasterxml.jackson.databind.JsonDeserializer;
  6. import com.fasterxml.jackson.databind.JsonMappingException;
  7. import com.fasterxml.jackson.databind.KeyDeserializer;
  8. import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
  9. import com.fasterxml.jackson.databind.module.SimpleDeserializers;
  10. import com.fasterxml.jackson.databind.type.MapType;
  11. import com.google.protobuf.Message;
  12. import net.morimekta.proto.ProtoEnum;

  13. import static net.morimekta.proto.ProtoEnum.getEnumDescriptorUnchecked;
  14. import static net.morimekta.proto.ProtoMessage.getMessageDescriptor;

  15. /**
  16.  * Serializer handler for complex proto types.
  17.  */
  18. public class ProtoDeserializers
  19.         extends SimpleDeserializers {
  20.     @Override
  21.     public JsonDeserializer<?> findMapDeserializer(MapType type,
  22.                                                    DeserializationConfig config,
  23.                                                    BeanDescription beanDesc,
  24.                                                    KeyDeserializer keyDeserializer,
  25.                                                    TypeDeserializer elementTypeDeserializer,
  26.                                                    JsonDeserializer<?> elementDeserializer)
  27.             throws JsonMappingException {
  28.         if (ProtoEnum.isProtoEnumClass(type.getKeyType().getRawClass())) {
  29.             return new ProtoMapDeserializer<>(
  30.                     type.getRawClass(),
  31.                     createEnumKeyDeserializer(type.getKeyType().getRawClass()),
  32.                     type.getContentType());
  33.         }
  34.         return super.findMapDeserializer(type,
  35.                                          config,
  36.                                          beanDesc,
  37.                                          keyDeserializer,
  38.                                          elementTypeDeserializer,
  39.                                          elementDeserializer);
  40.     }

  41.     @Override
  42.     public JsonDeserializer<?> findEnumDeserializer(Class<?> type,
  43.                                                     DeserializationConfig config,
  44.                                                     BeanDescription beanDesc) throws JsonMappingException {
  45.         if (ProtoEnum.isProtoEnumClass(type)) {
  46.             try {
  47.                 return createEnumDeserializer(type);
  48.             } catch (IllegalArgumentException e) {
  49.                 throw new JsonMappingException(null, e.getMessage(), e);
  50.             }
  51.         }
  52.         return super.findEnumDeserializer(type, config, beanDesc);
  53.     }

  54.     @Override
  55.     public JsonDeserializer<?> findBeanDeserializer(JavaType type,
  56.                                                     DeserializationConfig config,
  57.                                                     BeanDescription beanDesc) throws JsonMappingException {
  58.         if (Message.class.isAssignableFrom(type.getRawClass())) {
  59.             try {
  60.                 return this.createMessageDeserializer(type.getRawClass(), config);
  61.             } catch (IllegalArgumentException e) {
  62.                 throw new JsonMappingException(null, e.getMessage(), e);
  63.             }
  64.         }
  65.         return super.findBeanDeserializer(type, config, beanDesc);
  66.     }

  67.     private JsonDeserializer<?> createMessageDeserializer(Class<?> type, DeserializationConfig config) {
  68.         return new ProtoMessageDeserializer<>(getMessageDescriptor(type), config);
  69.     }

  70.     @SuppressWarnings("rawtypes")
  71.     private JsonDeserializer<?> createEnumDeserializer(Class<?> type) {
  72.         return new ProtoEnumDeserializer(getEnumDescriptorUnchecked(type));
  73.     }

  74.     private KeyDeserializer createEnumKeyDeserializer(Class<?> type) {
  75.         return new ProtoEnumKeyDeserializer(getEnumDescriptorUnchecked(type));
  76.     }
  77. }