Example usage for com.fasterxml.jackson.databind JsonNode longValue

List of usage examples for com.fasterxml.jackson.databind JsonNode longValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonNode longValue.

Prototype

public long longValue() 

Source Link

Usage

From source file:com.github.fge.jsonschema.keyword.validator.common.MinimumValidator.java

@Override
protected void validateLong(final ProcessingReport report, final MessageBundle bundle, final FullData data)
        throws ProcessingException {
    final JsonNode instance = data.getInstance().getNode();
    final long instanceValue = instance.longValue();
    final long longValue = number.longValue();

    if (instanceValue > longValue)
        return;/*from  www .jav  a  2 s  .  c o  m*/

    if (instanceValue < longValue) {
        report.error(newMsg(data, bundle, "err.common.minimum.tooSmall").putArgument(keyword, number)
                .putArgument("found", instance));
        return;
    }

    if (!exclusive)
        return;

    report.error(newMsg(data, bundle, "err.common.minimum.notExclusive").putArgument(keyword, number)
            .put("exclusiveMinimum", BooleanNode.TRUE));
}

From source file:org.agorava.twitter.jackson.TweetDeserializer.java

@Override
public Tweet deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonNode tree = jp.readValueAsTree();
    long id = tree.get("id").asLong();
    String text = tree.get("text").asText();
    JsonNode fromUserNode = tree.get("user");
    String fromScreenName = null;
    long fromId = 0;
    String fromImageUrl = null;/*from w  ww. j ava  2s  .c o  m*/
    String dateFormat = TIMELINE_DATE_FORMAT;
    if (fromUserNode != null) {
        fromScreenName = fromUserNode.get("screen_name").asText();
        fromId = fromUserNode.get("id").asLong();
        fromImageUrl = fromUserNode.get("profile_image_url").asText();
    } else {
        fromScreenName = tree.get("from_user").asText();
        fromId = tree.get("from_user_id").asLong();
        fromImageUrl = tree.get("profile_image_url").asText();
        dateFormat = SEARCH_DATE_FORMAT;
    }
    Date createdAt = toDate(tree.get("created_at").asText(), new SimpleDateFormat(dateFormat, Locale.ENGLISH));
    String source = tree.get("source").asText();
    JsonNode toUserIdNode = tree.get("in_reply_to_user_id");
    Long toUserId = toUserIdNode != null ? toUserIdNode.longValue() : null;
    JsonNode languageCodeNode = tree.get("iso_language_code");
    String languageCode = languageCodeNode != null ? languageCodeNode.asText() : null;
    Tweet tweet = new Tweet(id, text, createdAt, fromScreenName, fromImageUrl, toUserId, fromId, languageCode,
            source);
    JsonNode inReplyToStatusIdNode = tree.get("in_reply_to_status_id");
    Long inReplyToStatusId = inReplyToStatusIdNode != null && !inReplyToStatusIdNode.isNull()
            ? inReplyToStatusIdNode.longValue()
            : null;
    tweet.setInReplyToStatusId(inReplyToStatusId);
    JsonNode retweetCountNode = tree.get("retweet_count");
    Integer retweetCount = retweetCountNode != null && !retweetCountNode.isNull() ? retweetCountNode.intValue()
            : null;
    tweet.setRetweetCount(retweetCount);
    JsonNode favoritedNode = tree.get("favorited");
    boolean favorited = favoritedNode != null && !favoritedNode.isNull() ? favoritedNode.booleanValue() : false;
    tweet.setFavorited(favorited);
    jp.skipChildren();
    return tweet;
}

From source file:com.netflix.zeno.json.JsonFrameworkDeserializer.java

@Override
public Long deserializeLong(JsonReadGenericRecord record, String fieldName) {
    JsonNode node = record.getNode().isNumber() ? record.getNode() : getJsonNode(record, fieldName);
    if (node == null)
        return null;
    return node.longValue();
}

From source file:com.github.fge.jsonschema.keyword.digest.helpers.NumericDigester.java

protected final ObjectNode digestedNumberNode(final JsonNode schema) {
    final ObjectNode ret = FACTORY.objectNode();

    final JsonNode node = schema.get(keyword);
    final boolean isLong = valueIsLong(node);
    ret.put("valueIsLong", isLong);

    if (isLong) {
        ret.put(keyword, node.canConvertToInt() ? FACTORY.numberNode(node.intValue())
                : FACTORY.numberNode(node.longValue()));
        return ret;
    }//from   ww  w  . j  a v a 2  s.co m

    final BigDecimal decimal = node.decimalValue();
    ret.put(keyword, decimal.scale() == 0 ? FACTORY.numberNode(decimal.toBigIntegerExact()) : node);

    return ret;
}

From source file:com.github.restdriver.matchers.HasJsonValue.java

@Override
public boolean matchesSafely(JsonNode jsonNode) {

    JsonNode node = jsonNode.get(fieldName);

    if (node == null) {
        return false;
    }//from   ww  w .  jav  a  2  s .  c om

    if (node.isInt()) {
        return valueMatcher.matches(node.intValue());

    } else if (node.isLong()) {
        return valueMatcher.matches(node.longValue());

    } else if (node.isTextual()) {
        return valueMatcher.matches(node.textValue());

    } else if (node.isBoolean()) {
        return valueMatcher.matches(node.booleanValue());

    } else if (node.isDouble()) {
        return valueMatcher.matches(node.doubleValue());

    } else if (node.isObject()) {
        return valueMatcher.matches(node);

    } else if (node.isNull()) {
        return valueMatcher.matches(null);

    } else {
        return false;

    }

}

From source file:com.datamountaineer.streamreactor.connect.json.SimpleJsonConverterTest.java

@Test
public void longToJson() {
    JsonNode converted = converter.fromConnectData(Schema.INT64_SCHEMA, 4398046511104L);
    assertEquals(4398046511104L, converted.longValue());
}

From source file:com.amazonaws.services.iot.client.shadow.AwsIotDeviceDeltaListener.java

@Override
public void onMessage(AWSIotMessage message) {
    String payload = message.getStringPayload();
    if (payload == null) {
        LOGGER.warning("Received empty delta for device " + device.getThingName());
        return;//  www. java2s  .  c  o m
    }

    JsonNode rootNode;
    try {
        rootNode = device.getJsonObjectMapper().readTree(payload);
        if (!rootNode.isObject()) {
            throw new IOException();
        }
    } catch (IOException e) {
        LOGGER.warning("Received invalid delta for device " + device.getThingName());
        return;
    }

    if (device.enableVersioning) {
        JsonNode node = rootNode.get("version");
        if (node == null) {
            LOGGER.warning("Missing version field in delta for device " + device.getThingName());
            return;
        }

        long receivedVersion = node.longValue();
        long localVersion = device.getLocalVersion().get();
        if (receivedVersion < localVersion) {
            LOGGER.warning("An old version of delta received for " + device.getThingName() + ", local "
                    + localVersion + ", received " + receivedVersion);
            return;
        }

        device.getLocalVersion().set(receivedVersion);
        LOGGER.info("Local version number updated to " + receivedVersion);
    }

    JsonNode node = rootNode.get("state");
    if (node == null) {
        LOGGER.warning("Missing state field in delta for device " + device.getThingName());
        return;
    }
    device.onShadowUpdate(node.toString());
}

From source file:com.datamountaineer.streamreactor.connect.json.SimpleJsonConverterTest.java

@Test
public void timestampToJson() throws IOException {
    GregorianCalendar calendar = new GregorianCalendar(1970, Calendar.JANUARY, 1, 0, 0, 0);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.add(Calendar.MILLISECOND, 2000000000);
    calendar.add(Calendar.MILLISECOND, 2000000000);
    java.util.Date date = calendar.getTime();

    JsonNode converted = converter.fromConnectData(Timestamp.SCHEMA, date);
    assertTrue(converted.isLong());//from ww  w.jav a 2s .c  o m
    assertEquals(4000000000L, converted.longValue());
}

From source file:com.neovisionaries.security.JsonDigestUpdater.java

private boolean isZero(JsonNode value) {
    // int/*  ww  w.  j  a va  2s. c o  m*/
    if (value.isInt() && value.intValue() == 0) {
        return true;
    }

    // long
    if (value.isLong() && value.longValue() == 0) {
        return true;
    }

    // short
    if (value.isShort() && value.shortValue() == 0) {
        return true;
    }

    // float
    if (value.isFloat() && value.floatValue() == 0.0F) {
        return true;
    }

    // double
    if (value.isDouble() && value.doubleValue() == 0.0) {
        return true;
    }

    // BigInteger
    if (value.isBigInteger() && value.bigIntegerValue().equals(BigInteger.ZERO)) {
        return true;
    }

    // BigDecimal
    if (value.isBigDecimal() && value.decimalValue().equals(BigDecimal.ZERO)) {
        return true;
    }

    return false;
}

From source file:org.activiti.rest.common.api.SecuredResource.java

protected Map<String, Object> retrieveVariables(JsonNode jsonNode) {
    Map<String, Object> variables = new HashMap<String, Object>();
    if (jsonNode != null) {
        Iterator<String> itName = jsonNode.fieldNames();
        while (itName.hasNext()) {
            String name = itName.next();
            JsonNode valueNode = jsonNode.path(name);
            if (valueNode.isBoolean()) {
                variables.put(name, valueNode.booleanValue());
            } else if (valueNode.isInt()) {
                variables.put(name, valueNode.intValue());
            } else if (valueNode.isLong()) {
                variables.put(name, valueNode.longValue());
            } else if (valueNode.isDouble()) {
                variables.put(name, valueNode.doubleValue());
            } else if (valueNode.isTextual()) {
                variables.put(name, valueNode.textValue());
            } else {
                // Not using asText() due to the fact we expect a null-value to be returned rather than en emtpy string
                // when node is not a simple value-node
                variables.put(name, valueNode.textValue());
            }/*from   w  w w .  j  a v a 2 s.co m*/
        }
    }
    return variables;
}