Java Code Examples for com.fasterxml.jackson.databind.JsonNode#longValue()

The following examples show how to use com.fasterxml.jackson.databind.JsonNode#longValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EqualsComparison.java    From jslt with Apache License 2.0 6 votes vote down vote up
static boolean equals(JsonNode v1, JsonNode v2) {
  boolean result;
  if (v1.isNumber() && v2.isNumber()) {
    // unfortunately, comparison of numeric nodes in Jackson is
    // deliberately less helpful than what we need here. so we have
    // to develop our own support for it.
    // https://github.com/FasterXML/jackson-databind/issues/1758

    if (v1.isIntegralNumber() && v2.isIntegralNumber())
      // if both are integers, then compare them as such
      return v1.longValue() == v2.longValue();
    else
      return v1.doubleValue() == v2.doubleValue();
  } else
    return v1.equals(v2);
}
 
Example 2
Source File: DivideOperator.java    From jslt with Apache License 2.0 6 votes vote down vote up
public JsonNode perform(JsonNode v1, JsonNode v2) {
  if (v1.isNull() || v2.isNull())
    return NullNode.instance;

  // we only support the numeric operation and nothing else
  v1 = NodeUtils.number(v1, true, location);
  v2 = NodeUtils.number(v2, true, location);

  if (v1.isIntegralNumber() && v2.isIntegralNumber()) {
    long l1 = v1.longValue();
    long l2 = v2.longValue();
    if (l1 % l2 == 0)
      return new LongNode(l1 / l2);
    else
      return new DoubleNode((double) l1 / (double) l2);
  } else
    return new DoubleNode(perform(v1.doubleValue(), v2.doubleValue()));
}
 
Example 3
Source File: JsonConfigProvider.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public Long getLong(String key, Long defaultValue) {
    if (config == null) {
        return defaultValue;
    }
    JsonNode n = config.get(key);
    if (n == null) {
        return defaultValue;
    }
    if (n.isTextual()) {
        String v = replaceProperties(n.textValue());
        return !v.isEmpty() ? Long.parseLong(v) : defaultValue;
    } else {
        return n.longValue();
    }
}
 
Example 4
Source File: VariableContainsExpressionFunction.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
public static boolean arrayNodeContains(ArrayNode arrayNode, Object value) {
    Iterator<JsonNode> iterator = arrayNode.iterator();
    while (iterator.hasNext()) {
        JsonNode jsonNode = iterator.next();
        if (value == null && jsonNode.isNull()) {
            return true;
        } else if (value != null) {
            if (value instanceof String && jsonNode.isTextual() && StringUtils.equals(jsonNode.asText(), (String) value)) {
                return true;
            } else if (value instanceof Number && jsonNode.isLong() && jsonNode.longValue() == ((Number) value).longValue()) {
                return true;
            } else if (value instanceof Number && jsonNode.isDouble() && jsonNode.doubleValue() == ((Number) value).doubleValue()) {
                return true;
            } else if (value instanceof Number && jsonNode.isInt() && jsonNode.intValue() == ((Number) value).intValue()) {
                return true;
            } else if (value instanceof Boolean && jsonNode.isBoolean() && jsonNode.booleanValue() == ((Boolean) value).booleanValue()) {
                return true;
            }
        }
    }   
    return false;
}
 
Example 5
Source File: Int64TypeParser.java    From connect-utils with Apache License 2.0 6 votes vote down vote up
@Override
public Object parseJsonNode(JsonNode input, Schema schema) {
  Object result;
  if (input.isNumber()) {
    result = input.longValue();
  } else if (input.isTextual()) {
    result = parseString(input.textValue(), schema);
  } else {
    throw new UnsupportedOperationException(
        String.format(
            "Could not parse '%s' to %s",
            input,
            this.expectedClass().getSimpleName()
        )
    );
  }
  return result;
}
 
Example 6
Source File: LongIOProvider.java    From constellation with Apache License 2.0 5 votes vote down vote up
@Override
public void readObject(final int attributeId, final int elementId, final JsonNode jnode, 
        final GraphWriteMethods graph, final Map<Integer, Integer> vertexMap, final Map<Integer, Integer> transactionMap, 
        final GraphByteReader byteReader, final ImmutableObjectCache cache) throws IOException {
    final long attributeValue = jnode.longValue();
    graph.setLongValue(attributeId, elementId, attributeValue);
}
 
Example 7
Source File: BatchRequestBuilder.java    From simple-json-rpc with MIT License 5 votes vote down vote up
@NotNull
private static Object nodeValue(@NotNull JsonNode id) {
    if (id.isLong()) {
        return id.longValue();
    } else if (id.isInt()) {
        return id.intValue();
    } else if (id.isTextual()) {
        return id.textValue();
    }
    throw new IllegalArgumentException("Wrong id=" + id);
}
 
Example 8
Source File: JsonEventConventions.java    From tasmo with Apache License 2.0 5 votes vote down vote up
public long getEventId(ObjectNode eventNode) {
    JsonNode got = eventNode.get(ReservedFields.EVENT_ID);
    if (got == null || got.isNull()) {
        return 0L;
    }
    try {
        return got.longValue();
    } catch (Exception ex) {
        LOG.debug("Failed to get eventId: " + eventNode, ex);
        return 0L;
    }
}
 
Example 9
Source File: JsonExampleDeserializer.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
private Example createExample(JsonNode node) {
    if (node instanceof ObjectNode) {
        ObjectExample obj = new ObjectExample();
        ObjectNode on = (ObjectNode) node;
        for (Iterator<Entry<String, JsonNode>> x = on.fields(); x.hasNext(); ) {
            Entry<String, JsonNode> i = x.next();
            String key = i.getKey();
            JsonNode value = i.getValue();
            obj.put(key, createExample(value));
        }
        return obj;
    } else if (node instanceof ArrayNode) {
        ArrayExample arr = new ArrayExample();
        ArrayNode an = (ArrayNode) node;
        for (JsonNode childNode : an) {
            arr.add(createExample(childNode));
        }
        return arr;
    } else if (node instanceof DoubleNode) {
        return new DoubleExample(node.doubleValue());
    } else if (node instanceof IntNode || node instanceof ShortNode) {
        return new IntegerExample(node.intValue());
    } else if (node instanceof FloatNode) {
        return new FloatExample(node.floatValue());
    } else if (node instanceof BigIntegerNode) {
        return new BigIntegerExample(node.bigIntegerValue());
    } else if (node instanceof DecimalNode) {
        return new DecimalExample(node.decimalValue());
    } else if (node instanceof LongNode) {
        return new LongExample(node.longValue());
    } else if (node instanceof BooleanNode) {
        return new BooleanExample(node.booleanValue());
    } else {
        return new StringExample(node.asText());
    }
}
 
Example 10
Source File: AtlasGraphSONUtility.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
private static Object getTypedValueFromJsonNode(JsonNode node) {
    Object theValue = null;

    if (node != null && !node.isNull()) {
        if (node.isBoolean()) {
            theValue = node.booleanValue();
        } else if (node.isDouble()) {
            theValue = node.doubleValue();
        } else if (node.isFloatingPointNumber()) {
            theValue = node.floatValue();
        } else if (node.isInt()) {
            theValue = node.intValue();
        } else if (node.isLong()) {
            theValue = node.longValue();
        } else if (node.isTextual()) {
            theValue = node.textValue();
        } else if (node.isArray()) {
            // this is an array so just send it back so that it can be
            // reprocessed to its primitive components
            theValue = node;
        } else if (node.isObject()) {
            // this is an object so just send it back so that it can be
            // reprocessed to its primitive components
            theValue = node;
        } else {
            theValue = node.textValue();
        }
    }

    return theValue;
}
 
Example 11
Source File: JsonFieldToMapPayloadExtractor.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected Object getPayloadValue(JsonNode event, String definitionName, String definitionType) {
    JsonNode parameterNode = event.get(definitionName);
    Object value = null;

    if (EventPayloadTypes.STRING.equals(definitionType)) {
        value = parameterNode.asText();

    } else if (EventPayloadTypes.BOOLEAN.equals(definitionType)) {
        value = parameterNode.booleanValue();

    } else if (EventPayloadTypes.INTEGER.equals(definitionType)) {
        value = parameterNode.intValue();

    } else if (EventPayloadTypes.DOUBLE.equals(definitionType)) {
        value = parameterNode.doubleValue();

    } else if (EventPayloadTypes.LONG.equals(definitionType)) {
      value = parameterNode.longValue();

    } else if (EventPayloadTypes.JSON.equals(definitionType)) {
        value = parameterNode;

    } else {
        LOGGER.warn("Unsupported payload type: {} ", definitionType);
        value = parameterNode.asText();

    }

    return value;
}
 
Example 12
Source File: WebSocketService.java    From web3j with Apache License 2.0 5 votes vote down vote up
private long getReplyId(JsonNode replyJson) throws IOException {
    JsonNode idField = replyJson.get("id");
    if (idField == null) {
        throw new IOException("'id' field is missing in the reply");
    }

    if (!idField.isIntegralNumber()) {
        throw new IOException(
                String.format("'id' expected to be long, but it is: '%s'", idField.asText()));
    }

    return idField.longValue();
}
 
Example 13
Source File: JsonFrameworkDeserializer.java    From zeno with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 14
Source File: WebSocketService.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private long getReplyId(JsonNode replyJson) throws IOException {
    JsonNode idField = replyJson.get("id");
    if (idField == null) {
        throw new IOException("'id' field is missing in the reply");
    }

    if (!idField.isIntegralNumber()) {
        throw new IOException(
                String.format("'id' expected to be long, but it is: '%s'",
                        idField.asText()));
    }

    return idField.longValue();
}
 
Example 15
Source File: BuiltinFunctions.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode call(JsonNode input, JsonNode[] arguments) {
  JsonNode dividend = arguments[0];
  if (dividend.isNull())
    return NullNode.instance;
  else if (!dividend.isNumber())
    throw new JsltException("mod(): dividend cannot be a non-number: " + dividend);

  JsonNode divisor = arguments[1];
  if (divisor.isNull())
    return NullNode.instance;
  else if (!divisor.isNumber())
    throw new JsltException("mod(): divisor cannot be a non-number: " + divisor);

  if (!dividend.isIntegralNumber() || !divisor.isIntegralNumber()) {
    throw new JsltException("mod(): operands must be integral types");
  } else {
    long D = dividend.longValue();
    long d = divisor.longValue();
    if (d == 0)
      throw new JsltException("mod(): cannot divide by zero");

    long r = D % d;
    if (r < 0) {
      if (d > 0)
        r += d;
      else
        r -= d;
    }

    return new LongNode(r);
  }
}
 
Example 16
Source File: LongObjectIOProvider.java    From constellation with Apache License 2.0 5 votes vote down vote up
@Override
public void readObject(final int attributeId, final int elementId, final JsonNode jnode, 
        final GraphWriteMethods graph, final Map<Integer, Integer> vertexMap, final Map<Integer, Integer> transactionMap, 
        final GraphByteReader byteReader, final ImmutableObjectCache cache) throws IOException {
    final Long attributeValue = jnode.isNull() ? null : jnode.longValue();
    graph.setObjectValue(attributeId, elementId, attributeValue);
}
 
Example 17
Source File: JsonUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
public static long getLongField(long defaultVal, JsonNode node, String... keys) {
    JsonNode current = node;
    for (final String key : keys) {
        if (!current.hasNonNull(key)) {
            return defaultVal;
        }
        current = current.get(key);
    }
    return current.longValue();
}
 
Example 18
Source File: JsonFileReader.java    From kafka-connect-fs with Apache License 2.0 4 votes vote down vote up
private Object mapValue(Schema schema, JsonNode value) {
    if (value == null) return null;

    switch (value.getNodeType()) {
        case BOOLEAN:
            return value.booleanValue();
        case NUMBER:
            if (value.isShort()) {
                return value.shortValue();
            } else if (value.isInt()) {
                return value.intValue();
            } else if (value.isLong()) {
                return value.longValue();
            } else if (value.isFloat()) {
                return value.floatValue();
            } else if (value.isDouble()) {
                return value.doubleValue();
            } else if (value.isBigInteger()) {
                return value.bigIntegerValue();
            } else {
                return value.numberValue();
            }
        case STRING:
            return value.asText();
        case BINARY:
            try {
                return value.binaryValue();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        case OBJECT:
        case POJO:
            Struct struct = new Struct(schema);
            Iterable<Map.Entry<String, JsonNode>> fields = value::fields;
            StreamSupport.stream(fields.spliterator(), false)
                    .forEach(field -> struct.put(field.getKey(),
                            mapValue(extractSchema(field.getValue()), field.getValue()))
                    );
            return struct;
        case ARRAY:
            Iterable<JsonNode> arrayElements = value::elements;
            return StreamSupport.stream(arrayElements.spliterator(), false)
                    .map(elm -> mapValue(schema, elm))
                    .collect(Collectors.toList());
        case NULL:
        case MISSING:
        default:
            return null;
    }
}
 
Example 19
Source File: ThresholdMixinPerfTest.java    From json-schema-validator with Apache License 2.0 4 votes vote down vote up
@Override
public boolean crossesThreshold(JsonNode node) {
    long lm = maximumLong.longValue();
    long val = node.longValue();
    return lm < val || (excludeEqual && lm == val);
}
 
Example 20
Source File: JsonConvert.java    From DBus with Apache License 2.0 4 votes vote down vote up
@Override
public Object convert(Schema schema, JsonNode value) {
    return value.longValue();
}