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

The following examples show how to use com.fasterxml.jackson.databind.JsonNode#canConvertToInt() . 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: RevisionJsonDeserializer.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 2
Source File: JsonHelpers.java    From samantha with MIT License 6 votes vote down vote up
/**
 * @return a IntList from the input JsonNode with the given name, or throw an BadRequestException
 */
public static IntList getRequiredListOfInteger(JsonNode json, String name) throws BadRequestException {
    final JsonNode node = json.get(name);

    if (node == null || !(node.isArray())) {
        throw new BadRequestException("json is missing required List: " + name);
    }

    final IntList list = new IntArrayList(node.size());
    for (JsonNode innerNode : node) {
        if (!innerNode.canConvertToInt()) {
            throw new BadRequestException("json is not an int: " + innerNode.toString());
        }
        list.add(innerNode.asInt());
    }
    return list;
}
 
Example 3
Source File: JsonSettingsDecoder.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
protected Object decodeValue(SettingType type, JsonNode node) {
    switch (type) {
        case INTEGER:
            if (!node.canConvertToInt()) {
                numberDecodeError(type, node);
            }
            return node.intValue();
        case NUMERIC:
            if (!node.isNumber()) {
                numberDecodeError(type, node);
            }
            return node.doubleValue();
        case BOOLEAN:
            return node.booleanValue();
        case TIMEINSTANT:
            return SettingValueFactory.decodeDateTimeValue(node.textValue());
        case FILE:
            return SettingValueFactory.decodeFileValue(node.textValue());
        case STRING:
            return node.textValue();
        case URI:
            return SettingValueFactory.decodeUriValue(node.textValue());
        case MULTILINGUAL_STRING:
            return decodeMultilingualString(node);
        case CHOICE:
            return node.textValue();
        default:
            throw new ConfigurationError(String.format("Unknown Type %s", type));
    }
}
 
Example 4
Source File: JsonHelpers.java    From samantha with MIT License 5 votes vote down vote up
/**
 * @return an int from the input JsonNode with the given name, or throw an BadRequestException
 */
public static int getRequiredInt(JsonNode json, String name) throws BadRequestException {
    JsonNode node = json.get(name);
    if (node == null || !node.canConvertToInt()) {
        throw new BadRequestException("json is missing required int: " + name);
    }
    return node.asInt();
}
 
Example 5
Source File: JsonHelpers.java    From samantha with MIT License 5 votes vote down vote up
/**
 * @return an Integer from the input JsonNode with the given name, or the default value
 */
public static Integer getOptionalInt(JsonNode json, String name, Integer defaultInt) throws BadRequestException {
    JsonNode node = json.get(name);
    if (node == null) {
        return defaultInt;
    }
    if (!node.canConvertToInt()) {
        throw new BadRequestException("json is not an int: " + name);
    }
    return node.asInt();
}
 
Example 6
Source File: ExecutionContextDeserializer.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
public ExecutionContext deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
		throws IOException, JsonProcessingException {
	ObjectCodec oc = jsonParser.getCodec();
	JsonNode node = oc.readTree(jsonParser);

	final ExecutionContext executionContext = new ExecutionContext();
	final boolean dirty = node.get("dirty").asBoolean();

	for (JsonNode valueNode : node.get("values")) {

		final JsonNode nodeValue = valueNode.fields().next().getValue();
		final String nodeKey = valueNode.fields().next().getKey();

		if (nodeValue.isNumber() && !nodeValue.isFloatingPointNumber() && nodeValue.canConvertToInt()) {
			executionContext.putInt(nodeKey, nodeValue.asInt());
		}
		else if (nodeValue.isNumber() && !nodeValue.isFloatingPointNumber() && nodeValue.canConvertToLong()) {
			executionContext.putLong(nodeKey, nodeValue.asLong());
		}
		else if (nodeValue.isFloatingPointNumber()) {
			executionContext.putDouble(nodeKey, nodeValue.asDouble());
		}
		else if (nodeValue.isBoolean()) {
			executionContext.putString(nodeKey, String.valueOf(nodeValue.asBoolean()));
		}
		else if (nodeValue.isTextual()) {
			executionContext.putString(nodeKey, nodeValue.asText());
		}
		else {
			executionContext.put(nodeKey, nodeValue.toString());
		}
	}

	if (!dirty && executionContext.isDirty()) {
		executionContext.clearDirtyFlag();
	}

	return executionContext;
}
 
Example 7
Source File: JsonUtil.java    From besu with Apache License 2.0 4 votes vote down vote up
private static boolean validateInt(final JsonNode node) {
  if (!node.canConvertToInt()) {
    throw new IllegalArgumentException("Cannot convert value to integer: " + node.toString());
  }
  return true;
}