FasterXML / jackson-future-ideas

Repository for SOLE PURPOSE of issue tracker and Wiki for NEW IDEAS. Please: NO BUG REPORTS.
18 stars 6 forks source link

Deferred deserialization: Inject DeserializationContext into @JsonCreator methods #16

Open henryptung opened 7 years ago

henryptung commented 7 years ago

I have a few use cases where JSON deserialization may be deferred, and the user given a chance to override, modify properties, merge with another bean, or otherwise defer deserialization until some later point in runtime. Right now, there doesn't seem to be a feasible way to do this other than to write a weighty JsonDeserializer implementation that detects the value type, constructs the content deserializer, etc.

Instead, it would be wonderful if an annotation allowed for injection of a SubDeserializer<T>, with nothing but:

independently callable at a later time. This allows something like:

public class JsonTemplate<T> {
    private final JsonNode json;
    private final SubDeserializer<T> deserializer;

    @JsonCreator
    public JsonTemplate(JsonNode json, SubDeserializer<T> deserializer) {
        this.json = json;
        this.deserializer = deserializer;
    }

    public T render(StrSubstitutor substitutor) throws JsonProcessingException {
        JsonNode substitutedJson = substitute(json, substitutor);
        try {
            return deserializer.deserialize(new TreeTraversingParser(substitutedJson));
        } catch (JsonProcessingException e) {
            throw e;
        } catch (IOException e) {
            throw new RuntimeException(e); // nope
        }
    }

    private static JsonNode substitute(JsonNode json, StrSubstitutor substitutor) {
        // ...
    }
}
cowtowncoder commented 7 years ago

Ability to inject DeserializationContext, in general, sounds like a reasonable feature, even if not trivial to implement. I don't know if other support for deferred deserialization is feasible.