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(); }
@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 long getLong() { try { long longValue; if (value.isIntegralNumber() && !value.isBigInteger()) { longValue = value.longValue(); if (longValue >= minValue && longValue <= maxValue) { return longValue; } } else if (value.isValueNode()) { longValue = parseLong(value.asText()); if (longValue >= minValue && longValue <= maxValue) { return longValue; } } } catch (NumberFormatException ignore) { // ignore } throw new PrestoException( DECODER_CONVERSION_NOT_SUPPORTED, format("could not parse value '%s' as '%s' for column '%s'", value.asText(), columnHandle.getType(), columnHandle.getName())); }
@Override protected long getMillis() { if (value.isIntegralNumber() && !value.isBigInteger()) { return value.longValue(); } if (value.isValueNode()) { try { return parseLong(value.asText()); } catch (NumberFormatException e) { throw new PrestoException( DECODER_CONVERSION_NOT_SUPPORTED, format("could not parse value '%s' as '%s' for column '%s'", value.asText(), columnHandle.getType(), columnHandle.getName())); } } throw new PrestoException( DECODER_CONVERSION_NOT_SUPPORTED, format("could not parse non-value node as '%s' for column '%s'", columnHandle.getType(), columnHandle.getName())); } }
private byte[] extractJNodeValue(final JsonNode n){ if (n.isBoolean()){ //boolean return clientService.toBytes(n.asBoolean()); }else if(n.isNumber()){ if(n.isIntegralNumber()){ //interpret as Long return clientService.toBytes(n.asLong()); }else{ //interpret as Double return clientService.toBytes(n.asDouble()); } }else{ //if all else fails, interpret as String return clientService.toBytes(n.asText()); } }
@Override protected long getMillis() { try { if (value.isIntegralNumber() && !value.isBigInteger()) { return multiplyExact(value.longValue(), 1000); } if (value.isValueNode()) { return multiplyExact(parseLong(value.asText()), 1000); } throw new PrestoException( DECODER_CONVERSION_NOT_SUPPORTED, format("could not parse non-value node as '%s' for column '%s'", columnHandle.getType(), columnHandle.getName())); } catch (NumberFormatException | ArithmeticException e) { throw new PrestoException( DECODER_CONVERSION_NOT_SUPPORTED, format("could not parse value '%s' as '%s' for column '%s'", value.asText(), columnHandle.getType(), columnHandle.getName())); } } }
} else if (value.isFloatingPointNumber()) { fieldValue = value.asDouble(); } else if (value.isIntegralNumber()) { fieldValue = value.asLong(); } else if (value.isNull()) {
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"); }
eb.setAttribute(attr, mapper.convertValue(fn, attr.getJacksonType())); } else { if (fn.isIntegralNumber()) { eb.setAttribute(TypedName.create(name, Long.class), fn.asLong()); } else if (fn.isFloatingPointNumber()) {
@Test public void idIntegerType() throws Exception { EasyMock.expect(mockService.testMethod(param1)).andReturn(param1); EasyMock.replay(mockService); jsonRpcServer.handleRequest(messageWithListParamsStream(intParam1, "testMethod", param1), byteArrayOutputStream); assertTrue(decodeAnswer(byteArrayOutputStream).get(ID).isIntegralNumber()); }
private void checkSuccessfulResponse(MockHttpServletResponse response) throws IOException { assertTrue(HttpServletResponse.SC_OK == response.getStatus()); JsonNode responseEnvelope = decodeAnswer(toByteArrayOutputStream(response.getContentAsByteArray())); assertTrue(responseEnvelope.get(ID).isIntegralNumber()); assertEquals(responseEnvelope.get(ID).asLong(), 123L); assertTrue(responseEnvelope.get(RESULT).isTextual()); assertEquals(responseEnvelope.get(RESULT).asText(), "For?est"); }
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(); }
private static boolean numEquals(final JsonNode a, final JsonNode b) { /* * If both numbers are integers, delegate to JsonNode. */ if (a.isIntegralNumber() && b.isIntegralNumber()) { return a.equals(b); } /* * Otherwise, compare decimal values. */ return a.decimalValue().compareTo(b.decimalValue()) == 0; }
private static boolean numEquals(final JsonNode a, final JsonNode b) { /* * If both numbers are integers, delegate to JsonNode. */ if (a.isIntegralNumber() && b.isIntegralNumber()) { return a.equals(b); } /* * Otherwise, compare decimal values. */ return a.decimalValue().compareTo(b.decimalValue()) == 0; }
private static boolean numEquals(final JsonNode a, final JsonNode b) { /* * If both numbers are integers, delegate to JsonNode. */ if (a.isIntegralNumber() && b.isIntegralNumber()) return a.equals(b); /* * Otherwise, compare decimal values. */ return a.decimalValue().compareTo(b.decimalValue()) == 0; }
public MinLengthValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MIN_LENGTH, validationContext); minLength = Integer.MIN_VALUE; if (schemaNode != null && schemaNode.isIntegralNumber()) { minLength = schemaNode.intValue(); } parseErrorCode(getValidatorType().getErrorCodeKey()); }
public MinPropertiesValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MIN_PROPERTIES, validationContext); if (schemaNode.isIntegralNumber()) { min = schemaNode.intValue(); } parseErrorCode(getValidatorType().getErrorCodeKey()); }
/** * Extracts long value from JsonNode if it is within bounds. * * <p>Throws {@link UnsupportedRowJsonException} if value is out of bounds. */ static ValueExtractor<Long> longValueExtractor() { return ValidatingValueExtractor.<Long>builder() .setExtractor(JsonNode::longValue) .setValidator(jsonNode -> jsonNode.isIntegralNumber() && jsonNode.canConvertToLong()) .build(); }
/** * Extracts int value from JsonNode if it is within bounds. * * <p>Throws {@link UnsupportedRowJsonException} if value is out of bounds. */ static ValueExtractor<Integer> intValueExtractor() { return ValidatingValueExtractor.<Integer>builder() .setExtractor(JsonNode::intValue) .setValidator(jsonNode -> jsonNode.isIntegralNumber() && jsonNode.canConvertToInt()) .build(); }