lowentry.ue4.libs.jackson.databind.ser.std.StdSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lowentryue4 Show documentation
Show all versions of lowentryue4 Show documentation
A Java library for the Low Entry UE4 plugins.
package lowentry.ue4.libs.jackson.databind.ser.std;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Map;
import lowentry.ue4.libs.jackson.annotation.JsonFormat;
import lowentry.ue4.libs.jackson.annotation.JsonInclude;
import lowentry.ue4.libs.jackson.core.*;
import lowentry.ue4.libs.jackson.core.JsonParser.NumberType;
import lowentry.ue4.libs.jackson.databind.*;
import lowentry.ue4.libs.jackson.databind.annotation.JacksonStdImpl;
import lowentry.ue4.libs.jackson.databind.introspect.AnnotatedMember;
import lowentry.ue4.libs.jackson.databind.jsonFormatVisitors.*;
import lowentry.ue4.libs.jackson.databind.jsonschema.SchemaAware;
import lowentry.ue4.libs.jackson.databind.node.JsonNodeFactory;
import lowentry.ue4.libs.jackson.databind.node.ObjectNode;
import lowentry.ue4.libs.jackson.databind.ser.FilterProvider;
import lowentry.ue4.libs.jackson.databind.ser.PropertyFilter;
import lowentry.ue4.libs.jackson.databind.util.ClassUtil;
import lowentry.ue4.libs.jackson.databind.util.Converter;
/**
* Base class used by all standard serializers, and can also
* be used for custom serializers (in fact, this is the recommended
* base class to use).
* Provides convenience methods for implementing {@link SchemaAware}
*/
@SuppressWarnings("all")
public abstract class StdSerializer
extends JsonSerializer
implements JsonFormatVisitable, SchemaAware, java.io.Serializable
{
private static final long serialVersionUID = 1L;
/**
* Key used for storing a lock object to prevent infinite recursion when
* constructing converting serializers.
*
* @since 2.9
*/
private final static Object KEY_CONTENT_CONVERTER_LOCK = new Object();
/**
* Nominal type supported, usually declared type of
* property for which serializer is used.
*/
protected final Class _handledType;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
protected StdSerializer(Class t) {
_handledType = t;
}
@SuppressWarnings("unchecked")
protected StdSerializer(JavaType type) {
_handledType = (Class) type.getRawClass();
}
/**
* Alternate constructor that is (alas!) needed to work
* around kinks of generic type handling
*/
@SuppressWarnings("unchecked")
protected StdSerializer(Class> t, boolean dummy) {
_handledType = (Class) t;
}
/**
* @since 2.6
*/
@SuppressWarnings("unchecked")
protected StdSerializer(StdSerializer> src) {
_handledType = (Class) src._handledType;
}
/*
/**********************************************************
/* Accessors
/**********************************************************
*/
@Override
public Class handledType() { return _handledType; }
/*
/**********************************************************
/* Serialization
/**********************************************************
*/
@Override
public abstract void serialize(T value, JsonGenerator gen, SerializerProvider provider)
throws IOException;
/*
/**********************************************************
/* Type introspection API, partial/default implementation
/**********************************************************
*/
/**
* Default implementation specifies no format. This behavior is usually
* overriden by custom serializers.
*/
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
throws JsonMappingException
{
visitor.expectAnyFormat(typeHint);
}
/**
* Default implementation simply claims type is "string"; usually
* overriden by custom serializers.
*/
@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException
{
return createSchemaNode("string");
}
/**
* Default implementation simply claims type is "string"; usually
* overriden by custom serializers.
*/
@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint, boolean isOptional)
throws JsonMappingException
{
ObjectNode schema = (ObjectNode) getSchema(provider, typeHint);
if (!isOptional) {
schema.put("required", !isOptional);
}
return schema;
}
/*
/**********************************************************
/* Helper methods for JSON Schema generation
/**********************************************************
*/
protected ObjectNode createSchemaNode(String type)
{
ObjectNode schema = JsonNodeFactory.instance.objectNode();
schema.put("type", type);
return schema;
}
protected ObjectNode createSchemaNode(String type, boolean isOptional)
{
ObjectNode schema = createSchemaNode(type);
if (!isOptional) {
schema.put("required", !isOptional);
}
return schema;
}
/**
* Helper method that calls necessary visit method(s) to indicate that the
* underlying JSON type is JSON String.
*
* @since 2.7
*/
protected void visitStringFormat(JsonFormatVisitorWrapper visitor, JavaType typeHint)
throws JsonMappingException {
/*JsonStringFormatVisitor v2 =*/ visitor.expectStringFormat(typeHint);
}
/**
* Helper method that calls necessary visit method(s) to indicate that the
* underlying JSON type is JSON String, but that there is a more refined
* logical type
*
* @since 2.7
*/
protected void visitStringFormat(JsonFormatVisitorWrapper visitor, JavaType typeHint,
JsonValueFormat format)
throws JsonMappingException
{
JsonStringFormatVisitor v2 = visitor.expectStringFormat(typeHint);
if (v2 != null) {
v2.format(format);
}
}
/**
* Helper method that calls necessary visit method(s) to indicate that the
* underlying JSON type is JSON Integer number.
*
* @since 2.7
*/
protected void visitIntFormat(JsonFormatVisitorWrapper visitor, JavaType typeHint,
NumberType numberType)
throws JsonMappingException
{
JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
if (_neitherNull(v2, numberType)) {
v2.numberType(numberType);
}
}
/**
* Helper method that calls necessary visit method(s) to indicate that the
* underlying JSON type is JSON Integer number, but that there is also a further
* format restriction involved.
*
* @since 2.7
*/
protected void visitIntFormat(JsonFormatVisitorWrapper visitor, JavaType typeHint,
NumberType numberType, JsonValueFormat format)
throws JsonMappingException
{
JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
if (v2 != null) {
if (numberType != null) {
v2.numberType(numberType);
}
if (format != null) {
v2.format(format);
}
}
}
/**
* Helper method that calls necessary visit method(s) to indicate that the
* underlying JSON type is a floating-point JSON number.
*
* @since 2.7
*/
protected void visitFloatFormat(JsonFormatVisitorWrapper visitor, JavaType typeHint,
NumberType numberType)
throws JsonMappingException
{
JsonNumberFormatVisitor v2 = visitor.expectNumberFormat(typeHint);
if (v2 != null) {
v2.numberType(numberType);
}
}
/**
* @since 2.7
*/
protected void visitArrayFormat(JsonFormatVisitorWrapper visitor, JavaType typeHint,
JsonSerializer> itemSerializer, JavaType itemType)
throws JsonMappingException
{
JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
if (_neitherNull(v2, itemSerializer)) {
v2.itemsFormat(itemSerializer, itemType);
}
}
/**
* @since 2.7
*/
protected void visitArrayFormat(JsonFormatVisitorWrapper visitor, JavaType typeHint,
JsonFormatTypes itemType)
throws JsonMappingException
{
JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
if (v2 != null) {
v2.itemsFormat(itemType);
}
}
/*
/**********************************************************
/* Helper methods for exception handling
/**********************************************************
*/
/**
* Method that will modify caught exception (passed in as argument)
* as necessary to include reference information, and to ensure it
* is a subtype of {@link IOException}, or an unchecked exception.
*
* Rules for wrapping and unwrapping are bit complicated; essentially:
*
* - Errors are to be passed as is (if uncovered via unwrapping)
*
- "Plain" IOExceptions (ones that are not of type
* {@link JsonMappingException} are to be passed as is
*
*/
public void wrapAndThrow(SerializerProvider provider,
Throwable t, Object bean, String fieldName)
throws IOException
{
/* 05-Mar-2009, tatu: But one nasty edge is when we get
* StackOverflow: usually due to infinite loop. But that
* usually gets hidden within an InvocationTargetException...
*/
while (t instanceof InvocationTargetException && t.getCause() != null) {
t = t.getCause();
}
// Errors and "plain" to be passed as is
ClassUtil.throwIfError(t);
// Ditto for IOExceptions... except for mapping exceptions!
boolean wrap = (provider == null) || provider.isEnabled(SerializationFeature.WRAP_EXCEPTIONS);
if (t instanceof IOException) {
if (!wrap || !(t instanceof JsonMappingException)) {
throw (IOException) t;
}
} else if (!wrap) {
ClassUtil.throwIfRTE(t);
}
// Need to add reference information
throw JsonMappingException.wrapWithPath(t, bean, fieldName);
}
public void wrapAndThrow(SerializerProvider provider,
Throwable t, Object bean, int index)
throws IOException
{
while (t instanceof InvocationTargetException && t.getCause() != null) {
t = t.getCause();
}
// Errors are to be passed as is
ClassUtil.throwIfError(t);
// Ditto for IOExceptions... except for mapping exceptions!
boolean wrap = (provider == null) || provider.isEnabled(SerializationFeature.WRAP_EXCEPTIONS);
if (t instanceof IOException) {
if (!wrap || !(t instanceof JsonMappingException)) {
throw (IOException) t;
}
} else if (!wrap) {
ClassUtil.throwIfRTE(t);
}
// Need to add reference information
throw JsonMappingException.wrapWithPath(t, bean, index);
}
/*
/**********************************************************
/* Helper methods, accessing annotation-based configuration
/**********************************************************
*/
/**
* Helper method that can be used to see if specified property has annotation
* indicating that a converter is to be used for contained values (contents
* of structured types; array/List/Map values)
*
* @param existingSerializer (optional) configured content
* serializer if one already exists.
*
* @since 2.9
*/
protected JsonSerializer> findContextualConvertingSerializer(SerializerProvider provider,
BeanProperty property, JsonSerializer> existingSerializer)
throws JsonMappingException
{
// 08-Dec-2016, tatu: to fix [databind#357], need to prevent recursive calls for
// same property
@SuppressWarnings("unchecked")
Map