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

org.embulk.config.DataSourceSerDe Maven / Gradle / Ivy

package org.embulk.config;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;

public class DataSourceSerDe {
    public static class SerDeModule extends SimpleModule {
        @SuppressWarnings("deprecation")  // https://github.com/embulk/embulk/issues/1304
        public SerDeModule(final ModelManager model) {
            // DataSourceImpl
            addSerializer(DataSourceImpl.class, new DataSourceSerializer(model));
            addDeserializer(DataSourceImpl.class, new DataSourceDeserializer(model));

            // ConfigSource
            addSerializer(ConfigSource.class, new DataSourceSerializer(model));
            addDeserializer(ConfigSource.class, new DataSourceDeserializer(model));

            // TaskSource
            addSerializer(TaskSource.class, new DataSourceSerializer(model));
            addDeserializer(TaskSource.class, new DataSourceDeserializer(model));

            // TaskReport
            addSerializer(TaskReport.class, new DataSourceSerializer(model));
            addDeserializer(TaskReport.class, new DataSourceDeserializer(model));

            // ConfigDiff
            addSerializer(ConfigDiff.class, new DataSourceSerializer(model));
            addDeserializer(ConfigDiff.class, new DataSourceDeserializer(model));
        }
    }

    // TODO T extends DataSource super DataSourceImpl
    private static class DataSourceDeserializer extends JsonDeserializer {
        @Deprecated  // https://github.com/embulk/embulk/issues/1304
        private final ModelManager model;

        private final ObjectMapper treeObjectMapper;

        @SuppressWarnings("deprecation")  // https://github.com/embulk/embulk/issues/1304
        DataSourceDeserializer(ModelManager model) {
            this.model = model;
            this.treeObjectMapper = new ObjectMapper();
        }

        @Override
        @SuppressWarnings("unchecked")
        public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
            JsonNode json = treeObjectMapper.readTree(jp);
            if (!json.isObject()) {
                throw new JsonMappingException("Expected object to deserialize DataSource", jp.getCurrentLocation());
            }
            return (T) new DataSourceImpl(model, (ObjectNode) json);
        }
    }

    private static class DataSourceSerializer extends JsonSerializer {
        @Deprecated  // https://github.com/embulk/embulk/issues/1304
        private final ModelManager model;

        @SuppressWarnings("deprecation")  // https://github.com/embulk/embulk/issues/1304
        DataSourceSerializer(final ModelManager model) {
            this.model = model;
        }

        @Override
        public void serialize(T value, JsonGenerator jgen, SerializerProvider provider)
                throws IOException {
            if (value == null) {
                throw new JsonGenerationException(new NullPointerException(
                        "DataSourceSerDe.DataSourceSerializer#serialize accepts only non-null value"));
            }
            final String valueJsonStringified = value.toJson();
            if (valueJsonStringified == null) {
                throw new JsonGenerationException(new NullPointerException(
                        "DataSourceSerDe.DataSourceSerializer#serialize accepts only valid DataSource"));
            }
            final JsonNode valueJsonNode = this.model.readObject(JsonNode.class, valueJsonStringified);
            if (!valueJsonNode.isObject()) {
                throw new JsonGenerationException(new ClassCastException(
                        "DataSourceSerDe.DataSourceSerializer#serialize accepts only valid JSON object"));
            }
            ((ObjectNode) valueJsonNode).serialize(jgen, provider);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy