Example usage for com.fasterxml.jackson.core JsonParser getCodec

List of usage examples for com.fasterxml.jackson.core JsonParser getCodec

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser getCodec.

Prototype

public abstract ObjectCodec getCodec();

Source Link

Document

Accessor for ObjectCodec associated with this parser, if any.

Usage

From source file:com.addthis.codec.jackson.CodecBeanDeserializer.java

@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonLocation currentLocation = jp.getTokenLocation();
    JsonToken t = jp.getCurrentToken();/*from  ww w . j a  va 2s  .  c om*/
    try {
        if (t == JsonToken.START_OBJECT) {
            ObjectNode objectNode = jp.readValueAsTree();
            handleDefaultsAndRequiredAndNull(ctxt, objectNode);
            jp = jp.getCodec().treeAsTokens(objectNode);
            jp.nextToken();
        } else if (t == JsonToken.END_OBJECT) {
            // for some reason this is how they chose to handle single field objects
            jp.nextToken();
            ObjectNode objectNode = ctxt.getNodeFactory().objectNode();
            handleDefaultsAndRequiredAndNull(ctxt, objectNode);
            jp = jp.getCodec().treeAsTokens(objectNode);
            jp.nextToken();
        }
        Object value = getDelegatee().deserialize(jp, ctxt);
        if (value instanceof SuperCodable) {
            ((SuperCodable) value).postDecode();
        }
        return value;
    } catch (JsonMappingException ex) {
        throw Jackson.maybeImproveLocation(currentLocation, ex);
    }
}

From source file:org.n52.tamis.core.json.deserialize.processes.SingleProcessDescriptionDeserializer.java

@Override
public ProcessDescription_singleProcess deserialize(JsonParser jsonParser, DeserializationContext arg1)
        throws IOException, JsonProcessingException {
    logger.info("Start deserialization of extended WPS singleProcessDescription document.");

    // initialization
    ObjectCodec codec = jsonParser.getCodec();
    JsonNode node = codec.readTree(jsonParser);

    // create empty shortened CapabilitiesDocument.
    ProcessDescription_singleProcess singleProcessDescription_short = new ProcessDescription_singleProcess();

    /*//  ww  w. j  a  v a  2s. c  om
     * JSPON element "ProcessSummaries" is an array of process descriptions
     */
    JsonNode process = node.get("ProcessOffering").get("Process");

    /*
     * label
     */
    singleProcessDescription_short.setLabel(process.get("Title").asText());

    /*
     * ID
     */
    singleProcessDescription_short.setId(process.get("Identifier").asText());

    /*
     * description
     */
    if (process.has("Abstract"))
        singleProcessDescription_short.setDescription(process.get("Abstract").asText());
    else {
        // just set the title/label as description
        singleProcessDescription_short.setDescription(singleProcessDescription_short.getLabel());
    }

    // array of Input elements
    JsonNode inputs = process.get("Input");

    transformAndAddInputs(singleProcessDescription_short, inputs);

    /*
     * TODO are multiple outputs possible?
     */
    JsonNode outputs = process.get("Output");

    transformAndAddOutputs(singleProcessDescription_short, outputs);

    logger.info("Deserialization ended! The following processes description instance was created: {}",
            singleProcessDescription_short);

    return singleProcessDescription_short;
}

From source file:com.addthis.codec.jackson.CaseIgnoringEnumDeserializer.java

@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken curr = jp.getCurrentToken();

    if ((curr == JsonToken.VALUE_STRING) || (curr == JsonToken.FIELD_NAME) || (curr == JsonToken.VALUE_FALSE)
            || (curr == JsonToken.VALUE_TRUE)) {
        String name = jp.getText();
        if (_lookupByName.find(name) != null) {
            return super.deserialize(jp, ctxt);
        }//  w w w . ja  va2 s. c o  m
        TextNode upperName = ctxt.getNodeFactory().textNode(name.toUpperCase());

        JsonParser treeParser = jp.getCodec().treeAsTokens(upperName);
        treeParser.nextToken();
        return super.deserialize(treeParser, ctxt);
    } else {
        return super.deserialize(jp, ctxt);
    }
}

From source file:org.apache.syncope.core.misc.serialization.AttributeDeserializer.java

@Override
public Attribute deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException {

    ObjectNode tree = jp.readValueAsTree();

    String name = tree.get("name").asText();

    List<Object> values = new ArrayList<Object>();
    for (Iterator<JsonNode> itor = tree.get("value").iterator(); itor.hasNext();) {
        JsonNode node = itor.next();//  w w w.j a v a 2 s.c  om
        if (node.isNull()) {
            values.add(null);
        } else if (node.isObject()) {
            values.add(((ObjectNode) node).traverse(jp.getCodec()).readValueAs(GuardedString.class));
        } else if (node.isBoolean()) {
            values.add(node.asBoolean());
        } else if (node.isDouble()) {
            values.add(node.asDouble());
        } else if (node.isLong()) {
            values.add(node.asLong());
        } else if (node.isInt()) {
            values.add(node.asInt());
        } else {
            String text = node.asText();
            if (text.startsWith(AttributeSerializer.BYTE_ARRAY_PREFIX)
                    && text.endsWith(AttributeSerializer.BYTE_ARRAY_SUFFIX)) {

                values.add(Base64.decode(StringUtils.substringBetween(text,
                        AttributeSerializer.BYTE_ARRAY_PREFIX, AttributeSerializer.BYTE_ARRAY_SUFFIX)));
            } else {
                values.add(text);
            }
        }
    }

    return Uid.NAME.equals(name)
            ? new Uid(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString())
            : Name.NAME.equals(name)
                    ? new Name(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString())
                    : AttributeBuilder.build(name, values);
}

From source file:de.upb.wdqa.wdvd.datamodel.oldjson.jackson.deserializers.OldAliasesDeserializer.java

@Override
public LinkedHashMap<String, List<String>> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    LinkedHashMap<String, List<String>> result = new LinkedHashMap<String, List<String>>();

    // Is the alias broken, i.e., it starts with '['
    if (jp.getCurrentToken().equals(JsonToken.START_ARRAY)) {
        jp.nextToken();//ww  w .j  ava  2  s  . co m
        if (!jp.getCurrentToken().equals(JsonToken.END_ARRAY)) {
            logger.warn("Token " + JsonToken.END_ARRAY + " expected");
        }
    } else {
        ObjectCodec mapper = jp.getCodec();
        result = mapper.readValue(jp, new TypeReference<LinkedHashMap<String, OldAliasList>>() {
        });
    }

    return result;
}

From source file:de.upb.wdqa.wdvd.datamodel.oldjson.jackson.deserializers.OldLabelsDescriptionsDeserializer.java

@Override
public LinkedHashMap<String, String> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    LinkedHashMap<String, String> result = null;

    // Is the alias broken, i.e., it starts with '['
    if (jp.getCurrentToken().equals(JsonToken.START_ARRAY)) {
        result = new LinkedHashMap<String, String>();
        jp.nextToken();/*  www. jav  a  2 s  .c  om*/
        if (!jp.getCurrentToken().equals(JsonToken.END_ARRAY)) {
            logger.warn("Token " + JsonToken.END_ARRAY + " expected");
        }
    } else {
        ObjectCodec mapper = jp.getCodec();
        result = mapper.readValue(jp, new TypeReference<LinkedHashMap<String, String>>() {
        });
    }

    return result;
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.EdmxDeserializer.java

@Override
public Edmx deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final Edmx edmx = new Edmx();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("Version".equals(jp.getCurrentName())) {
                edmx.setVersion(jp.nextTextValue());
            } else if ("DataServices".equals(jp.getCurrentName())) {
                jp.nextToken();//from w  w w .  j a v a2  s  . c o  m
                edmx.setDataServices(jp.getCodec().readValue(jp, DataServices.class));
            }
        }
    }

    return edmx;
}

From source file:de.upb.wdqa.wdvd.datamodel.oldjson.jackson.deserializers.OldSitelinksDeserializer.java

@Override
public LinkedHashMap<String, OldJacksonSiteLink> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    LinkedHashMap<String, OldJacksonSiteLink> result = null;

    // Is the alias broken, i.e., it starts with '['
    if (jp.getCurrentToken().equals(JsonToken.START_ARRAY)) {
        result = new LinkedHashMap<String, OldJacksonSiteLink>();
        jp.nextToken();// ww  w  .  ja  v  a 2 s  . c om
        if (!jp.getCurrentToken().equals(JsonToken.END_ARRAY)) {
            logger.warn("Token " + JsonToken.END_ARRAY + " expected");
        }
    } else {
        ObjectCodec mapper = jp.getCodec();
        result = mapper.readValue(jp, new TypeReference<LinkedHashMap<String, OldJacksonSiteLink>>() {
        });
    }

    return result;
}

From source file:org.apache.syncope.core.util.AttributeDeserializer.java

@Override
public Attribute deserialize(final JsonParser jp, final DeserializationContext ctx)
        throws IOException, JsonProcessingException {

    ObjectNode tree = jp.readValueAsTree();

    String name = tree.get("name").asText();

    List<Object> values = new ArrayList<Object>();
    for (Iterator<JsonNode> itor = tree.get("value").iterator(); itor.hasNext();) {
        JsonNode node = itor.next();// w w w.  j av  a 2  s  .  com
        if (node.isNull()) {
            values.add(null);
        } else if (node.isObject()) {
            values.add(((ObjectNode) node).traverse(jp.getCodec()).readValueAs(GuardedString.class));
        } else if (node.isBoolean()) {
            values.add(node.asBoolean());
        } else if (node.isDouble()) {
            values.add(node.asDouble());
        } else if (node.isLong()) {
            values.add(node.asLong());
        } else if (node.isInt()) {
            values.add(node.asInt());
        } else {
            String text = node.asText();
            if (text.startsWith(AttributeSerializer.BYTE_ARRAY_PREFIX)
                    && text.endsWith(AttributeSerializer.BYTE_ARRAY_SUFFIX)) {

                values.add(Base64.decode(StringUtils.substringBetween(text,
                        AttributeSerializer.BYTE_ARRAY_PREFIX, AttributeSerializer.BYTE_ARRAY_SUFFIX)));
            } else {
                values.add(text);
            }
        }
    }

    return Uid.NAME.equals(name)
            ? new Uid(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString())
            : Name.NAME.equals(name)
                    ? new Name(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString())
                    : AttributeBuilder.build(name, values);
}