@Override public final boolean isContainerNode() { final JsonNodeType type = getNodeType(); return type == JsonNodeType.OBJECT || type == JsonNodeType.ARRAY; }
/** * Method that can be used to check if this node was created from * JSON literal null value. */ public final boolean isNull() { return getNodeType() == JsonNodeType.NULL; }
@Override public final boolean isValueNode() { switch (getNodeType()) { case ARRAY: case OBJECT: case MISSING: return false; default: return true; } }
/** * @return True if this node represents a numeric JSON value */ public final boolean isNumber() { return getNodeType() == JsonNodeType.NUMBER; }
/** * Method that can be used to check if this node was created from * JSON boolean value (literals "true" and "false"). */ public final boolean isBoolean() { return getNodeType() == JsonNodeType.BOOLEAN; }
/** * Method that can be used to check if this node represents * binary data (Base64 encoded). Although this will be externally * written as JSON String value, {@link #isTextual} will * return false if this method returns true. * * @return True if this node represents base64 encoded binary data */ public final boolean isBinary() { return getNodeType() == JsonNodeType.BINARY; }
/** * Method that can be used to check if the node is a wrapper * for a POJO ("Plain Old Java Object" aka "bean". * Returns true only for * instances of <code>POJONode</code>. * * @return True if this node wraps a POJO */ public final boolean isPojo() { return getNodeType() == JsonNodeType.POJO; }
/** * Method that checks whether this node represents basic JSON String * value. */ public final boolean isTextual() { return getNodeType() == JsonNodeType.STRING; }
private static Object convert(JsonNode value) { if (value.isArray()) { List<String> retvalList = new ArrayList<>(); for (JsonNode arrayElement : value) retvalList.add(arrayElement.asText()); return retvalList; } return value.getNodeType() == JsonNodeType.NUMBER ? value.numberValue() : value.asText(); }
protected static ObjectNode toObjectNode(JsonNode node) { if (node != null) { if (node.isNull()) { return null; } if (!(node instanceof ObjectNode)) { throw new IllegalArgumentException( "Expected OBJECT node. Instead got " + node.getNodeType()); } } return (ObjectNode) node; }
protected static ArrayNode toArrayNode(JsonNode node) { if (node != null) { if (node.isNull()) { return null; } if (!(node instanceof ArrayNode)) { throw new IllegalArgumentException( "Expected ARRAY node. Instead got " + node.getNodeType()); } } return (ArrayNode) node; }
/** * Get a textual value from a json object, throwing an exception if the node is missing or not textual. */ private String getText(JsonNode jsonObject, String nodeName) { JsonNode subNode = jsonObject.get(nodeName); if (subNode == null) { return null; } if (!subNode.isTextual()) { throw new IllegalStateException(nodeName + " from credential process should be textual, but was " + subNode.getNodeType()); } return subNode.asText(); }
private static <T extends JsonNode> T tryDecode(JsonNode encodedNode, T defaultValue, String fieldName) throws IOException { if (encodedNode == null || encodedNode.isNull()) { return defaultValue; } if (!encodedNode.isTextual()) { throw new IllegalArgumentException("error before decoding " + fieldName + "." + " Expected textual or null field, but encountered " + encodedNode.getNodeType()); } JsonNode decodedNode = mapper.readTree(encodedNode.textValue()); if (!decodedNode.getNodeType().equals(defaultValue.getNodeType())) { throw new IllegalArgumentException("error after decoding" + fieldName + "." + " Expected decoded type to be " + defaultValue.getNodeType() + ", but encountered " + decodedNode.getNodeType()); } return (T) decodedNode; }
private static void addHeader(DeserializationContext ctx, HttpHeaders headers, AsciiString name, JsonNode valueNode) throws JsonMappingException { if (!valueNode.isTextual()) { ctx.reportInputMismatch(HttpHeaders.class, "HTTP header '%s' contains %s (%s); only strings are allowed.", name, valueNode.getNodeType(), valueNode); } headers.add(name, valueNode.asText()); } }
log.warn(prefix + "has invalid \"timestamp\": {} (type: {})", timestampNode.asText(), timestampNode.getNodeType().name());
switch (read.getNodeType()) { case ARRAY: return ImmutableList.copyOf(read.elements());
if (versionNode.getNodeType() != JsonNodeType.MISSING) { clusterVersion = versionNode.asText();
@Test public void shouldThrowIfAnExtraordinaryExceptionHappensWhenParsingAsGenericMap() throws Exception { JsonNode value = mock(ObjectNode.class); when(value.getNodeType()).thenReturn(JsonNodeType.OBJECT); ObjectReader mockedMapper = mock(ObjectReader.class); JsonNodeClaim claim = (JsonNodeClaim) JsonNodeClaim.claimFromNode(value, mockedMapper); JsonNodeClaim spiedClaim = spy(claim); JsonParser mockedParser = mock(JsonParser.class); when(mockedMapper.treeAsTokens(value)).thenReturn(mockedParser); when(mockedParser.readValueAs(ArgumentMatchers.any(TypeReference.class))).thenThrow(IOException.class); exception.expect(JWTDecodeException.class); spiedClaim.asMap(); }
@Override public PathDetail deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonNode pathDetail = jp.readValueAsTree(); if (pathDetail.size() != 3) throw new JsonParseException(jp, "PathDetail array must have exactly 3 entries but was " + pathDetail.size()); JsonNode from = pathDetail.get(0); JsonNode to = pathDetail.get(1); JsonNode val = pathDetail.get(2); PathDetail pd; if (val.isBoolean()) pd = new PathDetail(val.asBoolean()); else if (val.isLong()) pd = new PathDetail(val.asLong()); else if (val.isInt()) pd = new PathDetail(val.asInt()); else if (val.isDouble()) pd = new PathDetail(val.asDouble()); else if (val.isTextual()) pd = new PathDetail(val.asText()); else throw new JsonParseException(jp, "Unsupported type of PathDetail value " + pathDetail.getNodeType().name()); pd.setFirst(from.asInt()); pd.setLast(to.asInt()); return pd; } }
@Override public int readI32() throws TException { final Class<?> fieldClass = getCurrentFieldClassIfIs(TEnum.class); if (fieldClass != null) { // Enum fields may be set by string, even though they represent integers. getCurrentContext().read(); final JsonNode elem = getCurrentContext().getCurrentChild(); if (elem.isInt()) { return TypedParser.INTEGER.readFromJsonElement(elem); } else if (elem.isTextual()) { // All TEnum are enums @SuppressWarnings({ "unchecked", "rawtypes" }) final TEnum tEnum = (TEnum) Enum.valueOf((Class<Enum>) fieldClass, TypedParser.STRING.readFromJsonElement(elem)); return tEnum.getValue(); } else { throw new TTransportException("invalid value type for enum field: " + elem.getNodeType() + " (" + elem + ')'); } } else { return readNameOrValue(TypedParser.INTEGER); } }