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: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  2 s.c  om

    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);
}

From source file:org.apache.streams.jackson.ThroughputQueueDeserializer.java

@Override
public ThroughputQueueBroadcast deserialize(JsonParser jsonParser,
        DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    try {/*from  w  ww . j a  v a2s. c  om*/
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        ThroughputQueueBroadcast throughputQueueBroadcast = new ThroughputQueueBroadcast();
        JsonNode attributes = jsonParser.getCodec().readTree(jsonParser);

        ObjectName name = new ObjectName(attributes.get("canonicalName").asText());
        MBeanInfo info = server.getMBeanInfo(name);
        throughputQueueBroadcast.setName(name.toString());

        for (MBeanAttributeInfo attribute : Arrays.asList(info.getAttributes())) {
            try {
                switch (attribute.getName()) {
                case "CurrentSize":
                    throughputQueueBroadcast
                            .setCurrentSize((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "AvgWait":
                    throughputQueueBroadcast
                            .setAvgWait((double) server.getAttribute(name, attribute.getName()));
                    break;
                case "MaxWait":
                    throughputQueueBroadcast.setMaxWait((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "Removed":
                    throughputQueueBroadcast.setRemoved((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "Added":
                    throughputQueueBroadcast.setAdded((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "Throughput":
                    throughputQueueBroadcast
                            .setThroughput((double) server.getAttribute(name, attribute.getName()));
                    break;
                }
            } catch (Exception e) {
                LOGGER.error("Exception while trying to deserialize ThroughputQueueBroadcast object: {}", e);
            }
        }

        return throughputQueueBroadcast;
    } catch (Exception e) {
        return null;
    }
}

From source file:org.pentaho.metaverse.impl.model.kettle.json.TransMetaJsonDeserializer.java

@Override
public TransMeta deserialize(JsonParser parser, DeserializationContext context)
        throws IOException, JsonProcessingException {

    TransMeta transMeta = null;/*from   ww w.  j a v a2 s .co m*/
    JsonNode node = parser.getCodec().readTree(parser);

    ObjectMapper mapper = (ObjectMapper) parser.getCodec();

    String name = node.get(IInfo.JSON_PROPERTY_NAME).textValue();
    String desc = node.get(IInfo.JSON_PROPERTY_DESCRIPTION).textValue();

    String createdBy = node.get(TransMetaJsonSerializer.JSON_PROPERTY_CREATED_BY).textValue();
    String modifiedBy = node.get(TransMetaJsonSerializer.JSON_PROPERTY_LAST_MODIFIED_BY).textValue();
    Date createdDate = new Date(node.get(TransMetaJsonSerializer.JSON_PROPERTY_CREATED_DATE).asLong());
    Date modifiedDate = new Date(node.get(TransMetaJsonSerializer.JSON_PROPERTY_LAST_MODIFIED_DATE).asLong());
    String path = node.get(TransMetaJsonSerializer.JSON_PROPERTY_PATH).textValue();

    transMeta = new TransMeta(null, name);
    transMeta.setDescription(desc);

    transMeta.setCreatedDate(createdDate);
    transMeta.setCreatedUser(createdBy);
    transMeta.setModifiedDate(modifiedDate);
    transMeta.setModifiedUser(modifiedBy);
    transMeta.setFilename(path);

    // parameters
    deserializeParameters(transMeta, node, mapper);

    // variables
    deserializeVariables(transMeta, node, mapper);

    // connections
    deserializeConnections(transMeta, node, mapper);

    // steps
    deserializeSteps(transMeta, node, mapper);

    // hops
    deserializeHops(transMeta, node, mapper);

    return transMeta;

}

From source file:org.apache.streams.jackson.StreamsTaskCounterDeserializer.java

@Override
public StreamsTaskCounterBroadcast deserialize(JsonParser jsonParser,
        DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    try {//  w  w  w  .j a va2s.  c  o m
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        StreamsTaskCounterBroadcast streamsTaskCounterBroadcast = new StreamsTaskCounterBroadcast();
        JsonNode attributes = jsonParser.getCodec().readTree(jsonParser);

        ObjectName name = new ObjectName(attributes.get("canonicalName").asText());
        MBeanInfo info = server.getMBeanInfo(name);
        streamsTaskCounterBroadcast.setName(name.toString());

        for (MBeanAttributeInfo attribute : Arrays.asList(info.getAttributes())) {
            try {
                switch (attribute.getName()) {
                case "ErrorRate":
                    streamsTaskCounterBroadcast
                            .setErrorRate((double) server.getAttribute(name, attribute.getName()));
                    break;
                case "NumEmitted":
                    streamsTaskCounterBroadcast
                            .setNumEmitted((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "NumReceived":
                    streamsTaskCounterBroadcast
                            .setNumReceived((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "NumUnhandledErrors":
                    streamsTaskCounterBroadcast
                            .setNumUnhandledErrors((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "AvgTime":
                    streamsTaskCounterBroadcast
                            .setAvgTime((double) server.getAttribute(name, attribute.getName()));
                    break;
                case "MaxTime":
                    streamsTaskCounterBroadcast
                            .setMaxTime((long) server.getAttribute(name, attribute.getName()));
                    break;
                }
            } catch (Exception e) {
                LOGGER.error("Exception while trying to deserialize StreamsTaskCounterBroadcast object: {}", e);
            }
        }

        return streamsTaskCounterBroadcast;
    } catch (Exception e) {
        LOGGER.error("Exception while trying to deserialize StreamsTaskCounterBroadcast object: {}", e);
        return null;
    }
}

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());
    }//  w  w  w . j av  a  2 s. com

    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:com.msopentech.odatajclient.engine.data.metadata.edm.EntityKeyDeserializer.java

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

    final EntityKey entityKey = new EntityKey();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();

        if (token == JsonToken.FIELD_NAME && "PropertyRef".equals(jp.getCurrentName())) {
            jp.nextToken();//from  w w w .j  a va 2  s.  c  o  m
            entityKey.getPropertyRefs().add(jp.getCodec().readValue(jp, PropertyRef.class));
        }
    }

    return entityKey;
}

From source file:org.apache.syncope.core.provisioning.api.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<>();
    for (JsonNode node : tree.get("value")) {
        if (node.isNull()) {
            values.add(null);/* w  w  w.ja v  a2 s . co m*/
        } 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.getDecoder().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:com.msopentech.odatajclient.engine.data.json.JSONEntryDeserializer.java

/**
 * {@inheritDoc }/*w w w  .j  a  va2  s. co m*/
 */
@Override
public JSONEntry deserialize(final JsonParser parser, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

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

    if (tree.has(ODataConstants.JSON_VALUE) && tree.get(ODataConstants.JSON_VALUE).isArray()) {
        throw new JsonParseException("Expected OData Entity, found EntitySet", parser.getCurrentLocation());
    }

    final boolean isMediaEntry = tree.hasNonNull(ODataConstants.JSON_MEDIAREAD_LINK)
            && tree.hasNonNull(ODataConstants.JSON_MEDIA_CONTENT_TYPE);

    final JSONEntry entry = new JSONEntry();

    if (tree.hasNonNull(ODataConstants.JSON_METADATA)) {
        entry.setMetadata(URI.create(tree.get(ODataConstants.JSON_METADATA).textValue()));
        tree.remove(ODataConstants.JSON_METADATA);
    }

    if (tree.hasNonNull(ODataConstants.JSON_MEDIA_ETAG)) {
        entry.setMediaETag(tree.get(ODataConstants.JSON_MEDIA_ETAG).textValue());
        tree.remove(ODataConstants.JSON_MEDIA_ETAG);
    }

    if (tree.hasNonNull(ODataConstants.JSON_ETAG)) {
        entry.setETag(tree.get(ODataConstants.JSON_ETAG).textValue());
        tree.remove(ODataConstants.JSON_ETAG);
    }

    if (tree.hasNonNull(ODataConstants.JSON_TYPE)) {
        entry.setType(tree.get(ODataConstants.JSON_TYPE).textValue());
        tree.remove(ODataConstants.JSON_TYPE);
    }

    if (tree.hasNonNull(ODataConstants.JSON_ID)) {
        entry.setId(tree.get(ODataConstants.JSON_ID).textValue());
        tree.remove(ODataConstants.JSON_ID);
    }

    if (tree.hasNonNull(ODataConstants.JSON_READ_LINK)) {
        final JSONLink link = new JSONLink();
        link.setRel(ODataConstants.SELF_LINK_REL);
        link.setHref(tree.get(ODataConstants.JSON_READ_LINK).textValue());
        entry.setSelfLink(link);

        tree.remove(ODataConstants.JSON_READ_LINK);
    }

    if (tree.hasNonNull(ODataConstants.JSON_EDIT_LINK)) {
        final JSONLink link = new JSONLink();
        link.setRel(ODataConstants.EDIT_LINK_REL);
        link.setHref(tree.get(ODataConstants.JSON_EDIT_LINK).textValue());
        entry.setEditLink(link);

        tree.remove(ODataConstants.JSON_EDIT_LINK);
    }

    if (tree.hasNonNull(ODataConstants.JSON_MEDIAREAD_LINK)) {
        entry.setMediaContentSource(tree.get(ODataConstants.JSON_MEDIAREAD_LINK).textValue());
        tree.remove(ODataConstants.JSON_MEDIAREAD_LINK);
    }
    if (tree.hasNonNull(ODataConstants.JSON_MEDIAEDIT_LINK)) {
        final JSONLink link = new JSONLink();
        link.setHref(tree.get(ODataConstants.JSON_MEDIAEDIT_LINK).textValue());
        entry.addMediaEditLink(link);

        tree.remove(ODataConstants.JSON_MEDIAEDIT_LINK);
    }
    if (tree.hasNonNull(ODataConstants.JSON_MEDIA_CONTENT_TYPE)) {
        entry.setMediaContentType(tree.get(ODataConstants.JSON_MEDIA_CONTENT_TYPE).textValue());
        tree.remove(ODataConstants.JSON_MEDIA_CONTENT_TYPE);
    }

    final Set<String> toRemove = new HashSet<String>();
    final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields();
    while (itor.hasNext()) {
        final Map.Entry<String, JsonNode> field = itor.next();

        if (field.getKey().endsWith(ODataConstants.JSON_NAVIGATION_LINK_SUFFIX)
                || field.getKey().endsWith(ODataConstants.JSON_NAVIGATION_LINK_ODATA_4_SUFFIX)) {
            final JSONLink link = new JSONLink();
            link.setTitle(getTitle(field));
            link.setRel(ODataConstants.NAVIGATION_LINK_REL + getTitle(field));
            if (field.getValue().isValueNode()) {
                link.setHref(field.getValue().textValue());
                link.setType(ODataLinkType.ENTITY_NAVIGATION.toString());
            }
            // NOTE: this should be expected to happen, but it isn't - at least up to OData 4.0
            /* if (field.getValue().isArray()) {
             * link.setHref(field.getValue().asText());
             * link.setType(ODataLinkType.ENTITY_SET_NAVIGATION.toString());
             * } */
            entry.addNavigationLink(link);

            toRemove.add(field.getKey());
            toRemove.add(setInline(field.getKey(),
                    field.getKey().endsWith(ODataConstants.JSON_NAVIGATION_LINK_SUFFIX)
                            ? ODataConstants.JSON_NAVIGATION_LINK_SUFFIX
                            : ODataConstants.JSON_NAVIGATION_LINK_ODATA_4_SUFFIX,
                    tree, parser.getCodec(), link));
        } else if (field.getKey().endsWith(ODataConstants.JSON_ASSOCIATION_LINK_SUFFIX)) {
            final JSONLink link = new JSONLink();
            link.setTitle(getTitle(field));
            link.setRel(ODataConstants.ASSOCIATION_LINK_REL + getTitle(field));
            link.setHref(field.getValue().textValue());
            link.setType(ODataLinkType.ASSOCIATION.toString());
            entry.addAssociationLink(link);

            toRemove.add(field.getKey());
        } else if (field.getKey().endsWith(ODataConstants.JSON_MEDIAEDIT_LINK_SUFFIX)) {
            final JSONLink link = new JSONLink();
            link.setTitle(getTitle(field));
            link.setRel(ODataConstants.MEDIA_EDIT_LINK_REL + getTitle(field));
            link.setHref(field.getValue().textValue());
            link.setType(ODataLinkType.MEDIA_EDIT.toString());
            entry.addMediaEditLink(link);

            toRemove.add(field.getKey());
            toRemove.add(setInline(field.getKey(), ODataConstants.JSON_MEDIAEDIT_LINK_SUFFIX, tree,
                    parser.getCodec(), link));
        } 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(ODataConstants.ATTR_TITLE).asText());
            operation.setTarget(URI.create(opNode.get(ODataConstants.ATTR_TARGET).asText()));

            entry.addOperation(operation);

            toRemove.add(field.getKey());
        }
    }
    tree.remove(toRemove);

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

        final Element properties = document.createElementNS(ODataConstants.NS_METADATA,
                ODataConstants.ELEM_PROPERTIES);

        DOMTreeUtils.buildSubtree(properties, tree);

        if (isMediaEntry) {
            entry.setMediaEntryProperties(properties);
        } else {
            entry.setContent(properties);
        }
    } catch (ParserConfigurationException e) {
        throw new JsonParseException("Cannot build entry content", parser.getCurrentLocation(), e);
    }

    return entry;
}

From source file:io.syndesis.model.integration.StepDeserializer.java

@Override
public Step deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    ObjectNode node = jp.readValueAsTree();
    JsonNode stepKind = node.get(STEP_KIND);
    if (stepKind != null) {
        String value = stepKind.textValue();
        Class<? extends Step> resourceType = getTypeForName(value);
        if (resourceType == null) {
            throw ctxt.mappingException("No step type found for kind:" + value);
        } else {// www .  ja v a 2 s  . c om
            return jp.getCodec().treeToValue(node, resourceType);
        }
    }
    return null;
}