4

Is it possible to deserialize JSON using Jackson into one of two types based on the content of the JSON?

For example, I have the following Java (technically Groovy, but that's not important) interfaces and classes:

interface Id {
    Thing toThing() 
}

class NaturalId implements Id {

    final String packageId

    final String thingId

    Thing toThing() {
        new PackageIdentifiedThing(packageId, thingId)
    }         
}

class AlternateId implements Id {

    final String key

    Thing toThing() {
        new AlternatelyIdentifiedThing(key)
    }
}

The JSON I will receive will look like either of the following:

This JSON should map to NaturalId {"packageId": "SomePackage", "thingId": "SomeEntity"}

This JSON should map to AlternateId {"key": "SomeUniqueKey"}

Does anyone know how I can accomplish this with Jackson 2.x WITHOUT including type id's?

3 Answers 3

7

Are these the only two classes that implement Id? If so, you could write an IdDeserializer class and put @JsonDeserialize(using = IdDeserializer.class) on the Id interface, and the deserializer would look at the JSON and determine which object to deserialize into.

EDIT: The JsonParser is streaming so it should look something like this:

public Id deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    ObjectNode node = jp.readValueAsTree();
    Class<? extends Id> concreteType = determineConcreteType(node); //Implement 
    return jp.getCodec().treeToValue(node, concreteType);
}
5
  • At the moment; only these two. There may be a third in the near future, but I do not anticipate an explosion of Id types. I am going to look into the custom Deserializer approach since it is probably the most valid. Apr 5, 2013 at 14:59
  • I would suggest that your deserializer should only figure figure out which implementation of Id to deserialize into, and then delegate to Jackson for deserializing into that concrete type. This will make it so that any Jackson annotations on the concrete types are still respected. (make sure to put @JsonDeserialize(using = None.class) on the concrete types to avoid infinite deserialization loops)
    – HiJon89
    Apr 5, 2013 at 19:12
  • That's a good idea HiJon, but I seem to be running into an issue doing that; I get the following error: "No content to map due to end-of-input" when I call mapper.readValue(jsonParser, ConcreteType) where jsonParser is the one that was passed into the deserialize method and ConcreteType is the type I determined to be correct. Any ideas? I have a feeling it has something to do with the streaming nature of the API. Apr 5, 2013 at 22:47
  • 1
    Yes, the JsonParser is streaming, you need to keep a reference to the object you read and convert it to the concrete type once it's been determined, I've added a code sample for this.
    – HiJon89
    Apr 6, 2013 at 15:42
  • Got it! Thanks. I'm gonna switch this to the accepted answer. Apr 6, 2013 at 19:06
0

Annotate your methods with @JsonIgnore

@JsonIgnore
Thing toThing() {
    new PackageIdentifiedThing(packageId, thingId)
}  
0

With Jackson2, you can easily marshall to different classes using generics:

private <T> T json2Object(String jsonString, String type, Class<T> clazz) {
    JsonNode jsonObjectNode = getChildNode(jsonString, type);       
    T typeObject = null;
    try {
        typeObject = jacksonMapper.treeToValue(jsonObjectNode, clazz);
    } catch (JsonProcessingException jsonProcessingException) {
        LOGGER.severe(jsonProcessingException);
    }
    return typeObject;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.