4

FOUND SOLUTION TO THE PROBLEM For the people who will be stuck like me!: in order to handle third party java or scala Objects for jackson deserialization, you can either use Mixins( but you need to reconfigure the jackson mapper or user Modules) OR you can simply create a class called MyClassDeserializer that extends JsonDeserializer and use the @JsonDeserialize(using = MyClassDeserializer.class) annotation.

exemple :

it's really simple and works like a charm! :)

public class User implements Identity{
       @JsonProperty("_id")
        private String id;
        @JsonDeserialize(using = OptionDeserializer.class)
        public Option<String> email;
    }



 public class OptionDeserializer extends JsonDeserializer<Option> {
        @Override
        public Option deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
            //code can be improved
ObjectCodec oc = jsonParser.getCodec();
            JsonNode node = oc.readTree(jsonParser);
            return  Option.apply(node.get("email").getText());
        }
    }

hello guys i'm having some bad time trying to deserialize this with jackson using Jongo

public class User implements Identity{
   @JsonProperty("_id")
    private String id;
    public Option<String> email;
}

Option is an abstract type, I'm really new to this, is there a way to actually tell jackson how to translate it?

this User object is correctly saved to mongo, but cannot be read :/ i've spent so many hours trying to understand you guys are my last resort! thanks (keeps looking)

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of scala.Option, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information at [Source: de.undercouch.bson4jackson.io.LittleEndianInputStream@6f255853; pos: 237] (through reference chain: models.User["email"])

dbc
  • 104,963
  • 20
  • 228
  • 340
popo joe
  • 910
  • 1
  • 9
  • 24

1 Answers1

1

posting popo joe's answer as an answer:

FOUND SOLUTION TO THE PROBLEM For the people who will be stuck like me!: in order to handle third party java or scala Objects for jackson deserialization, you can either use Mixins( but you need to reconfigure the jackson mapper or user Modules) OR you can simply create a class called MyClassDeserializer that extends JsonDeserializer and use the @JsonDeserialize(using = MyClassDeserializer.class) annotation.

example :

it's really simple and works like a charm! :)

public class User implements Identity{
     @JsonProperty("_id")
     private String id;
     @JsonDeserialize(using = OptionDeserializer.class)
     public Option<String> email;
 }

public class OptionDeserializer extends JsonDeserializer<Option> {
    @Override
    public Option deserialize(JsonParser jsonParser,
    DeserializationContext deserializationContext) throws IOException, 
    JsonProcessingException {`

        //code can be improved`

        ObjectCodec oc = jsonParser.getCodec();
        JsonNode node = oc.readTree(jsonParser);
        return  Option.apply(node.get("email").getText());
   }
}
Andrew Norman
  • 843
  • 9
  • 22