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

com.amazonaws.athena.connector.lambda.serde.BaseSerializer Maven / Gradle / Ivy

/*-
 * #%L
 * Amazon Athena Query Federation SDK
 * %%
 * Copyright (C) 2019 - 2020 Amazon Web Services
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.amazonaws.athena.connector.lambda.serde;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

public abstract class BaseSerializer extends StdSerializer implements VersionedSerDe.Serializer
{
    static final String TYPE_FIELD = "@type";

    protected BaseSerializer(Class clazz)
    {
        super(clazz);
    }

    @Override
    public void serialize(T value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException
    {
        if (value != null) {
            jgen.writeStartObject();
            doSerialize(value, jgen, provider);
            jgen.writeEndObject();
        }
        else {
            jgen.writeNull();
        }
    }

    @Override
    public void serializeWithType(T value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer)
            throws IOException
    {
        // TODO leverage TypeSerializer if it simplifies things
        serialize(value, gen, serializers);
    }

    public abstract void doSerialize(T value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException;

    /**
     * Helper used to help serialize a list of strings.
     *
     * @param jgen The json generator to use.
     * @param fieldName The name to associated to the resulting json array.
     * @param values The values to populate the array with.
     * @throws IOException If an error occurs while writing to the generator.
     */
    protected void writeStringArray(JsonGenerator jgen, String fieldName, Collection values)
            throws IOException
    {
        jgen.writeArrayFieldStart(fieldName);

        for (String nextElement : values) {
            jgen.writeString(nextElement);
        }

        jgen.writeEndArray();
    }

    /**
     * Helper used to help serialize a list of strings.
     *
     * @param jgen The json generator to use.
     * @param fieldName The name to associated to the resulting json array.
     * @param values The values to populate the array with.
     * @throws IOException If an error occurs while writing to the generator.
     */
    protected void writeStringMap(JsonGenerator jgen, String fieldName, Map values) throws IOException
    {
        jgen.writeObjectFieldStart(fieldName);

        for (Map.Entry entry : values.entrySet()) {
            jgen.writeStringField(entry.getKey(), entry.getValue());
        }

        jgen.writeEndObject();
    }

    /**
     * Helper used to write the type of the object (usually the class name).
     *
     * @param jgen The JSON generator.
     * @throws IOException If there is an error generating the JSON.
     */
    void writeType(JsonGenerator jgen, Class type)
            throws IOException
    {
        jgen.writeStringField(TYPE_FIELD, type.getSimpleName());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy