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

fr.insee.vtl.jackson.ComponentDeserializer Maven / Gradle / Ivy

The newest version!
package fr.insee.vtl.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import fr.insee.vtl.model.Dataset;
import fr.insee.vtl.model.Structured;
import fr.insee.vtl.model.utils.Java8Helpers;

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

/**
 * ComponentDeserializer is a JSON deserializer specialized for dataset components.
 */
public class ComponentDeserializer extends StdDeserializer {

    private static final Map> TYPES = Java8Helpers.mapOf(
            "STRING", String.class,
            "INTEGER", Long.class,
            "NUMBER", Double.class,
            "BOOLEAN", Boolean.class
    );

    /**
     * Base constructor.
     */
    protected ComponentDeserializer() {
        super(Structured.Component.class);
    }

    /**
     * Deserializes a JSON component into a Structured.Component object.
     *
     * @param p    The base JSON parser.
     * @param ctxt A deserialization context.
     * @return The deserialized dataset component.
     * @throws IOException In case of problem while processing the JSON component.
     */
    @Override
    public Structured.Component deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonNode node = ctxt.readTree(p);
        String name = node.get("name").asText();
        String type = node.get("type").asText();
        Dataset.Role role = Dataset.Role.valueOf(node.get("role").asText());
        Boolean nullable = node.get("nullable") != null ? node.get("nullable").asBoolean() : null;
        return new Dataset.Component(name, asType(type), role, nullable);
    }

    private Class asType(String type) {
        return TYPES.get(type);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy