Java Code Examples for com.fasterxml.jackson.databind.node.JsonNodeType#NUMBER

The following examples show how to use com.fasterxml.jackson.databind.node.JsonNodeType#NUMBER . 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: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
protected JsonNodeType nodeTypeFor(Object value) {
    JsonNodeType type;
    if (value == null) {
        type = JsonNodeType.NULL;
    } else if (value instanceof Boolean) {
        type = JsonNodeType.BOOLEAN;
    } else if (value instanceof Number) {
        type = JsonNodeType.NUMBER;
    } else if (value instanceof String) {
        type = JsonNodeType.STRING;
    } else if (value instanceof ArrayNode || value instanceof List) {
        type = JsonNodeType.ARRAY;
    } else if (value instanceof byte[]) {
        type = JsonNodeType.BINARY;
    } else if (value instanceof ObjectNode || value instanceof Map) {
        type = JsonNodeType.OBJECT;
    } else {
        type = JsonNodeType.POJO;
    }
    return type;
}
 
Example 2
Source File: GeneralUtils.java    From template-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Convert an opaque JSON node to Decimal using the most correct
 * conversion method.
 */
public static Decimal nodeToDecimal(JsonNode node) {
  JsonNodeType type = node.getNodeType();
  if (type == JsonNodeType.NUMBER) {
    return numericToDecimal((NumericNode)node);

  } else {
    try {
      return new Decimal(node.asText());
    } catch (ArithmeticException | NumberFormatException e) {
      // Fall through..
    }
  }
  return null;
}
 
Example 3
Source File: GeneralUtils.java    From template-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Convert an opaque JSON node to BigDecimal using the most correct
 * conversion method.
 */
public static BigDecimal nodeToBigDecimal(JsonNode node) {
  JsonNodeType type = node.getNodeType();
  if (type == JsonNodeType.NUMBER) {
    return numericToBigDecimal((NumericNode)node);

  } else {
    try {
      return new BigDecimal(node.asText());
    } catch (ArithmeticException | NumberFormatException e) {
      // Fall through..
    }
  }
  return null;
}
 
Example 4
Source File: CustomAttributeDeserializer.java    From intercom-java with Apache License 2.0 5 votes vote down vote up
@Override
public CustomAttribute deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    CustomAttribute cda = null;
    final String currentName = jp.getParsingContext().getCurrentName();
    final ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    final ValueNode vNode = mapper.readTree(jp);
    if (vNode.asToken().isScalarValue()) {
        if (vNode.getNodeType() == JsonNodeType.BOOLEAN) {
            cda = new CustomAttribute<Boolean>(currentName, vNode.asBoolean(), Boolean.class);
        } else if (vNode.getNodeType() == JsonNodeType.STRING) {
            cda = new CustomAttribute<String>(currentName, vNode.asText(), String.class);
        } else if (vNode.getNodeType() == JsonNodeType.NUMBER) {
            final NumericNode nNode = (NumericNode) vNode;
            if (currentName.endsWith("_at")) {
                cda = new CustomAttribute<Long>(currentName, vNode.longValue(), Long.class);
            } else if (nNode.isInt()) {
                cda = new CustomAttribute<Integer>(currentName, vNode.intValue(), Integer.class);
            } else if (nNode.isFloat()) {
                cda = new CustomAttribute<Float>(currentName, vNode.floatValue(), Float.class);
            } else if (nNode.isDouble()) {
                cda = new CustomAttribute<Double>(currentName, vNode.doubleValue(), Double.class);
            } else if (nNode.isLong()) {
                cda = new CustomAttribute<Long>(currentName, vNode.longValue(), Long.class);
            } else {
                cda = new CustomAttribute<String>(currentName, vNode.asText(), String.class);
            }
        } else {
            cda = new CustomAttribute<String>(currentName, vNode.asText(), String.class);
        }
    }
    return cda;
}
 
Example 5
Source File: JsonQueryObjectModelConverter.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
private void parseProperties(QueryPart queryPart, ObjectNode properties) throws QueryException {
	Iterator<Entry<String, JsonNode>> fields = properties.fields();
	while (fields.hasNext()) {
		Entry<String, JsonNode> entry = fields.next();
		String propertySetName = entry.getKey();
		JsonNode value = entry.getValue();
		if (value.isObject()) {
			ObjectNode set = (ObjectNode)value;
			Iterator<Entry<String, JsonNode>> propertySetFields = set.fields();
			while (propertySetFields.hasNext()) {
				Entry<String, JsonNode> propertyEntry = propertySetFields.next();
				JsonNode propertyValue = propertyEntry.getValue();
				
				if (propertyValue.isValueNode()) {
					if (propertyValue.getNodeType() == JsonNodeType.BOOLEAN) {
						queryPart.addProperty(propertySetName, propertyEntry.getKey(), propertyValue.asBoolean());
					} else if (propertyValue.getNodeType() == JsonNodeType.NUMBER) {
						queryPart.addProperty(propertySetName, propertyEntry.getKey(), propertyValue.asDouble());
					} else if (propertyValue.getNodeType() == JsonNodeType.STRING) {
						queryPart.addProperty(propertySetName, propertyEntry.getKey(), propertyValue.asText());
					} else if (propertyValue.getNodeType() == JsonNodeType.NULL) {
						queryPart.addProperty(propertySetName, propertyEntry.getKey(), null);
					}
				} else {
					throw new QueryException("property \"" + propertyEntry.getKey() + "\" type not supported");
				}
			}				
		} else {
			throw new QueryException("Query language has changed, propertyset name required now");
		}
	}
}
 
Example 6
Source File: JsonNode.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return True if this node represents a numeric JSON value
 */
public final boolean isNumber() {
    return getNodeType() == JsonNodeType.NUMBER;
}
 
Example 7
Source File: IsJsonNumber.java    From java-hamcrest with Apache License 2.0 4 votes vote down vote up
private IsJsonNumber(final Matcher<?> numberMatcher,
                     final Function<NumericNode, Object> projection) {
  super(JsonNodeType.NUMBER);
  this.numberMatcher = Objects.requireNonNull(numberMatcher);
  this.projection = Objects.requireNonNull(projection);
}