@Nullable private Object valueNode(JsonNode jsonNode) { if (jsonNode.isInt()) { return jsonNode.asInt(); } else if (jsonNode.isLong()) { return jsonNode.asLong(); } else if (jsonNode.isIntegralNumber()) { return jsonNode.asLong(); } else if (jsonNode.isFloatingPointNumber()) { return jsonNode.asDouble(); } else if (jsonNode.isBoolean()) { return jsonNode.asBoolean(); } else if (jsonNode.isNull()) { return null; } else { return jsonNode.asText(); } }
@Override public Object apply(JsonNode node) { if (node == null || node.isMissingNode() || node.isNull()) { return null; } if (node.isIntegralNumber()) { if (node.canConvertToLong()) { return node.asLong(); } else { return node.asDouble(); } } if (node.isFloatingPointNumber()) { return node.asDouble(); } final String s = node.asText(); final CharsetEncoder enc = StandardCharsets.UTF_8.newEncoder(); if (s != null && !enc.canEncode(s)) { // Some whacky characters are in this string (e.g. \uD900). These are problematic because they are decodeable // by new String(...) but will not encode into the same character. This dance here will replace these // characters with something more sane. return StringUtils.fromUtf8(StringUtils.toUtf8(s)); } else { return s; } } };
@Override public Object getField(String fieldName) { JsonNode fn = jn.get(fieldName); if (fn.isContainerNode()) { AtomicInteger idx = new AtomicInteger(0); List<Field> fields = Lists.newArrayList(fn.fieldNames()) .stream() .map(f -> new Field(f, idx.getAndIncrement())) .collect(Collectors.toList()); return new GenericJsonRecord(fields, fn); } else if (fn.isBoolean()) { return fn.asBoolean(); } else if (fn.isInt()) { return fn.asInt(); } else if (fn.isFloatingPointNumber()) { return fn.asDouble(); } else if (fn.isDouble()) { return fn.asDouble(); } else { return fn.asText(); } } }
if (value.isContainerNode()) { fieldValue = value.toString(); } else if (value.isFloatingPointNumber()) { fieldValue = value.asDouble(); } else if (value.isIntegralNumber()) {
private Object valueFromJsonNode(String path, JsonNode valueNode) { if (valueNode == null || valueNode.isNull()) { return null; } else if (valueNode.isTextual()) { return valueNode.asText(); } else if (valueNode.isFloatingPointNumber()) { return valueNode.asDouble(); } else if (valueNode.isBoolean()) { return valueNode.asBoolean(); } else if (valueNode.isInt()) { return valueNode.asInt(); } else if (valueNode.isLong()) { return valueNode.asLong(); } else if (valueNode.isObject() || (valueNode.isArray())) { return new JsonLateObjectEvaluator(mapper, valueNode); } throw new PatchException( String.format("Unrecognized valueNode type at path %s and value node %s.", path, valueNode)); } }
private Object parseId(JsonNode node) { if (isNullNodeOrValue(node)) { return null; } if (node.isDouble()) { return node.asDouble(); } if (node.isFloatingPointNumber()) { return node.asDouble(); } if (node.isInt()) { return node.asInt(); } if (node.isLong()) { return node.asLong(); } //TODO(donequis): consider parsing bigints if (node.isIntegralNumber()) { return node.asInt(); } if (node.isTextual()) { return node.asText(); } throw new IllegalArgumentException("Unknown id type"); }
if (fn.isIntegralNumber()) { eb.setAttribute(TypedName.create(name, Long.class), fn.asLong()); } else if (fn.isFloatingPointNumber()) { eb.setAttribute(TypedName.create(name, Double.class), fn.asDouble()); } else if (fn.isTextual()) {
@JsonIgnore public boolean isDouble(int index) { return value.get(index).isFloatingPointNumber(); }
public JsonNode call(JsonNode input, JsonNode[] arguments) { return NodeUtils.toJson(arguments[0].isFloatingPointNumber()); } }
/** * Checks if a node is compatible with a specific Java type. */ static boolean isInstanceOfType(JsonNode node, Class<?> clazz) { return (node.isIntegralNumber() && INTEGRAL_NUMBER_TYPES.contains(clazz)) || (node.isFloatingPointNumber() && FLOATING_POINT_NUMBER_TYPES.contains(clazz)) || (node.isTextual() && TEXTUAL_TYPES.contains(clazz)) || (node.isBoolean() && BOOLEAN_TYPES.contains(clazz)); }
private Object readValue(JsonNode node) { if (node.isFloatingPointNumber()) { return node.asDouble(); } else if (node.isNumber()) { return node.asLong(); } else if (node.isBoolean()) { return node.asBoolean(); } else if (node.isTextual()) { return node.asText(); } else if (node.isArray()) { return StreamSupport.stream(node.spliterator(), false) .map(this::readValue) .collect(toList()); } return node.toString(); } }
/** * Returns a more detailed type name then the node type would provide. */ static String typeName(JsonNode node) { if (node.isIntegralNumber()) { return "integral number"; } else if (node.isFloatingPointNumber()) { return "floating point number"; } else if (node.isTextual()) { return "textual"; } else if (node.isBoolean()) { return "boolean"; } else { return node.getClass().getName(); } }
@Override protected Float valueNonNull(JsonNode node) { if (node.isTextual()) { String value = node.asText(); if ("NaN".equalsIgnoreCase(value)) { return Float.NaN; } else if ("infinity".equalsIgnoreCase(value) || "+infinity".equalsIgnoreCase(value)) { return Float.POSITIVE_INFINITY; } else if ("-infinity".equalsIgnoreCase(value)) { return Float.NEGATIVE_INFINITY; } } if (!node.isIntegralNumber() && !node.isFloatingPointNumber()) { throw new AgException(Status.BAD_REQUEST, "Expected numeric value, got: " + node.asText()); } Double doubleValue = node.asDouble(); if (Double.valueOf(0).equals(doubleValue)) { return 0f; } Double absDoubleValue = Math.abs(doubleValue); if (absDoubleValue < Float.MIN_VALUE || absDoubleValue > Float.MAX_VALUE) { throw new AgException(Status.BAD_REQUEST, "Value is either too small or too large for the java.lang.Float datatype: " + node.asText()); } return doubleValue.floatValue(); }
public Object deserialize(JsonNode node) { if (!node.isArray()) { switch (node.getNodeType()) { case BOOLEAN: return node.asBoolean(); case NUMBER: if (node.isFloatingPointNumber()) { return node.decimalValue(); } else { return node.bigIntegerValue(); } case STRING: return node.asText(); default: break; } } if (node.isArray() && node.get(0).isTextual() && ("uuid".equals(node.get(0).asText()) || "named-uuid".equals(node.get(0).asText()))) { return new UUID(node.get(1).asText()); } throw new IllegalArgumentException("not an atom node"); } }
private Object valueFromJsonNode(String path, JsonNode valueNode) { if (valueNode == null || valueNode.isNull()) { return null; } else if (valueNode.isTextual()) { return valueNode.asText(); } else if (valueNode.isFloatingPointNumber()) { return valueNode.asDouble(); } else if (valueNode.isBoolean()) { return valueNode.asBoolean(); } else if (valueNode.isInt()) { return valueNode.asInt(); } else if (valueNode.isLong()) { return valueNode.asLong(); } else if (valueNode.isObject() || (valueNode.isArray())) { return new JsonLateObjectEvaluator(mapper, valueNode); } throw new PatchException( String.format("Unrecognized valueNode type at path %s and value node %s.", path, valueNode)); } }
/** * Serialize the attribute value from a JsonNode object to a String object. * * @return */ @Override public String serialize(JsonNode value) { if (value.isNull()) return "null"; else if (value.isIntegralNumber()) return value.bigIntegerValue().toString(); else if (value.isFloatingPointNumber()) return String.valueOf(value.doubleValue()); else return value.numberValue().toString(); }
@Override public Object apply(JsonNode node) { if (node == null || node.isMissingNode() || node.isNull()) { return null; } if (node.isIntegralNumber()) { if (node.canConvertToLong()) { return node.asLong(); } else { return node.asDouble(); } } if (node.isFloatingPointNumber()) { return node.asDouble(); } final String s = node.asText(); final CharsetEncoder enc = Charsets.UTF_8.newEncoder(); if (s != null && !enc.canEncode(s)) { // Some whacky characters are in this string (e.g. \uD900). These are problematic because they are decodeable // by new String(...) but will not encode into the same character. This dance here will replace these // characters with something more sane. return StringUtils.fromUtf8(StringUtils.toUtf8(s)); } else { return s; } } };
/** * Extracts double value from JsonNode if it is within bounds. * * <p>Throws {@link UnsupportedRowJsonException} if value is out of bounds. */ static ValueExtractor<Double> doubleValueExtractor() { return ValidatingValueExtractor.<Double>builder() .setExtractor(JsonNode::doubleValue) .setValidator( jsonNode -> jsonNode.isDouble() // Either a long number which allows lossless conversion to float || (jsonNode.isIntegralNumber() && jsonNode.canConvertToLong() && jsonNode.asLong() == (long) (double) jsonNode.asInt()) // Or a decimal number which allows lossless conversion to float || (jsonNode.isFloatingPointNumber() && jsonNode .decimalValue() .equals(BigDecimal.valueOf(jsonNode.doubleValue())))) .build(); }
@Test public void testDoubleField() { DoubleField doubleField = new DoubleField(11.11); JsonNode jsonNode = TestUtils.testJsonSerialization(doubleField); Assert.assertTrue(jsonNode.get(JsonConstants.FIELD_VALUE).isFloatingPointNumber()); }
/** * Extracts float value from JsonNode if it is within bounds. * * <p>Throws {@link UnsupportedRowJsonException} if value is out of bounds. */ static ValueExtractor<Float> floatValueExtractor() { return ValidatingValueExtractor.<Float>builder() .setExtractor(JsonNode::floatValue) .setValidator( jsonNode -> jsonNode.isFloat() // Either floating number which allows lossless conversion to float || (jsonNode.isFloatingPointNumber() && jsonNode.doubleValue() == (double) (float) jsonNode.doubleValue()) // Or an integer number which allows lossless conversion to float || (jsonNode.isIntegralNumber() && jsonNode.canConvertToInt() && jsonNode.asInt() == (int) (float) jsonNode.asInt())) .build(); }