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

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

Introduction

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

Prototype

public abstract JsonLocation getCurrentLocation();

Source Link

Document

Method that returns location of the last processed character; usually for error reporting purposes.

Usage

From source file:org.debezium.core.doc.JacksonReader.java

private Array parseArray(JsonParser parser) throws IOException {
    // Iterate over the fields in the top-level document ...
    BasicArray array = new BasicArray();
    JsonToken token = parser.nextToken();
    while (token != JsonToken.END_ARRAY) {
        switch (token) {
        case START_OBJECT:
            array.add(parseDocument(parser, true));
            break;
        case START_ARRAY:
            array.add(parseArray(parser));
            break;
        case VALUE_STRING:
            array.add(parser.getValueAsString());
            break;
        case VALUE_TRUE:
            array.add(true);/*  w  w  w  .j  ava 2s .c  o m*/
            break;
        case VALUE_FALSE:
            array.add(false);
            break;
        case VALUE_NULL:
            array.addNull();
            break;
        case VALUE_NUMBER_FLOAT:
        case VALUE_NUMBER_INT:
            switch (parser.getNumberType()) {
            case FLOAT:
                array.add(parser.getFloatValue());
                break;
            case DOUBLE:
                array.add(parser.getDoubleValue());
                break;
            case BIG_DECIMAL:
                array.add(parser.getDecimalValue());
                break;
            case INT:
                array.add(parser.getIntValue());
                break;
            case LONG:
                array.add(parser.getLongValue());
                break;
            case BIG_INTEGER:
                array.add(parser.getBigIntegerValue());
                break;
            }
            break;
        case VALUE_EMBEDDED_OBJECT:
            // disregard this, since it's an extension ...
            break;
        case NOT_AVAILABLE:
            throw new JsonParseException("Non-blocking parsers are not supported", parser.getCurrentLocation());
        case FIELD_NAME:
            throw new JsonParseException("Not expecting a FIELD_NAME token", parser.getCurrentLocation());
        case END_ARRAY:
            throw new JsonParseException("Not expecting an END_ARRAY token", parser.getCurrentLocation());
        case END_OBJECT:
            throw new JsonParseException("Not expecting an END_OBJECT token", parser.getCurrentLocation());
        }
        token = parser.nextToken();
    }
    return array;
}

From source file:io.debezium.document.JacksonReader.java

private Array parseArray(JsonParser parser, boolean nested) throws IOException {
    // Iterate over the values in the array ...
    BasicArray array = new BasicArray();
    JsonToken token = null;/*from w ww. jav a  2s  .  co m*/
    if (!nested) {
        // We expect the START_ARRAY token ...
        token = parser.nextToken();
        if (!nested && token != JsonToken.START_ARRAY) {
            throw new IOException("Expected data to start with an Array, but was " + token);
        }
    }
    token = parser.nextToken();
    while (token != JsonToken.END_ARRAY) {
        switch (token) {
        case START_OBJECT:
            array.add(parseDocument(parser, true));
            break;
        case START_ARRAY:
            array.add(parseArray(parser, true));
            break;
        case VALUE_STRING:
            array.add(parser.getValueAsString());
            break;
        case VALUE_TRUE:
            array.add(true);
            break;
        case VALUE_FALSE:
            array.add(false);
            break;
        case VALUE_NULL:
            array.addNull();
            break;
        case VALUE_NUMBER_FLOAT:
        case VALUE_NUMBER_INT:
            switch (parser.getNumberType()) {
            case FLOAT:
                array.add(parser.getFloatValue());
                break;
            case DOUBLE:
                array.add(parser.getDoubleValue());
                break;
            case BIG_DECIMAL:
                array.add(parser.getDecimalValue());
                break;
            case INT:
                array.add(parser.getIntValue());
                break;
            case LONG:
                array.add(parser.getLongValue());
                break;
            case BIG_INTEGER:
                array.add(parser.getBigIntegerValue());
                break;
            }
            break;
        case VALUE_EMBEDDED_OBJECT:
            // disregard this, since it's an extension ...
            break;
        case NOT_AVAILABLE:
            throw new JsonParseException("Non-blocking parsers are not supported", parser.getCurrentLocation());
        case FIELD_NAME:
            throw new JsonParseException("Not expecting a FIELD_NAME token", parser.getCurrentLocation());
        case END_ARRAY:
            throw new JsonParseException("Not expecting an END_ARRAY token", parser.getCurrentLocation());
        case END_OBJECT:
            throw new JsonParseException("Not expecting an END_OBJECT token", parser.getCurrentLocation());
        }
        token = parser.nextToken();
    }
    return array;
}

From source file:org.debezium.core.doc.JacksonReader.java

private Document parseDocument(JsonParser parser, boolean nested) throws IOException {
    // Iterate over the fields in the top-level document ...
    BasicDocument doc = new BasicDocument();
    JsonToken token = null;/*from  w  w w.  j  a  va 2 s.c  o  m*/
    if (!nested) {
        // We expect the START_OBJECT token ...
        token = parser.nextToken();
        if (!nested && token != JsonToken.START_OBJECT) {
            throw new IOException("Expected data to start with an Object, but was " + token);
        }
    }
    String fieldName = null;
    token = parser.nextToken();
    while (token != JsonToken.END_OBJECT) {
        switch (token) {
        case FIELD_NAME:
            fieldName = parser.getCurrentName();
            break;
        case START_OBJECT:
            doc.setDocument(fieldName, parseDocument(parser, true));
            break;
        case START_ARRAY:
            doc.setArray(fieldName, parseArray(parser));
            break;
        case VALUE_STRING:
            doc.setString(fieldName, parser.getValueAsString());
            break;
        case VALUE_TRUE:
            doc.setBoolean(fieldName, true);
            break;
        case VALUE_FALSE:
            doc.setBoolean(fieldName, false);
            break;
        case VALUE_NULL:
            doc.setNull(fieldName);
            break;
        case VALUE_NUMBER_FLOAT:
        case VALUE_NUMBER_INT:
            switch (parser.getNumberType()) {
            case FLOAT:
                doc.setNumber(fieldName, parser.getFloatValue());
                break;
            case DOUBLE:
                doc.setNumber(fieldName, parser.getDoubleValue());
                break;
            case BIG_DECIMAL:
                doc.setNumber(fieldName, parser.getDecimalValue());
                break;
            case INT:
                doc.setNumber(fieldName, parser.getIntValue());
                break;
            case LONG:
                doc.setNumber(fieldName, parser.getLongValue());
                break;
            case BIG_INTEGER:
                doc.setNumber(fieldName, parser.getBigIntegerValue());
                break;
            }
            break;
        case VALUE_EMBEDDED_OBJECT:
            // disregard this, since it's an extension ...
            break;
        case NOT_AVAILABLE:
            throw new JsonParseException("Non-blocking parsers are not supported", parser.getCurrentLocation());
        case END_ARRAY:
            throw new JsonParseException("Not expecting an END_ARRAY token", parser.getCurrentLocation());
        case END_OBJECT:
            throw new JsonParseException("Not expecting an END_OBJECT token", parser.getCurrentLocation());
        }
        token = parser.nextToken();
    }
    return doc;
}

From source file:io.debezium.document.JacksonReader.java

private Document parseDocument(JsonParser parser, boolean nested) throws IOException {
    // Iterate over the fields in the top-level document ...
    BasicDocument doc = new BasicDocument();
    JsonToken token = null;/*from   w w  w.j ava 2  s  .c  o m*/
    if (!nested) {
        // We expect the START_OBJECT token ...
        token = parser.nextToken();
        if (!nested && token != JsonToken.START_OBJECT) {
            throw new IOException("Expected data to start with an Object, but was " + token);
        }
    }
    String fieldName = null;
    token = parser.nextToken();
    while (token != JsonToken.END_OBJECT) {
        switch (token) {
        case FIELD_NAME:
            fieldName = parser.getCurrentName();
            break;
        case START_OBJECT:
            doc.setDocument(fieldName, parseDocument(parser, true));
            break;
        case START_ARRAY:
            doc.setArray(fieldName, parseArray(parser, true));
            break;
        case VALUE_STRING:
            doc.setString(fieldName, parser.getValueAsString());
            break;
        case VALUE_TRUE:
            doc.setBoolean(fieldName, true);
            break;
        case VALUE_FALSE:
            doc.setBoolean(fieldName, false);
            break;
        case VALUE_NULL:
            doc.setNull(fieldName);
            break;
        case VALUE_NUMBER_FLOAT:
        case VALUE_NUMBER_INT:
            switch (parser.getNumberType()) {
            case FLOAT:
                doc.setNumber(fieldName, parser.getFloatValue());
                break;
            case DOUBLE:
                doc.setNumber(fieldName, parser.getDoubleValue());
                break;
            case BIG_DECIMAL:
                doc.setNumber(fieldName, parser.getDecimalValue());
                break;
            case INT:
                doc.setNumber(fieldName, parser.getIntValue());
                break;
            case LONG:
                doc.setNumber(fieldName, parser.getLongValue());
                break;
            case BIG_INTEGER:
                doc.setNumber(fieldName, parser.getBigIntegerValue());
                break;
            }
            break;
        case VALUE_EMBEDDED_OBJECT:
            // disregard this, since it's an extension ...
            break;
        case NOT_AVAILABLE:
            throw new JsonParseException("Non-blocking parsers are not supported", parser.getCurrentLocation());
        case END_ARRAY:
            throw new JsonParseException("Not expecting an END_ARRAY token", parser.getCurrentLocation());
        case END_OBJECT:
            throw new JsonParseException("Not expecting an END_OBJECT token", parser.getCurrentLocation());
        }
        token = parser.nextToken();
    }
    return doc;
}

From source file:com.msopentech.odatajclient.engine.data.json.JSONPropertyDeserializer.java

@Override
protected JSONProperty doDeserializeV4(JsonParser parser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser);
    final JSONProperty property = new JSONProperty();

    try {/*  w ww  .  j a v a 2  s  .c om*/
        final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
        final Document document = builder.newDocument();

        Element content = document.createElement(ODataConstants.ELEM_PROPERTY);

        JsonNode subtree = null;
        if (tree.has(ODataConstants.JSON_VALUE)) {
            if (tree.has(v4AnnotationPrefix + ODataConstants.JSON_TYPE)
                    && StringUtils.isBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {

                content.setAttribute(ODataConstants.ATTR_M_TYPE,
                        tree.get(v4AnnotationPrefix + ODataConstants.JSON_TYPE).asText());
            }

            final JsonNode value = tree.get(ODataConstants.JSON_VALUE);
            if (value.isValueNode()) {
                content.appendChild(document.createTextNode(value.asText()));
            } else if (EdmSimpleType.isGeospatial(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                subtree = tree.objectNode();
                ((ObjectNode) subtree).put(ODataConstants.JSON_VALUE, tree.get(ODataConstants.JSON_VALUE));
                if (StringUtils.isNotBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                    ((ObjectNode) subtree).put(ODataConstants.JSON_VALUE + "@" + ODataConstants.JSON_TYPE,
                            content.getAttribute(ODataConstants.ATTR_M_TYPE));
                }
            } else {
                subtree = tree.get(ODataConstants.JSON_VALUE);
            }
        } else {
            subtree = tree;
        }

        if (subtree != null) {
            DOMTreeUtilsV4.buildSubtree(content, subtree);
        }

        final List<Node> children = XMLUtils.getChildNodes(content, Node.ELEMENT_NODE);
        if (children.size() == 1) {
            final Element value = (Element) children.iterator().next();
            if (ODataConstants.JSON_VALUE.equals(XMLUtils.getSimpleName(value))) {
                if (StringUtils.isNotBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                    value.setAttribute(ODataConstants.ATTR_M_TYPE,
                            content.getAttribute(ODataConstants.ATTR_M_TYPE));
                }
                content = value;
            }
        }

        property.setContent(content);
    } catch (ParserConfigurationException e) {
        throw new JsonParseException("Cannot build property", parser.getCurrentLocation(), e);
    }

    return property;
}

From source file:com.msopentech.odatajclient.engine.data.impl.JSONPropertyDeserializer.java

@Override
protected JSONProperty doDeserialize(final JsonParser parser, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser);

    final JSONProperty property = new JSONProperty();

    if (tree.hasNonNull(ODataConstants.JSON_METADATA)) {
        property.setMetadata(URI.create(tree.get(ODataConstants.JSON_METADATA).textValue()));
        tree.remove(ODataConstants.JSON_METADATA);
    }//from   w  w w .  ja v  a2 s .c o m

    try {
        final DocumentBuilder builder = XMLUtils.DOC_BUILDER_FACTORY.newDocumentBuilder();
        final Document document = builder.newDocument();

        Element content = document.createElement(ODataConstants.ELEM_PROPERTY);

        if (property.getMetadata() != null) {
            final String metadataURI = property.getMetadata().toASCIIString();
            final int dashIdx = metadataURI.lastIndexOf('#');
            if (dashIdx != -1) {
                content.setAttribute(ODataConstants.ATTR_M_TYPE, metadataURI.substring(dashIdx + 1));
            }
        }

        JsonNode subtree = null;
        if (tree.has(ODataConstants.JSON_VALUE)) {
            if (tree.has(ODataConstants.JSON_TYPE)
                    && StringUtils.isBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {

                content.setAttribute(ODataConstants.ATTR_M_TYPE, tree.get(ODataConstants.JSON_TYPE).asText());
            }

            final JsonNode value = tree.get(ODataConstants.JSON_VALUE);
            if (value.isValueNode()) {
                content.appendChild(document.createTextNode(value.asText()));
            } else if (EdmSimpleType.isGeospatial(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                subtree = tree.objectNode();
                ((ObjectNode) subtree).put(ODataConstants.JSON_VALUE, tree.get(ODataConstants.JSON_VALUE));
                if (StringUtils.isNotBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                    ((ObjectNode) subtree).put(ODataConstants.JSON_VALUE + "@" + ODataConstants.JSON_TYPE,
                            content.getAttribute(ODataConstants.ATTR_M_TYPE));
                }
            } else {
                subtree = tree.get(ODataConstants.JSON_VALUE);
            }
        } else {
            subtree = tree;
        }

        if (subtree != null) {
            JSONDOMTreeUtils.buildSubtree(client, content, subtree);
        }

        final List<Node> children = XMLUtils.getChildNodes(content, Node.ELEMENT_NODE);
        if (children.size() == 1) {
            final Element value = (Element) children.iterator().next();
            if (ODataConstants.JSON_VALUE.equals(XMLUtils.getSimpleName(value))) {
                if (StringUtils.isNotBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                    value.setAttribute(ODataConstants.ATTR_M_TYPE,
                            content.getAttribute(ODataConstants.ATTR_M_TYPE));
                }
                content = value;
            }
        }

        property.setContent(content);
    } catch (ParserConfigurationException e) {
        throw new JsonParseException("Cannot build property", parser.getCurrentLocation(), e);
    }

    return property;
}

From source file:com.msopentech.odatajclient.engine.data.json.JSONPropertyDeserializer.java

@Override
protected JSONProperty doDeserializeV3(final JsonParser parser, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser);

    final JSONProperty property = new JSONProperty();

    if (tree.hasNonNull(ODataConstants.JSON_METADATA)) {
        property.setMetadata(URI.create(tree.get(ODataConstants.JSON_METADATA).textValue()));
        tree.remove(ODataConstants.JSON_METADATA);
    }/*from  w  w  w. j av a  2  s  . c  o m*/

    try {
        final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
        final Document document = builder.newDocument();

        Element content = document.createElement(ODataConstants.ELEM_PROPERTY);

        if (property.getMetadata() != null) {
            final String metadataURI = property.getMetadata().toASCIIString();
            final int dashIdx = metadataURI.lastIndexOf('#');
            if (dashIdx != -1) {
                content.setAttribute(ODataConstants.ATTR_M_TYPE, metadataURI.substring(dashIdx + 1));
            }
        }

        JsonNode subtree = null;
        if (tree.has(ODataConstants.JSON_VALUE)) {
            if (tree.has(ODataConstants.JSON_TYPE)
                    && StringUtils.isBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {

                content.setAttribute(ODataConstants.ATTR_M_TYPE, tree.get(ODataConstants.JSON_TYPE).asText());
            }

            final JsonNode value = tree.get(ODataConstants.JSON_VALUE);
            if (value.isValueNode()) {
                content.appendChild(document.createTextNode(value.asText()));
            } else if (EdmSimpleType.isGeospatial(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                subtree = tree.objectNode();
                ((ObjectNode) subtree).put(ODataConstants.JSON_VALUE, tree.get(ODataConstants.JSON_VALUE));
                if (StringUtils.isNotBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                    ((ObjectNode) subtree).put(ODataConstants.JSON_VALUE + "@" + ODataConstants.JSON_TYPE,
                            content.getAttribute(ODataConstants.ATTR_M_TYPE));
                }
            } else {
                subtree = tree.get(ODataConstants.JSON_VALUE);
            }
        } else {
            subtree = tree;
        }

        if (subtree != null) {
            DOMTreeUtilsV3.buildSubtree(content, subtree);
        }

        final List<Node> children = XMLUtils.getChildNodes(content, Node.ELEMENT_NODE);
        if (children.size() == 1) {
            final Element value = (Element) children.iterator().next();
            if (ODataConstants.JSON_VALUE.equals(XMLUtils.getSimpleName(value))) {
                if (StringUtils.isNotBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                    value.setAttribute(ODataConstants.ATTR_M_TYPE,
                            content.getAttribute(ODataConstants.ATTR_M_TYPE));
                }
                content = value;
            }
        }

        property.setContent(content);
    } catch (ParserConfigurationException e) {
        throw new JsonParseException("Cannot build property", parser.getCurrentLocation(), e);
    }

    return property;
}

From source file:org.apache.olingo.client.core.serialization.JsonEntityDeserializer.java

protected ResWrap<Entity> doDeserialize(final JsonParser parser) throws IOException {

    final ObjectNode tree = parser.getCodec().readTree(parser);

    if (tree.has(Constants.VALUE) && tree.get(Constants.VALUE).isArray()) {
        throw new JsonParseException("Expected OData Entity, found EntitySet", parser.getCurrentLocation());
    }/*from  w ww.  jav  a 2  s .  c  o m*/

    final Entity entity = new Entity();

    final URI contextURL;
    if (tree.hasNonNull(Constants.JSON_CONTEXT)) {
        contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue());
        tree.remove(Constants.JSON_CONTEXT);
    } else if (tree.hasNonNull(Constants.JSON_METADATA)) {
        contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue());
        tree.remove(Constants.JSON_METADATA);
    } else {
        contextURL = null;
    }
    if (contextURL != null) {
        entity.setBaseURI(
                URI.create(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA)));
    }

    final String metadataETag;
    if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) {
        metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue();
        tree.remove(Constants.JSON_METADATA_ETAG);
    } else {
        metadataETag = null;
    }

    if (tree.hasNonNull(Constants.JSON_ETAG)) {
        entity.setETag(tree.get(Constants.JSON_ETAG).textValue());
        tree.remove(Constants.JSON_ETAG);
    }

    if (tree.hasNonNull(Constants.JSON_TYPE)) {
        entity.setType(new EdmTypeInfo.Builder().setTypeExpression(tree.get(Constants.JSON_TYPE).textValue())
                .build().internal());
        tree.remove(Constants.JSON_TYPE);
    }

    if (tree.hasNonNull(Constants.JSON_ID)) {
        entity.setId(URI.create(tree.get(Constants.JSON_ID).textValue()));
        tree.remove(Constants.JSON_ID);
    }

    if (tree.hasNonNull(Constants.JSON_READ_LINK)) {
        final Link link = new Link();
        link.setRel(Constants.SELF_LINK_REL);
        link.setHref(tree.get(Constants.JSON_READ_LINK).textValue());
        entity.setSelfLink(link);

        tree.remove(Constants.JSON_READ_LINK);
    }

    if (tree.hasNonNull(Constants.JSON_EDIT_LINK)) {
        final Link link = new Link();
        if (serverMode) {
            link.setRel(Constants.EDIT_LINK_REL);
        }
        link.setHref(tree.get(Constants.JSON_EDIT_LINK).textValue());
        entity.setEditLink(link);

        tree.remove(Constants.JSON_EDIT_LINK);
    }

    if (tree.hasNonNull(Constants.JSON_MEDIA_READ_LINK)) {
        entity.setMediaContentSource(URI.create(tree.get(Constants.JSON_MEDIA_READ_LINK).textValue()));
        tree.remove(Constants.JSON_MEDIA_READ_LINK);
    }
    if (tree.hasNonNull(Constants.JSON_MEDIA_EDIT_LINK)) {
        entity.setMediaContentSource(URI.create(tree.get(Constants.JSON_MEDIA_EDIT_LINK).textValue()));
        tree.remove(Constants.JSON_MEDIA_EDIT_LINK);
    }
    if (tree.hasNonNull(Constants.JSON_MEDIA_CONTENT_TYPE)) {
        entity.setMediaContentType(tree.get(Constants.JSON_MEDIA_CONTENT_TYPE).textValue());
        tree.remove(Constants.JSON_MEDIA_CONTENT_TYPE);
    }
    if (tree.hasNonNull(Constants.JSON_MEDIA_ETAG)) {
        entity.setMediaETag(tree.get(Constants.JSON_MEDIA_ETAG).textValue());
        tree.remove(Constants.JSON_MEDIA_ETAG);
    }

    final Set<String> toRemove = new HashSet<String>();

    final Map<String, List<Annotation>> annotations = new HashMap<String, List<Annotation>>();
    for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) {
        final Map.Entry<String, JsonNode> field = itor.next();
        final Matcher customAnnotation = CUSTOM_ANNOTATION.matcher(field.getKey());

        links(field, entity, toRemove, tree, parser.getCodec());
        if (field.getKey().endsWith(getJSONAnnotation(Constants.JSON_MEDIA_READ_LINK))) {
            final Link link = new Link();
            link.setTitle(getTitle(field));
            link.setRel(Constants.NS_MEDIA_READ_LINK_REL + getTitle(field));
            link.setType(Constants.MEDIA_EDIT_LINK_TYPE);
            link.setHref(field.getValue().textValue());
            entity.getMediaEditLinks().add(link);

            if (tree.has(link.getTitle() + getJSONAnnotation(Constants.JSON_MEDIA_ETAG))) {
                link.setMediaETag(
                        tree.get(link.getTitle() + getJSONAnnotation(Constants.JSON_MEDIA_ETAG)).asText());
                toRemove.add(link.getTitle() + getJSONAnnotation(Constants.JSON_MEDIA_ETAG));
            }

            if (tree.has(link.getTitle() + getJSONAnnotation(Constants.JSON_MEDIA_CONTENT_TYPE))) {
                link.setType(tree.get(link.getTitle() + getJSONAnnotation(Constants.JSON_MEDIA_CONTENT_TYPE))
                        .asText());
                toRemove.add(link.getTitle() + getJSONAnnotation(Constants.JSON_MEDIA_CONTENT_TYPE));
            }

            toRemove.add(field.getKey());
            toRemove.add(setInline(field.getKey(), getJSONAnnotation(Constants.JSON_MEDIA_READ_LINK), tree,
                    parser.getCodec(), link));
        } else if (field.getKey().endsWith(getJSONAnnotation(Constants.JSON_MEDIA_EDIT_LINK))) {
            final Link link = getOrCreateMediaLink(entity, getTitle(field));
            link.setRel(Constants.NS_MEDIA_EDIT_LINK_REL + getTitle(field));
            link.setHref(field.getValue().textValue());
            toRemove.add(field.getKey());
            toRemove.add(setInline(field.getKey(), getJSONAnnotation(Constants.JSON_MEDIA_EDIT_LINK), tree,
                    parser.getCodec(), link));
        } else if (field.getKey().endsWith(getJSONAnnotation(Constants.JSON_MEDIA_CONTENT_TYPE))) {
            final Link link = getOrCreateMediaLink(entity, getTitle(field));
            link.setType(field.getValue().asText());
            toRemove.add(field.getKey());
        } else if (field.getKey().endsWith(getJSONAnnotation(Constants.JSON_MEDIA_ETAG))) {
            final Link link = getOrCreateMediaLink(entity, getTitle(field));
            link.setMediaETag(field.getValue().asText());
            toRemove.add(field.getKey());
        } else if (field.getKey().charAt(0) == '#') {
            final Operation operation = new Operation();
            operation.setMetadataAnchor(field.getKey());

            final ObjectNode opNode = (ObjectNode) tree.get(field.getKey());
            operation.setTitle(opNode.get(Constants.ATTR_TITLE).asText());
            operation.setTarget(URI.create(opNode.get(Constants.ATTR_TARGET).asText()));

            entity.getOperations().add(operation);

            toRemove.add(field.getKey());
        } else if (customAnnotation.matches() && !"odata".equals(customAnnotation.group(2))) {
            final Annotation annotation = new Annotation();
            annotation.setTerm(customAnnotation.group(2) + "." + customAnnotation.group(3));
            try {
                value(annotation, field.getValue(), parser.getCodec());
            } catch (final EdmPrimitiveTypeException e) {
                throw new IOException(e);
            }

            if (!annotations.containsKey(customAnnotation.group(1))) {
                annotations.put(customAnnotation.group(1), new ArrayList<Annotation>());
            }
            annotations.get(customAnnotation.group(1)).add(annotation);
        }
    }

    for (Link link : entity.getNavigationLinks()) {
        if (annotations.containsKey(link.getTitle())) {
            link.getAnnotations().addAll(annotations.get(link.getTitle()));
            for (Annotation annotation : annotations.get(link.getTitle())) {
                toRemove.add(link.getTitle() + "@" + annotation.getTerm());
            }
        }
    }
    for (Link link : entity.getMediaEditLinks()) {
        if (annotations.containsKey(link.getTitle())) {
            link.getAnnotations().addAll(annotations.get(link.getTitle()));
            for (Annotation annotation : annotations.get(link.getTitle())) {
                toRemove.add(link.getTitle() + "@" + annotation.getTerm());
            }
        }
    }

    tree.remove(toRemove);

    try {
        populate(entity, entity.getProperties(), tree, parser.getCodec());
    } catch (final EdmPrimitiveTypeException e) {
        throw new IOException(e);
    }

    return new ResWrap<Entity>(contextURL, metadataETag, entity);
}

From source file:org.apache.olingo.commons.core.serialization.JsonEntityDeserializer.java

protected ResWrap<Entity> doDeserialize(final JsonParser parser) throws IOException {

    final ObjectNode tree = parser.getCodec().readTree(parser);

    if (tree.has(Constants.VALUE) && tree.get(Constants.VALUE).isArray()) {
        throw new JsonParseException("Expected OData Entity, found EntitySet", parser.getCurrentLocation());
    }//  w  w w  .  j av a 2s .c o m

    final EntityImpl entity = new EntityImpl();

    final URI contextURL;
    if (tree.hasNonNull(Constants.JSON_CONTEXT)) {
        contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue());
        tree.remove(Constants.JSON_CONTEXT);
    } else if (tree.hasNonNull(Constants.JSON_METADATA)) {
        contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue());
        tree.remove(Constants.JSON_METADATA);
    } else {
        contextURL = null;
    }
    if (contextURL != null) {
        entity.setBaseURI(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA));
    }

    final String metadataETag;
    if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) {
        metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue();
        tree.remove(Constants.JSON_METADATA_ETAG);
    } else {
        metadataETag = null;
    }

    if (tree.hasNonNull(jsonETag)) {
        entity.setETag(tree.get(jsonETag).textValue());
        tree.remove(jsonETag);
    }

    if (tree.hasNonNull(jsonType)) {
        entity.setType(
                new EdmTypeInfo.Builder().setTypeExpression(tree.get(jsonType).textValue()).build().internal());
        tree.remove(jsonType);
    }

    if (tree.hasNonNull(jsonId)) {
        entity.setId(URI.create(tree.get(jsonId).textValue()));
        tree.remove(jsonId);
    }

    if (tree.hasNonNull(jsonReadLink)) {
        final LinkImpl link = new LinkImpl();
        link.setRel(Constants.SELF_LINK_REL);
        link.setHref(tree.get(jsonReadLink).textValue());
        entity.setSelfLink(link);

        tree.remove(jsonReadLink);
    }

    if (tree.hasNonNull(jsonEditLink)) {
        final LinkImpl link = new LinkImpl();
        if (serverMode) {
            link.setRel(Constants.EDIT_LINK_REL);
        }
        link.setHref(tree.get(jsonEditLink).textValue());
        entity.setEditLink(link);

        tree.remove(jsonEditLink);
    }

    if (tree.hasNonNull(jsonMediaReadLink)) {
        entity.setMediaContentSource(URI.create(tree.get(jsonMediaReadLink).textValue()));
        tree.remove(jsonMediaReadLink);
    }
    if (tree.hasNonNull(jsonMediaEditLink)) {
        entity.setMediaContentSource(URI.create(tree.get(jsonMediaEditLink).textValue()));
        tree.remove(jsonMediaEditLink);
    }
    if (tree.hasNonNull(jsonMediaContentType)) {
        entity.setMediaContentType(tree.get(jsonMediaContentType).textValue());
        tree.remove(jsonMediaContentType);
    }
    if (tree.hasNonNull(jsonMediaETag)) {
        entity.setMediaETag(tree.get(jsonMediaETag).textValue());
        tree.remove(jsonMediaETag);
    }

    final Set<String> toRemove = new HashSet<String>();

    final Map<String, List<Annotation>> annotations = new HashMap<String, List<Annotation>>();
    for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) {
        final Map.Entry<String, JsonNode> field = itor.next();
        final Matcher customAnnotation = CUSTOM_ANNOTATION.matcher(field.getKey());

        links(field, entity, toRemove, tree, parser.getCodec());
        if (field.getKey().endsWith(getJSONAnnotation(jsonMediaEditLink))) {
            final LinkImpl link = new LinkImpl();
            link.setTitle(getTitle(field));
            link.setRel(version.getNamespace(ODataServiceVersion.NamespaceKey.MEDIA_EDIT_LINK_REL)
                    + getTitle(field));
            link.setHref(field.getValue().textValue());
            link.setType(ODataLinkType.MEDIA_EDIT.toString());
            entity.getMediaEditLinks().add(link);

            if (tree.has(link.getTitle() + getJSONAnnotation(jsonMediaETag))) {
                link.setMediaETag(tree.get(link.getTitle() + getJSONAnnotation(jsonMediaETag)).asText());
                toRemove.add(link.getTitle() + getJSONAnnotation(jsonMediaETag));
            }

            toRemove.add(field.getKey());
            toRemove.add(setInline(field.getKey(), getJSONAnnotation(jsonMediaEditLink), tree,
                    parser.getCodec(), link));
        } else if (field.getKey().endsWith(getJSONAnnotation(jsonMediaContentType))) {
            final String linkTitle = getTitle(field);
            for (Link link : entity.getMediaEditLinks()) {
                if (linkTitle.equals(link.getTitle())) {
                    ((LinkImpl) link).setType(field.getValue().asText());
                }
            }
            toRemove.add(field.getKey());
        } else if (field.getKey().charAt(0) == '#') {
            final ODataOperation operation = new ODataOperation();
            operation.setMetadataAnchor(field.getKey());

            final ObjectNode opNode = (ObjectNode) tree.get(field.getKey());
            operation.setTitle(opNode.get(Constants.ATTR_TITLE).asText());
            operation.setTarget(URI.create(opNode.get(Constants.ATTR_TARGET).asText()));

            entity.getOperations().add(operation);

            toRemove.add(field.getKey());
        } else if (customAnnotation.matches() && !"odata".equals(customAnnotation.group(2))) {
            final Annotation annotation = new AnnotationImpl();
            annotation.setTerm(customAnnotation.group(2) + "." + customAnnotation.group(3));
            try {
                value(annotation, field.getValue(), parser.getCodec());
            } catch (final EdmPrimitiveTypeException e) {
                throw new IOException(e);
            }

            if (!annotations.containsKey(customAnnotation.group(1))) {
                annotations.put(customAnnotation.group(1), new ArrayList<Annotation>());
            }
            annotations.get(customAnnotation.group(1)).add(annotation);
        }
    }

    for (Link link : entity.getNavigationLinks()) {
        if (annotations.containsKey(link.getTitle())) {
            link.getAnnotations().addAll(annotations.get(link.getTitle()));
            for (Annotation annotation : annotations.get(link.getTitle())) {
                toRemove.add(link.getTitle() + "@" + annotation.getTerm());
            }
        }
    }
    for (Link link : entity.getMediaEditLinks()) {
        if (annotations.containsKey(link.getTitle())) {
            link.getAnnotations().addAll(annotations.get(link.getTitle()));
            for (Annotation annotation : annotations.get(link.getTitle())) {
                toRemove.add(link.getTitle() + "@" + annotation.getTerm());
            }
        }
    }

    tree.remove(toRemove);

    try {
        populate(entity, entity.getProperties(), tree, parser.getCodec());
    } catch (final EdmPrimitiveTypeException e) {
        throw new IOException(e);
    }

    return new ResWrap<Entity>(contextURL, metadataETag, entity);
}