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

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

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

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import fr.insee.vtl.model.Dataset;

import javax.script.Bindings;
import javax.script.SimpleBindings;
import java.io.IOException;

/**
 * BindingsDeserializer is a JSON deserializer specialized for data bindings.
 */
public class BindingsDeserializer extends StdDeserializer {

    /**
     * Base constructor.
     */
    protected BindingsDeserializer() {
        super(Bindings.class);
    }

    /**
     * Deserializes JSON data bindings into a Bindings object.
     *
     * @param p    The base JSON parser.
     * @param ctxt A deserialization context.
     * @return The deserialized data bindings.
     * @throws IOException In case of problem while processing the JSON bindings.
     */
    @Override
    public Bindings deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {

        SimpleBindings bindings = new SimpleBindings();
        JsonToken token = p.currentToken();
        if (!token.isStructStart()) {
            ctxt.handleUnexpectedToken(Bindings.class, p);
        }

        while (p.nextToken() == JsonToken.FIELD_NAME) {
            String name = p.currentName();
            JsonToken value = p.nextValue();
            if (value.isStructStart()) {
                // Dataset
                bindings.put(name, ctxt.readValue(p, Dataset.class));
            } else {
                // Scalar
                bindings.put(name, ctxt.readValue(p, Object.class));
            }
        }

        return bindings;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy