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; } final BigDecimal decimal = node.decimalValue(); ret.put(keyword, decimal.scale() == 0 ? FACTORY.numberNode(decimal.toBigIntegerExact()) : node); return ret; } }
public static WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode == null || messageNode.size() < 1 || !messageNode.get(0).canConvertToInt()) throw new WampError(ApplicationError.INVALID_MESSAGE); int messageType = messageNode.get(0).asInt(); WampMessageFactory factory = messageFactories.get(messageType); if (factory == null) return null; // We can't find the message type, so we skip it return factory.fromObjectArray(messageNode); }
@Override public List<JsonNode> apply(final Scope scope, final List<JsonQuery> args, final JsonNode in) throws JsonQueryException { Preconditions.checkInputArrayType("implode", in, JsonNodeType.NUMBER); final StringBuilder builder = new StringBuilder(); for (final JsonNode ch : in) { if (ch.canConvertToInt()) { builder.append((char) ch.asInt()); } else { throw new JsonQueryException("input to implode() must be a list of codepoints; " + ch.getNodeType() + " found"); } } return Collections.<JsonNode> singletonList(new TextNode(builder.toString())); } }
public static WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode == null || messageNode.size() < 1 || !messageNode.get(0).canConvertToInt()) throw new WampError(ApplicationError.INVALID_MESSAGE); int messageType = messageNode.get(0).asInt(); WampMessageFactory factory = messageFactories.get(messageType); if (factory == null) return null; // We can't find the message type, so we skip it return factory.fromObjectArray(messageNode); }
@Override public List<JsonNode> apply(final Scope scope, final List<JsonQuery> args, final JsonNode in) throws JsonQueryException { Preconditions.checkInputArrayType("implode", in, JsonNodeType.NUMBER); final StringBuilder builder = new StringBuilder(); for (final JsonNode ch : in) { if (ch.canConvertToInt()) { builder.append((char) ch.asInt()); } else { throw new JsonQueryException("input to implode() must be a list of codepoints; " + ch.getNodeType() + " found"); } } return Collections.<JsonNode> singletonList(new TextNode(builder.toString())); } }
public static WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode == null || messageNode.size() < 1 || !messageNode.get(0).canConvertToInt()) throw new WampError(ApplicationError.INVALID_MESSAGE); int messageType = messageNode.get(0).asInt(); WampMessageFactory factory = messageFactories.get(messageType); if (factory == null) return null; // We can't find the message type, so we skip it return factory.fromObjectArray(messageNode); }
private static void validateRevisionNumber(DeserializationContext ctx, @Nullable JsonNode node, String type, boolean zeroAllowed) throws JsonMappingException { if (node == null) { ctx.reportInputMismatch(Revision.class, "missing %s revision number", type); // Should never reach here. throw new Error(); } if (!node.canConvertToInt() || !zeroAllowed && node.intValue() == 0) { ctx.reportInputMismatch(Revision.class, "A %s revision number must be %s integer.", type, zeroAllowed ? "an" : "a non-zero"); // Should never reach here. throw new Error(); } } }
private static void validateRevisionNumber(DeserializationContext ctx, @Nullable JsonNode node, String type, boolean zeroAllowed) throws JsonMappingException { if (node == null) { ctx.reportInputMismatch(Revision.class, "missing %s revision number", type); // Should never reach here. throw new Error(); } if (!node.canConvertToInt() || !zeroAllowed && node.intValue() == 0) { ctx.reportInputMismatch(Revision.class, "A %s revision number must be %s integer.", type, zeroAllowed ? "an" : "a non-zero"); // Should never reach here. throw new Error(); } } }
private static void validateRevisionNumber(DeserializationContext ctx, @Nullable JsonNode node, String type, boolean zeroAllowed) throws JsonMappingException { if (node == null) { ctx.reportInputMismatch(Revision.class, "missing %s revision number", type); // Should never reach here. throw new Error(); } if (!node.canConvertToInt() || !zeroAllowed && node.intValue() == 0) { ctx.reportInputMismatch(Revision.class, "A %s revision number must be %s integer.", type, zeroAllowed ? "an" : "a non-zero"); // Should never reach here. throw new Error(); } } }
@Override public JsonNode apply(ObjectMapper mapper, JsonNode lhs, JsonNode rhs) throws JsonQueryException { if (lhs.isIntegralNumber() && rhs.isIntegralNumber()) { final long r = lhs.asLong() * rhs.asLong(); return JsonNodeUtils.asNumericNode(r); } else if (lhs.isNumber() && rhs.isNumber()) { final double r = lhs.asDouble() * rhs.asDouble(); return JsonNodeUtils.asNumericNode(r); } else if (lhs.isTextual() && rhs.canConvertToInt()) { final int count = rhs.asInt(); if (count <= 0) return NullNode.getInstance(); return new TextNode(Strings.repeat(lhs.asText(), count)); } else if (lhs.isObject() && rhs.isObject()) { return mergeRecursive(mapper, (ObjectNode) lhs, (ObjectNode) rhs); } else { throw new JsonQueryTypeException(lhs, rhs, "cannot be multiplied"); } }
@Override public void checkValue(final SyntaxValidator validator, final List<Message> messages, final JsonNode schema) { final JsonNode node = schema.get(keyword); final Message.Builder msg = newMsg().addInfo("found", node); if (!node.canConvertToInt()) { msg.setMessage("integer value is too large") .addInfo("max", Integer.MAX_VALUE); messages.add(msg.build()); return; } if (node.intValue() < 0) messages.add(msg.setMessage("value cannot be negative").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(); }
@Override public void validate(final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final JsonNode instance = data.getInstance().getNode(); if (!instance.canConvertToInt()) { report.warn(newMsg(data, bundle, "warn.format.int32.overflow") .put("key", "warn.format.int32.overflow") .putArgument("value", instance)); } } }
@Override public void validate(final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final JsonNode instance = data.getInstance().getNode(); if (!instance.canConvertToInt()) { report.warn(newMsg(data, bundle, "warn.format.int32.overflow") .put("key", "warn.format.int32.overflow") .putArgument("value", instance)); } } }
/** * 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(); }
@Override protected void checkValue(final Collection<JsonPointer> pointers, final MessageBundle bundle, final ProcessingReport report, final SchemaTree tree) throws ProcessingException { final JsonNode node = getNode(tree); if (!node.canConvertToInt()) { report.error(newMsg(tree, bundle, "common.integerTooLarge") .put("max", Integer.MAX_VALUE)); return; } if (node.intValue() < 0) report.error(newMsg(tree, bundle, "common.integerIsNegative")); } }
@Override protected void checkValue(final Collection<JsonPointer> pointers, final MessageBundle bundle, final ProcessingReport report, final SchemaTree tree) throws ProcessingException { final JsonNode node = getNode(tree); if (!node.canConvertToInt()) { report.error(newMsg(tree, bundle, "common.integerTooLarge") .put("max", Integer.MAX_VALUE)); return; } if (node.intValue() < 0) report.error(newMsg(tree, bundle, "common.integerIsNegative")); } }
@Override protected void checkValue(final Collection<JsonPointer> pointers, final MessageBundle bundle, final ProcessingReport report, final SchemaTree tree) throws ProcessingException { final JsonNode node = getNode(tree); if (!node.canConvertToInt()) { report.error(newMsg(tree, bundle, "common.integerTooLarge") .put("max", Integer.MAX_VALUE)); return; } if (node.intValue() < 0) report.error(newMsg(tree, bundle, "common.integerIsNegative")); } }
/** * Extracts byte value from JsonNode if it is within bounds. * * <p>Throws {@link UnsupportedRowJsonException} if value is out of bounds. */ static ValueExtractor<Byte> byteValueExtractor() { return ValidatingValueExtractor.<Byte>builder() .setExtractor(jsonNode -> (byte) jsonNode.intValue()) .setValidator( jsonNode -> jsonNode.isIntegralNumber() && jsonNode.canConvertToInt() && jsonNode.intValue() >= Byte.MIN_VALUE && jsonNode.intValue() <= Byte.MAX_VALUE) .build(); }
/** * Extracts short value from JsonNode if it is within bounds. * * <p>Throws {@link UnsupportedRowJsonException} if value is out of bounds. */ static ValueExtractor<Short> shortValueExtractor() { return ValidatingValueExtractor.<Short>builder() .setExtractor(jsonNode -> (short) jsonNode.intValue()) .setValidator( jsonNode -> jsonNode.isIntegralNumber() && jsonNode.canConvertToInt() && jsonNode.intValue() >= Short.MIN_VALUE && jsonNode.intValue() <= Short.MAX_VALUE) .build(); }