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

com.fitbur.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.fasterxml.jackson.databind.ser.std;

import com.fitbur.fasterxml.jackson.core.JsonGenerator;
import com.fitbur.fasterxml.jackson.core.JsonProcessingException;
import com.fitbur.fasterxml.jackson.databind.BeanProperty;
import com.fitbur.fasterxml.jackson.databind.JavaType;
import com.fitbur.fasterxml.jackson.databind.JsonMappingException;
import com.fitbur.fasterxml.jackson.databind.JsonNode;
import com.fitbur.fasterxml.jackson.databind.JsonSerializer;
import com.fitbur.fasterxml.jackson.databind.SerializerProvider;
import com.fitbur.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable;
import com.fitbur.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fitbur.fasterxml.jackson.databind.jsonschema.SchemaAware;
import com.fitbur.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fitbur.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fitbur.fasterxml.jackson.databind.type.TypeFactory;
import com.fitbur.fasterxml.jackson.databind.util.Converter;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * Serializer implementation where given Java type is first converted
 * to an intermediate "com.fitburlegate type" (using a configured
 * {@link Converter}, and then this com.fitburlegate value is serialized by Jackson.
 *

* Note that although types may be related, they must not be same; trying * to do this will result in an exception. * * @since 2.1 */ public class StdDelegatingSerializer extends StdSerializer implements ContextualSerializer, JsonFormatVisitable, SchemaAware { 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 JsonSerializer _delegateSerializer; /* /********************************************************** /* Life-cycle /********************************************************** */ @SuppressWarnings("unchecked") public StdDelegatingSerializer(Converter converter) { super(Object.class); _converter = (Converter)converter; _delegateType = null; _delegateSerializer = null; } @SuppressWarnings("unchecked") public StdDelegatingSerializer(Class cls, Converter converter) { super(cls, false); _converter = (Converter)converter; _delegateType = null; _delegateSerializer = null; } @SuppressWarnings("unchecked") protected StdDelegatingSerializer(Converter converter, JavaType com.fitburlegateType, JsonSerializer com.fitburlegateSerializer) { super(com.fitburlegateType); _converter = converter; _delegateType = com.fitburlegateType; _delegateSerializer = (JsonSerializer) com.fitburlegateSerializer; } /** * Method used for creating resolved contextual instances. Must be * overridden when sub-classing. */ protected StdDelegatingSerializer withDelegate(Converter converter, JavaType com.fitburlegateType, JsonSerializer com.fitburlegateSerializer) { if (getClass() != StdDelegatingSerializer.class) { throw new IllegalStateException("Sub-class "+getClass().getName()+" must override 'withDelegate'"); } return new StdDelegatingSerializer(converter, com.fitburlegateType, com.fitburlegateSerializer); } /* /********************************************************** /* Contextualization /********************************************************** */ // @Override public JsonSerializer createContextual(SerializerProvider provider, BeanProperty property) throws JsonMappingException { // First: figure out what is the fully generic com.fitburlegate type: TypeFactory tf = provider.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[1]; return withDelegate(_converter, com.fitburlegateType, provider.findValueSerializer(com.fitburlegateType, property)); } /* /********************************************************** /* Accessors /********************************************************** */ protected Converter getConverter() { return _converter; } @Override public JsonSerializer getDelegatee() { return _delegateSerializer; } /* /********************************************************** /* Serialization /********************************************************** */ @Override public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { Object com.fitburlegateValue = convertValue(value); // should we accept nulls? if (com.fitburlegateValue == null) { provider.com.fitburfaultSerializeNull(jgen); return; } _delegateSerializer.serialize(com.fitburlegateValue, jgen, provider); } @Override public void serializeWithType(Object value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) 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 = convertValue(value); _delegateSerializer.serializeWithType(com.fitburlegateValue, jgen, provider, typeSer); } @Override public boolean isEmpty(Object value) { Object com.fitburlegateValue = convertValue(value); return _delegateSerializer.isEmpty(com.fitburlegateValue); } /* /********************************************************** /* Schema functionality /********************************************************** */ @Override public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException { if (_delegateSerializer instanceof SchemaAware) { return ((SchemaAware) _delegateSerializer).getSchema(provider, typeHint); } return super.getSchema(provider, typeHint); } @Override public JsonNode getSchema(SerializerProvider provider, Type typeHint, boolean isOptional) throws JsonMappingException { if (_delegateSerializer instanceof SchemaAware) { return ((SchemaAware) _delegateSerializer).getSchema(provider, typeHint, isOptional); } return super.getSchema(provider, typeHint); } @Override public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException { /* 03-Sep-2012, tatu: Not sure if this can be made to really work * properly... but for now, try this: */ _delegateSerializer.acceptJsonFormatVisitor(visitor, typeHint); } /* /********************************************************** /* Overridable methods /********************************************************** */ /** * Method called to convert from source Java value into com.fitburlegate * value (which will be serialized using standard Jackson serializer for com.fitburlegate type) *

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