Package com.facebook.presto.hive.shaded.org.codehaus.jackson.map.ser.std

Source Code of com.facebook.presto.hive.shaded.org.codehaus.jackson.map.ser.std.SerializableWithTypeSerializer

package com.facebook.presto.hive.shaded.org.codehaus.jackson.map.ser.std;

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

import com.facebook.presto.hive.shaded.org.codehaus.jackson.JsonGenerationException;
import com.facebook.presto.hive.shaded.org.codehaus.jackson.JsonGenerator;
import com.facebook.presto.hive.shaded.org.codehaus.jackson.JsonNode;
import com.facebook.presto.hive.shaded.org.codehaus.jackson.map.JsonMappingException;
import com.facebook.presto.hive.shaded.org.codehaus.jackson.map.JsonSerializableWithType;
import com.facebook.presto.hive.shaded.org.codehaus.jackson.map.ObjectMapper;
import com.facebook.presto.hive.shaded.org.codehaus.jackson.map.SerializerProvider;
import com.facebook.presto.hive.shaded.org.codehaus.jackson.map.TypeSerializer;
import com.facebook.presto.hive.shaded.org.codehaus.jackson.map.annotate.JacksonStdImpl;
import com.facebook.presto.hive.shaded.org.codehaus.jackson.map.type.TypeFactory;
import com.facebook.presto.hive.shaded.org.codehaus.jackson.node.ObjectNode;
import com.facebook.presto.hive.shaded.org.codehaus.jackson.schema.JsonSerializableSchema;

/**
* Generic handler for types that implement {@link JsonSerializableWithType}.
*<p>
* Note: given that this is used for anything that implements
* interface, can not be checked for direct class equivalence.
*/
@JacksonStdImpl
public class SerializableWithTypeSerializer
    extends SerializerBase<JsonSerializableWithType>
{
    public final static SerializableWithTypeSerializer instance = new SerializableWithTypeSerializer();

    protected SerializableWithTypeSerializer() { super(JsonSerializableWithType.class); }

    @SuppressWarnings("deprecation") // why is this needed?
    @Override
    public void serialize(JsonSerializableWithType value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonGenerationException
    {
        value.serialize(jgen, provider);
    }

    @Override
    public final void serializeWithType(JsonSerializableWithType value, JsonGenerator jgen, SerializerProvider provider,
            TypeSerializer typeSer)
        throws IOException, JsonGenerationException
    {
        value.serializeWithType(jgen, provider, typeSer);
    }
   
    // copied verbatim from "JsonSerializableSerializer"
    @Override
    public JsonNode getSchema(SerializerProvider provider, Type typeHint)
        throws JsonMappingException
    {
        ObjectNode objectNode = createObjectNode();
        String schemaType = "any";
        String objectProperties = null;
        String itemDefinition = null;
        if (typeHint != null) {
            Class<?> rawClass = TypeFactory.rawClass(typeHint);
            if (rawClass.isAnnotationPresent(JsonSerializableSchema.class)) {
                JsonSerializableSchema schemaInfo = rawClass.getAnnotation(JsonSerializableSchema.class);
                schemaType = schemaInfo.schemaType();
                if (!"##irrelevant".equals(schemaInfo.schemaObjectPropertiesDefinition())) {
                    objectProperties = schemaInfo.schemaObjectPropertiesDefinition();
                }
                if (!"##irrelevant".equals(schemaInfo.schemaItemDefinition())) {
                    itemDefinition = schemaInfo.schemaItemDefinition();
                }
            }
        }
        objectNode.put("type", schemaType);
        if (objectProperties != null) {
            try {
                objectNode.put("properties", new ObjectMapper().readValue(objectProperties, JsonNode.class));
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }
        if (itemDefinition != null) {
            try {
                objectNode.put("items", new ObjectMapper().readValue(itemDefinition, JsonNode.class));
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }
        // always optional, no need to specify:
        //objectNode.put("required", false);
        return objectNode;
    }
}
TOP

Related Classes of com.facebook.presto.hive.shaded.org.codehaus.jackson.map.ser.std.SerializableWithTypeSerializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.