Example usage for com.fasterxml.jackson.databind.node JsonNodeType NULL

List of usage examples for com.fasterxml.jackson.databind.node JsonNodeType NULL

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node JsonNodeType NULL.

Prototype

JsonNodeType NULL

To view the source code for com.fasterxml.jackson.databind.node JsonNodeType NULL.

Click Source Link

Usage

From source file:com.spotify.hamcrest.jackson.IsJsonNull.java

private IsJsonNull() {
    super(JsonNodeType.NULL);
}

From source file:org.dswarm.graph.json.deserializer.StatementDeserializer.java

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

    final ObjectCodec oc = jp.getCodec();

    if (oc == null) {

        return null;
    }//ww  w. j a  va  2s  . c o  m

    final JsonNode node = oc.readTree(jp);

    if (node == null) {

        return null;
    }

    final JsonNode idNode = node.get("id");

    Long id = null;

    if (idNode != null) {

        try {

            id = idNode.asLong();
        } catch (final Exception e) {

            id = null;
        }
    }

    final JsonNode uuidNode = node.get("uuid");

    String uuid = null;

    if (uuidNode != null && JsonNodeType.NULL != uuidNode.getNodeType()) {

        uuid = uuidNode.asText();
    }

    final JsonNode subjectNode = node.get("s");

    if (subjectNode == null) {

        throw new JsonParseException("expected JSON node that represents the subject of a statement",
                jp.getCurrentLocation());
    }

    final Node subject;

    if (subjectNode.get("uri") != null) {

        // resource node
        subject = subjectNode.traverse(oc).readValueAs(ResourceNode.class);
    } else {

        // bnode
        subject = subjectNode.traverse(oc).readValueAs(Node.class);
    }

    final JsonNode predicateNode = node.get("p");

    if (predicateNode == null) {

        throw new JsonParseException("expected JSON node that represents the predicate of a statement",
                jp.getCurrentLocation());
    }

    final Predicate predicate = predicateNode.traverse(oc).readValueAs(Predicate.class);

    final JsonNode objectNode = node.get("o");

    if (objectNode == null) {

        throw new JsonParseException("expected JSON node that represents the object of a statement",
                jp.getCurrentLocation());
    }

    final Node object;

    if (objectNode.get("uri") != null) {

        // resource node
        object = objectNode.traverse(oc).readValueAs(ResourceNode.class);
    } else if (objectNode.get("v") != null) {

        // literal node
        object = objectNode.traverse(oc).readValueAs(LiteralNode.class);
    } else {

        // bnode
        object = objectNode.traverse(oc).readValueAs(Node.class);
    }

    final JsonNode orderNode = node.get("order");

    Long order = null;

    if (orderNode != null) {

        try {

            order = orderNode.asLong();
        } catch (final Exception e) {

            order = null;
        }
    }

    final JsonNode evidenceNode = node.get("evidence");

    String evidence = null;

    if (evidenceNode != null) {

        evidence = evidenceNode.asText();
    }

    final JsonNode confidenceNode = node.get("confidence");

    String confidence = null;

    if (confidenceNode != null) {

        confidence = confidenceNode.asText();
    }

    final Statement statement = new Statement(subject, predicate, object);

    if (id != null) {

        statement.setId(id);
    }

    if (uuid != null) {

        statement.setUUID(uuid);
    }

    if (order != null) {

        statement.setOrder(order);
    }

    if (evidence != null) {

        statement.setEvidence(evidence);
    }

    if (confidence != null) {

        statement.setConfidence(confidence);
    }

    return statement;
}

From source file:org.apache.airavata.db.AbstractThriftDeserializer.java

@Override
public T deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    final T instance = newInstance();
    final ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    final ObjectNode rootNode = mapper.readTree(jp);
    final Iterator<Map.Entry<String, JsonNode>> iterator = rootNode.fields();

    while (iterator.hasNext()) {
        final Map.Entry<String, JsonNode> currentField = iterator.next();
        try {/*from  www  .  j av  a 2  s . c  om*/
            /*
             * If the current node is not a null value, process it.  Otherwise,
             * skip it.  Jackson will treat the null as a 0 for primitive
             * number types, which in turn will make Thrift think the field
             * has been set. Also we ignore the MongoDB specific _id field
             */
            if (!currentField.getKey().equalsIgnoreCase("_id")
                    && currentField.getValue().getNodeType() != JsonNodeType.NULL) {
                final E field = getField(
                        CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, currentField.getKey()));
                final JsonParser parser = currentField.getValue().traverse();
                parser.setCodec(mapper);
                final Object value = mapper.readValue(parser, generateValueType(instance, field));
                if (value != null) {
                    log.debug(String.format("Field %s produced value %s of type %s.", currentField.getKey(),
                            value, value.getClass().getName()));
                    instance.setFieldValue(field, value);
                } else {
                    log.debug("Field {} contains a null value.  Skipping...", currentField.getKey());
                }
            } else {
                log.debug("Field {} contains a null value.  Skipping...", currentField.getKey());
            }
        } catch (final NoSuchFieldException | IllegalArgumentException e) {
            log.error("Unable to de-serialize field '{}'.", currentField.getKey(), e);
            ctxt.mappingException(e.getMessage());
        }
    }

    try {
        // Validate that the instance contains all required fields.
        validate(instance);
    } catch (final TException e) {
        log.error(String.format("Unable to deserialize JSON '%s' to type '%s'.", jp.getValueAsString(),
                instance.getClass().getName(), e));
        ctxt.mappingException(e.getMessage());
    }

    return instance;
}

From source file:de.thingweb.desc.ThingDescriptionParser.java

public static ObjectNode toJsonObject(Thing thing) {

    ObjectNode td = factory.objectNode();
    if (thing.getMetadata().get("@context") == null
            || thing.getMetadata().get("@context").getNodeType() == JsonNodeType.NULL) {
        td.put("@context", factory.textNode(WOT_TD_CONTEXT));
    } else {/*w  w  w  .j av a2s.co m*/
        td.put("@context", thing.getMetadata().get("@context"));
    }
    td.put("name", thing.getName());
    if (thing.getMetadata().contains("@type")) {
        td.put("@type", thing.getMetadata().get("@type"));
    }

    if (thing.getMetadata().contains("security")) {
        td.put("security", thing.getMetadata().get("security"));
    }

    if (thing.getMetadata().contains("encodings")) {
        // ArrayNode encodings = factory.arrayNode();
        // for (String e : thing.getMetadata().getAll("encodings")) {
        // encodings.add(e);
        // }
        td.put("encodings", thing.getMetadata().get("encodings"));
    }

    if (thing.getMetadata().contains("uris")) {
        //          ArrayNode uris = factory.arrayNode();
        //          for (JsonNode uri : thing.getMetadata().getAll("uris")) {
        //             uris.add(uri);
        //          }
        // // TODO array even if single value?
        // td.put("uris", uris);
        td.put("uris", thing.getMetadata().get("uris"));
    }

    ArrayNode properties = factory.arrayNode();
    for (Property prop : thing.getProperties()) {
        ObjectNode p = factory.objectNode();
        if (prop.getPropertyType() != null && prop.getPropertyType().length() > 0) {
            p.put("@type", prop.getPropertyType());
        }
        p.put("name", prop.getName());
        p.put("writable", prop.isWritable());
        p.put("valueType", prop.getValueType());

        if (prop.getHrefs().size() > 1) {
            ArrayNode hrefs = factory.arrayNode();
            for (String href : prop.getHrefs()) {
                hrefs.add(href);
            }
            p.put("hrefs", hrefs);
        } else if (prop.getHrefs().size() == 1) {
            p.put("hrefs", factory.textNode(prop.getHrefs().get(0)));
        }
        if (prop.getStability() != null) {
            p.put("stability", prop.getStability());
        }

        properties.add(p);
    }
    td.put("properties", properties);

    ArrayNode actions = factory.arrayNode();
    for (Action action : thing.getActions()) {
        ObjectNode a = factory.objectNode();
        if (action.getActionType() != null && action.getActionType().length() > 0) {
            a.put("@type", action.getActionType());
        }
        a.put("name", action.getName());

        if (action.getInputType() != null) {
            ObjectNode in = factory.objectNode();
            in.put("valueType", action.getInputType());
            a.put("inputData", in);
        }

        if (action.getOutputType() != null) {
            ObjectNode out = factory.objectNode();
            out.put("valueType", action.getOutputType());
            a.put("outputData", out);
        }

        if (action.getHrefs().size() > 1) {
            ArrayNode hrefs = factory.arrayNode();
            for (String href : action.getHrefs()) {
                hrefs.add(href);
            }
            a.put("hrefs", hrefs);
        } else if (action.getHrefs().size() == 1) {
            a.put("hrefs", factory.textNode(action.getHrefs().get(0)));
        }

        actions.add(a);
    }
    td.put("actions", actions);

    ArrayNode events = factory.arrayNode();
    for (Event event : thing.getEvents()) {
        ObjectNode a = factory.objectNode();
        if (event.getEventType() != null && event.getEventType().length() > 0) {
            a.put("@type", event.getEventType());
        }
        a.put("name", event.getName());

        if (event.getValueType() != null) {
            a.put("valueType", event.getValueType());
        }

        if (event.getHrefs().size() > 1) {
            ArrayNode hrefs = factory.arrayNode();
            for (String href : event.getHrefs()) {
                hrefs.add(href);
            }
            a.put("hrefs", hrefs);
        } else if (event.getHrefs().size() == 1) {
            a.put("hrefs", factory.textNode(event.getHrefs().get(0)));
        }

        events.add(a);
    }
    td.put("events", events);

    return td;
}

From source file:de.thingweb.desc.ThingDescriptionParser.java

private static Thing parse(JsonNode td) throws Exception {
    // ProcessingReport report =
    // JsonSchemaFactory.byDefault().getValidator().validate(TD_SCHEMA, td);
    // if (!report.isSuccess()) {
    // throw new IOException("JSON data not valid");
    // }//from w ww. ja va 2s  .  c om

    Thing thing = new Thing(td.get("name").asText());

    Iterator<String> tdIterator = td.fieldNames();
    while (tdIterator.hasNext()) {
        switch (tdIterator.next()) {
        case "@context":
            if (td.get("@context") == null || td.get("@context").getNodeType() == JsonNodeType.NULL) {
                thing.getMetadata().add("@context", factory.textNode(WOT_TD_CONTEXT));
            } else {
                thing.getMetadata().add("@context", td.get("@context"));
            }
            break;
        case "uris":
            thing.getMetadata().add("uris", td.get("uris"));
            break;
        case "@type":
            thing.getMetadata().add("@type", td.get("@type"));
            break;
        case "security":
            thing.getMetadata().add("security", td.get("security"));
            break;

        case "properties":
            for (JsonNode prop : td.get("properties")) {
                Property.Builder builder = Property.getBuilder(prop.get("name").asText());
                Iterator<String> it = prop.fieldNames();
                while (it.hasNext()) {
                    switch (it.next()) {
                    case "valueType":
                        JsonNode jn = prop.get("valueType");
                        builder.setValueType(jn);
                        break;
                    case "@type":
                        builder.setPropertyType(prop.get("@type").asText());
                        break;
                    case "writable":
                        builder.setWriteable(prop.get("writable").asBoolean());
                        break;
                    case "hrefs":
                        builder.setHrefs(stringOrArray(prop.get("hrefs")));
                        break;
                    case "security":
                        builder.setSecurity(prop.get("security"));
                        break;
                    case "stability":
                        builder.setStability(prop.get("stability").asInt());
                        break;
                    }
                }
                thing.addProperty(builder.build());
            }
            break;

        case "actions":
            for (JsonNode action : td.get("actions")) {
                Action.Builder builder = Action.getBuilder(action.get("name").asText());
                Iterator<String> it = action.fieldNames();
                while (it.hasNext()) {
                    switch (it.next()) {
                    case "inputData":
                        JsonNode jnI = action.get("inputData").get("valueType");
                        builder.setInputType(jnI);
                        break;
                    case "outputData":
                        JsonNode jnO = action.get("outputData").get("valueType");
                        builder.setOutputType(jnO);
                        break;
                    case "@type":
                        builder.setActionType(action.get("@type").asText());
                        break;
                    case "hrefs":
                        builder.setHrefs(stringOrArray(action.get("hrefs")));
                        break;
                    case "security":
                        builder.setSecurity(action.get("security"));
                        break;
                    }
                }
                thing.addAction(builder.build());
            }
            break;

        case "events":
            for (JsonNode event : td.get("events")) {
                Event.Builder builder = Event.getBuilder(event.get("name").asText());
                Iterator<String> it = event.fieldNames();
                while (it.hasNext()) {
                    switch (it.next()) {
                    case "valueType":
                        JsonNode jn = event.get("valueType");
                        builder.setValueType(jn);
                        break;
                    case "@type":
                        builder.setEventType(event.get("@type").asText());
                        break;
                    case "hrefs":
                        builder.setHrefs(stringOrArray(event.get("hrefs")));
                        break;
                    case "security":
                        builder.setSecurity(event.get("security"));
                        break;
                    }
                }
                thing.addEvent(builder.build());
            }
            break;

        case "encodings":
            thing.getMetadata().add("encodings", td.get("encodings"));
            break;
        }
    }

    return thing;
}