fr.insee.vtl.jackson.BindingsDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vtl-jackson Show documentation
Show all versions of vtl-jackson Show documentation
Jackson module for the VTL model
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;
}
}