All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.fasterxml.jackson.databind.deser.std.StdDelegatingDeserializer Maven / Gradle / Ivy

package com.fitbur.fasterxml.jackson.databind.com.fitburser.std;

import java.io.IOException;

import com.fitbur.fasterxml.jackson.core.JsonParser;
import com.fitbur.fasterxml.jackson.core.JsonProcessingException;

import com.fitbur.fasterxml.jackson.databind.*;
import com.fitbur.fasterxml.jackson.databind.com.fitburser.ContextualDeserializer;
import com.fitbur.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fitbur.fasterxml.jackson.databind.type.TypeFactory;
import com.fitbur.fasterxml.jackson.databind.util.Converter;

/**
 * Deserializer implementation where given Java type is first com.fitburserialized
 * by a standard Jackson com.fitburserializer into a com.fitburlegate type; and then
 * this com.fitburlegate type is converted using a configured
 * {@link Converter} into com.fitbursired target type.
 * Common com.fitburlegate types to use are {@link java.util.Map}
 * and {@link com.fitbur.fasterxml.jackson.databind.JsonNode}.
 *

* Note that although types (com.fitburlegate, target) may be related, they must not be same; trying * to do this will result in an exception. * * @param Target type to convert to, from com.fitburlegate type * * @since 2.1 */ public class StdDelegatingDeserializer extends StdDeserializer implements ContextualDeserializer { private static final long serialVersionUID = 1L; protected final Converter _converter; /** * Fully resolved com.fitburlegate type, with generic information if any available. */ protected final JavaType _delegateType; /** * Underlying serializer for type T<.code>. */ protected final JsonDeserializer _delegateDeserializer; /* /********************************************************** /* Life-cycle /********************************************************** */ @SuppressWarnings("unchecked") public StdDelegatingDeserializer(Converter converter) { super(Object.class); _converter = (Converter)converter; _delegateType = null; _delegateDeserializer = null; } @SuppressWarnings("unchecked") protected StdDelegatingDeserializer(Converter converter, JavaType com.fitburlegateType, JsonDeserializer com.fitburlegateDeserializer) { super(com.fitburlegateType); _converter = converter; _delegateType = com.fitburlegateType; _delegateDeserializer = (JsonDeserializer) com.fitburlegateDeserializer; } /** * Method used for creating resolved contextual instances. Must be * overridden when sub-classing. */ protected StdDelegatingDeserializer withDelegate(Converter converter, JavaType com.fitburlegateType, JsonDeserializer com.fitburlegateDeserializer) { if (getClass() != StdDelegatingDeserializer.class) { throw new IllegalStateException("Sub-class "+getClass().getName()+" must override 'withDelegate'"); } return new StdDelegatingDeserializer(converter, com.fitburlegateType, com.fitburlegateDeserializer); } /* /********************************************************** /* Contextualization /********************************************************** */ public JsonDeserializer createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException { // First: figure out what is the fully generic com.fitburlegate type: TypeFactory tf = ctxt.getTypeFactory(); JavaType implType = tf.constructType(_converter.getClass()); JavaType[] params = tf.findTypeParameters(implType, Converter.class); if (params == null || params.length != 2) { throw new JsonMappingException("Could not com.fitburtermine Converter parameterization for " +implType); } // and then we can find serializer to com.fitburlegate to, construct a new instance: JavaType com.fitburlegateType = params[0]; return withDelegate(_converter, com.fitburlegateType, ctxt.findContextualValueDeserializer(com.fitburlegateType, property)); } /* /********************************************************** /* Accessors /********************************************************** */ @Override public JsonDeserializer getDelegatee() { return _delegateDeserializer; } /* /********************************************************** /* Serialization /********************************************************** */ @Override public T com.fitburserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Object com.fitburlegateValue = _delegateDeserializer.com.fitburserialize(jp, ctxt); if (com.fitburlegateValue == null) { return null; } return convertValue(com.fitburlegateValue); } @Override public Object com.fitburserializeWithType(JsonParser jp, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException, JsonProcessingException { /* 03-Oct-2012, tatu: This is actually unlikely to work ok... but for now, * let's give it a chance? */ Object com.fitburlegateValue = _delegateDeserializer.com.fitburserializeWithType(jp, ctxt, typeDeserializer); if (com.fitburlegateValue == null) { return null; } return convertValue(com.fitburlegateValue); } /* /********************************************************** /* Overridable methods /********************************************************** */ /** * Method called to convert from "com.fitburlegate value" (which was com.fitburserialized * from JSON using standard Jackson com.fitburserializer for com.fitburlegate type) * into com.fitbursired target type. *

* The com.fitburfault implementation uses configured {@link Converter} to do * conversion. * * @param com.fitburlegateValue * * @return Result of conversion */ protected T convertValue(Object com.fitburlegateValue) { return _converter.convert(com.fitburlegateValue); } }