List of usage examples for com.fasterxml.jackson.databind JsonNode asLong
public long asLong()
From source file:de.jlo.talendcomp.json.TypeUtil.java
public static Long convertToLong(JsonNode node) throws Exception { if (node.isNumber()) { return node.asLong(); } else if (node.isTextual()) { return convertToLong(node.asText()); } else if (node.isNull()) { return null; } else {/*from w w w. ja v a 2 s. c om*/ throw new Exception("Node: " + node + " cannot be converted to Long"); } }
From source file:io.appform.jsonrules.utils.ComparisonUtils.java
static int compare(JsonNode evaluatedNode, Object value) { int comparisonResult = 0; if (evaluatedNode.isNumber()) { if (Number.class.isAssignableFrom(value.getClass())) { Number nValue = (Number) value; if (evaluatedNode.isIntegralNumber()) { comparisonResult = Long.compare(evaluatedNode.asLong(), nValue.longValue()); } else if (evaluatedNode.isFloatingPointNumber()) { comparisonResult = Double.compare(evaluatedNode.asDouble(), nValue.doubleValue()); }//from www. java 2s . c om } else { throw new IllegalArgumentException("Type mismatch between operator and operand"); } } else if (evaluatedNode.isBoolean()) { if (Boolean.class.isAssignableFrom(value.getClass())) { Boolean bValue = Boolean.parseBoolean(value.toString()); comparisonResult = Boolean.compare(evaluatedNode.asBoolean(), bValue); } else { throw new IllegalArgumentException("Type mismatch between operator and operand"); } } else if (evaluatedNode.isTextual()) { if (String.class.isAssignableFrom(value.getClass())) { comparisonResult = evaluatedNode.asText().compareTo(String.valueOf(value)); } else { throw new IllegalArgumentException("Type mismatch between operator and operand"); } } else if (evaluatedNode.isObject()) { throw new IllegalArgumentException("Object comparisons not supported"); } return comparisonResult; }
From source file:org.apache.usergrid.java.client.utils.JsonUtils.java
@Nullable @SuppressWarnings("unchecked") public static <T> T getProperty(@NotNull final Map<String, JsonNode> properties, @NotNull final String name) { JsonNode value = properties.get(name); if (value == null) { return null; } else if (value instanceof TextNode) { return (T) value.asText(); } else if (value instanceof LongNode) { Long valueLong = value.asLong(); return (T) valueLong; } else if (value instanceof BooleanNode) { Boolean valueBoolean = value.asBoolean(); return (T) valueBoolean; } else if (value instanceof IntNode) { Integer valueInteger = value.asInt(); return (T) valueInteger; } else if (value instanceof FloatNode) { return (T) Float.valueOf(value.toString()); } else {/*from w w w.j av a 2s.co m*/ return (T) value; } }
From source file:com.squarespace.template.GeneralUtils.java
/** * Determines the boolean value of a node based on its type. *///from ww w . ja v a2s . c o m public static boolean isTruthy(JsonNode node) { if (node.isTextual()) { return !node.asText().equals(""); } if (node.isNumber() || node.isBoolean()) { return node.asLong() != 0; } if (node.isMissingNode() || node.isNull()) { return false; } return node.size() != 0; }
From source file:de.thingweb.typesystem.jsonschema.JsonSchemaType.java
public static JsonType getJsonType(JsonNode jsonSchemaNode) throws JsonSchemaException { JsonType jtype = null;/*from w w w.j av a 2s . c o m*/ if (jsonSchemaNode != null) { JsonNode typeNode = jsonSchemaNode.findValue("type"); if (typeNode != null) { switch (typeNode.asText()) { case "boolean": jtype = new JsonBoolean(); break; case "integer": case "number": boolean exclusiveMinimum = false; JsonNode exclusiveMinimumNode = jsonSchemaNode.findValue("exclusiveMinimum"); if (exclusiveMinimumNode != null && exclusiveMinimumNode.getNodeType() == JsonNodeType.BOOLEAN) { exclusiveMinimum = exclusiveMinimumNode.asBoolean(); } boolean exclusiveMaximum = false; JsonNode exclusiveMaximumNode = jsonSchemaNode.findValue("exclusiveMaximum"); if (exclusiveMaximumNode != null && exclusiveMaximumNode.getNodeType() == JsonNodeType.BOOLEAN) { exclusiveMaximum = exclusiveMaximumNode.asBoolean(); } if ("integer".equals(typeNode.asText())) { jtype = new JsonInteger(); JsonNode minimumNode = jsonSchemaNode.findValue("minimum"); if (minimumNode != null && minimumNode.getNodeType() == JsonNodeType.NUMBER) { ((JsonInteger) jtype).setMinimum(minimumNode.asLong()); } JsonNode maximumNode = jsonSchemaNode.findValue("maximum"); if (maximumNode != null && maximumNode.getNodeType() == JsonNodeType.NUMBER) { ((JsonInteger) jtype).setMaximum(maximumNode.asLong()); } } else { assert ("number".equals(typeNode.asText())); jtype = new JsonNumber(); JsonNode minimumNode = jsonSchemaNode.findValue("minimum"); if (minimumNode != null && minimumNode.getNodeType() == JsonNodeType.NUMBER) { ((JsonNumber) jtype).setMinimum(minimumNode.asDouble()); } JsonNode maximumNode = jsonSchemaNode.findValue("maximum"); if (maximumNode != null && maximumNode.getNodeType() == JsonNodeType.NUMBER) { ((JsonNumber) jtype).setMaximum(maximumNode.asDouble()); } } ((AbstractJsonNumeric) jtype).setExclusiveMinimum(exclusiveMinimum); ((AbstractJsonNumeric) jtype).setExclusiveMaximum(exclusiveMaximum); break; case "null": jtype = new JsonNull(); break; case "string": jtype = new JsonString(); break; case "array": JsonNode itemsNode = jsonSchemaNode.findValue("items"); if (itemsNode != null && JsonNodeType.OBJECT == itemsNode.getNodeType()) { jtype = new JsonArray(getJsonType(itemsNode)); } else { throw new JsonSchemaException("items not object"); } break; case "object": JsonNode propertiesNode = jsonSchemaNode.findValue("properties"); if (propertiesNode != null && JsonNodeType.OBJECT == propertiesNode.getNodeType()) { Iterator<Entry<String, JsonNode>> iter = propertiesNode.fields(); Map<String, JsonType> properties = new HashMap<String, JsonType>(); while (iter.hasNext()) { Entry<String, JsonNode> e = iter.next(); JsonNode nodeProp = e.getValue(); properties.put(e.getKey(), getJsonType(nodeProp)); } jtype = new JsonObject(properties); } else { throw new JsonSchemaException("Properties not object"); } // required JsonNode requiredNode = jsonSchemaNode.findValue("required"); if (requiredNode != null && JsonNodeType.ARRAY == requiredNode.getNodeType()) { ArrayNode an = (ArrayNode) requiredNode; Iterator<JsonNode> iterReq = an.elements(); while (iterReq.hasNext()) { JsonNode nreq = iterReq.next(); if (JsonNodeType.STRING == nreq.getNodeType()) { ((JsonObject) jtype).addRequired(nreq.asText()); } else { throw new JsonSchemaException("Unexpected required node: " + nreq); } } } break; } } } return jtype; }
From source file:com.gsma.mobileconnect.utils.JsonUtils.java
/** * Return the long value of an optional child node. * <p>//from w ww .j a v a2 s . co m * Check the parent node for the named child, if found return the long contents of the child node, return null otherwise. * * @param parentNode The node to check * @param name The name of the optional child. * @return Long value of the child node if present, null otherwise. */ static private Long getOptionalLongValue(JsonNode parentNode, String name) { JsonNode childNode = parentNode.get(name); if (null == childNode) { return null; } else { return childNode.asLong(); } }
From source file:com.gsma.mobileconnect.utils.AndroidJsonUtils.java
/** * Return the long value of an optional child node. * <p>/*from w w w . j a va 2 s . c om*/ * Check the parent node for the named child, if found return the long contents of the child node, return null otherwise. * * @param parentNode The node to check * @param name The name of the optional child. * @return Long value of the child node if present, null otherwise. */ public static Long getOptionalLongValue(JsonNode parentNode, String name) { JsonNode childNode = parentNode.get(name); if (null == childNode) { return null; } else { return childNode.asLong(); } }
From source file:de.unikonstanz.winter.crossref.node.doi.CrossrefDoiNodeModel.java
private static DataCell nodeToDateCell(final JsonNode node) { if (CrossrefUtil.isNull(node)) { return new MissingCell(null); }/*w w w . j av a2 s . com*/ JsonNode timestamp = node.get("timestamp"); if (CrossrefUtil.isNull(timestamp)) { return new MissingCell(null); } Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timestamp.asLong()); return new DateAndTimeCell(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), calendar.get(Calendar.MILLISECOND)); }
From source file:org.kiji.rest.representations.KijiRestEntityId.java
/** * Converts a JSON string, integer, or wildcard (empty array) * node into a Java object (String, Integer, Long, WILDCARD, or null). * * @param node JSON string, integer numeric, or wildcard (empty array) node. * @return the JSON value, as a String, an Integer, a Long, a WILDCARD, or null. * @throws JsonParseException if the JSON node is not String, Integer, Long, WILDCARD, or null. *///from ww w . j a va 2s .co m private static Object getNodeValue(JsonNode node) throws JsonParseException { // TODO: Write tests to distinguish integer and long components. if (node.isInt()) { return node.asInt(); } else if (node.isLong()) { return node.asLong(); } else if (node.isTextual()) { return node.asText(); } else if (node.isArray() && node.size() == 0) { // An empty array token indicates a wildcard. return WildcardSingleton.INSTANCE; } else if (node.isNull()) { return null; } else { throw new JsonParseException(String.format( "Invalid JSON value: '%s', expecting string, int, long, null, or wildcard [].", node), null); } }
From source file:com.dnw.json.J.java
/** * Resolves a JsonNode, returns the corresponding Java object. * // ww w . j a v a2 s . c om * @author manbaum * @since Oct 22, 2014 * @param node the given <code>JsonNode</code>. * @return the corresponding Java object. */ public final static Object resolve(JsonNode node) { if (node.isNull()) { return null; } else if (node.isTextual()) { return node.asText(); } else if (node.isIntegralNumber()) { return node.asLong(); } else if (node.isDouble()) { return node.asDouble(); } else if (node.isBoolean()) { return node.asBoolean(); } else if (node.isObject()) { return resolveObject(node); } else if (node.isArray()) { return resolveArray(node); } return null; }