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

The following examples show how to use com.fasterxml.jackson.databind.JsonNode#isFloat() . 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: JsonUtils.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
public static Object parserValue(JsonNode node) {
    if (node == null || node.isNull()) {
        return null;
    }
    if (node.isArray()) {
        return parserArrayNode((ArrayNode) node);
    } else if (node.isObject()) {
        return parserMap((ObjectNode) node);
    } else {
        if (node.isBigDecimal() || node.isBigInteger() || node.isLong()) {
            return node.asLong();
        } else if (node.isFloat() || node.isDouble()) {
            return node.asDouble();
        } else if (node.isInt() || node.isNumber() || node.isShort()) {
            return node.asInt();
        } else if (node.isBoolean()) {
            return node.asBoolean();
        } else if (node.isTextual()) {
            return node.asText();
        } else {// 其他类型
            return node.textValue();
        }
    }
}
 
Example 2
Source File: WorkflowParser.java    From cwlexec with Apache License 2.0 6 votes vote down vote up
private static Object toDefaultValue(String descTop,
        String id,
        JsonNode defaultNode) throws CWLException {
    Object value = null;
    if (defaultNode != null) {
        if (defaultNode.isTextual()) {
            value = defaultNode.asText();
        } else if (defaultNode.isInt()) {
            value = Integer.valueOf(defaultNode.asInt());
        } else if (defaultNode.isLong()) {
            value = Long.valueOf(defaultNode.asLong());
        } else if (defaultNode.isFloat()) {
            value = Float.valueOf(defaultNode.floatValue());
        } else if (defaultNode.isDouble()) {
            value = Double.valueOf(defaultNode.asDouble());
        } else if (defaultNode.isBoolean()) {
            value = Boolean.valueOf(defaultNode.asBoolean());
        } else if (defaultNode.isArray()) {
            value = toDefaultArrayValue(descTop, id, defaultNode);
        } else if (defaultNode.isObject()) {
            value = toDefaultObjectValue(descTop, id, defaultNode);
        }
    }
    return value;
}
 
Example 3
Source File: JSONReader.java    From bigquery-etl-dataflow-sample with Apache License 2.0 6 votes vote down vote up
/**
 * This method attempts to transform the json node into an object with a known type.
 *
 * @return an Object with the apparent type from JSON (number types are given their wide equivalent (Long for
 * ints, Double for float)
 */
private static Object nodeValueToObject(JsonNode node) { //No child objects or arrays in this flat data just text/number
  switch (node.getNodeType()) {
    case NUMBER:
      if (node.isFloat() || node.isDouble()) {
        return new Double(node.doubleValue());
      } else {
        //For simplicity let all integers be Long.
        return new Long(node.asLong());
      }
    case STRING:
      return node.asText();
    case BOOLEAN:
      return node.asBoolean();
    case NULL:
      return null;
    default:
      logger.warn("Unknown node type:" + node.getNodeType());
      return null;
  }
}
 
Example 4
Source File: JsonUtils.java    From template-compiler with Apache License 2.0 6 votes vote down vote up
/**
 * Compare two JsonNode objects and return an integer.
 *
 * @return  a negative integer, zero, or a positive integer as this object
 *          is less than, equal to, or greater than the specified object.
 */
public static int compare(JsonNode left, JsonNode right) {
  if (left.isLong() || left.isInt()) {
    return Long.compare(left.asLong(), right.asLong());

  } else if (left.isDouble() || left.isFloat()) {
    return Double.compare(left.asDouble(), right.asDouble());

  } else if (left.isTextual()) {
    return left.asText().compareTo(right.asText());

  } else if (left.isBoolean()) {
    return Boolean.compare(left.asBoolean(), right.asBoolean());
  }

  // Not comparable in a relative sense, default to equals.
  return left.equals(right) ? 0 : -1;
}
 
Example 5
Source File: JsonBlockSerializer.java    From succinct with Apache License 2.0 6 votes vote down vote up
private DataType getNodeType(JsonNode node) {
  if (node.isTextual()) {
    return DataType.STRING;
  } else if (node.isBoolean()) {
    return DataType.BOOLEAN;
  } else if (node.isInt()) {
    return DataType.INT;
  } else if (node.isLong()) {
    return DataType.LONG;
  } else if (node.isFloat()) {
    return DataType.FLOAT;
  } else if (node.isDouble()) {
    return DataType.DOUBLE;
  } else {
    throw new UnsupportedOperationException("JSON DataType not supported.");
  }
}
 
Example 6
Source File: PrimitiveTypeProvider.java    From cineast with MIT License 5 votes vote down vote up
public static PrimitiveTypeProvider fromJSON(JsonNode json) {
  if (json == null) {
    return NothingProvider.INSTANCE;
  }

  if (json.isTextual()) {
    return new StringTypeProvider(json.asText());
  }

  if (json.isInt()) {
    return new IntTypeProvider(json.asInt());
  }

  if (json.isLong()) {
    return new LongTypeProvider(json.asLong());
  }

  if (json.isFloat()) {
    return new FloatTypeProvider(json.floatValue());
  }

  if (json.isDouble()) {
    return new DoubleTypeProvider(json.doubleValue());
  }

  if (json.isBoolean()) {
    return new BooleanTypeProvider(json.asBoolean());
  }

  // TODO are arrays relevant here?
  return NothingProvider.INSTANCE;
}
 
Example 7
Source File: JsonNodeELResolver.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
/**
 * If the base object is a map, returns the value associated with the given key, as specified by the property argument. If the key was not found, null is returned. If the base is a Map, the
 * propertyResolved property of the ELContext object must be set to true by this resolver, before returning. If this property is not true after this method is called, the caller should ignore the
 * return value. Just as in java.util.Map.get(Object), just because null is returned doesn't mean there is no mapping for the key; it's also possible that the Map explicitly maps the key to null.
 * 
 * @param context
 *            The context of this evaluation.
 * @param base
 *            The map to analyze. Only bases of type Map are handled by this resolver.
 * @param property
 *            The key to return the acceptable type for. Ignored by this resolver.
 * @return If the propertyResolved property of ELContext was set to true, then the value associated with the given key or null if the key was not found. Otherwise, undefined.
 * @throws ClassCastException
 *             if the key is of an inappropriate type for this map (optionally thrown by the underlying Map).
 * @throws NullPointerException
 *             if context is null, or if the key is null and this map does not permit null keys (the latter is optionally thrown by the underlying Map).
 * @throws ELException
 *             if an exception was thrown while performing the property or variable resolution. The thrown exception must be included as the cause property of this exception, if available.
 */
@Override
public Object getValue(ELContext context, Object base, Object property) {
    if (context == null) {
        throw new NullPointerException("context is null");
    }
    Object result = null;
    if (isResolvable(base)) {
        JsonNode resultNode = getResultNode((JsonNode) base, property, context);
        if (resultNode != null && resultNode.isValueNode()) {
            if (resultNode.isBoolean()) {
                result = resultNode.asBoolean();
            } else if (resultNode.isShort() || resultNode.isInt()) {
                result = resultNode.asInt();
            } else if (resultNode.isLong()) {
                result = resultNode.asLong();
            } else if (resultNode.isBigDecimal() || resultNode.isDouble() || resultNode.isFloat()) {
                result = resultNode.asDouble();
            } else if (resultNode.isTextual()) {
                result = resultNode.asText();
            } else if (resultNode.isNull()) {
                result = null;
            } else {
                result = resultNode.toString();
            }

        } else {
            result = resultNode;
        }
        context.setPropertyResolved(true);
    }
    return result;
}
 
Example 8
Source File: JsonDeserializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private EdmPrimitiveTypeKind guessPrimitiveTypeKind(final JsonNode node) {
  return node.isShort() ? EdmPrimitiveTypeKind.Int16 :
    node.isInt() ? EdmPrimitiveTypeKind.Int32 :
      node.isLong() ? EdmPrimitiveTypeKind.Int64 :
        node.isBoolean() ? EdmPrimitiveTypeKind.Boolean :
          node.isFloat() ? EdmPrimitiveTypeKind.Single :
            node.isDouble() ? EdmPrimitiveTypeKind.Double :
              node.isBigDecimal() ? EdmPrimitiveTypeKind.Decimal :
                EdmPrimitiveTypeKind.String;
}
 
Example 9
Source File: JSONConfigParser.java    From walkmod-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Map<String, Object> getParams(JsonNode next) {
	if (next.has("params")) {
		Iterator<Entry<String, JsonNode>> it2 = next.get("params").fields();
		Map<String, Object> params = new HashMap<String, Object>();
		while (it2.hasNext()) {
			Entry<String, JsonNode> param = it2.next();
			JsonNode value = param.getValue();
			if (value.isTextual()) {
				params.put(param.getKey(), value.asText());
			} else if (value.isInt()) {
				params.put(param.getKey(), value.asInt());
			} else if (value.isBoolean()) {
				params.put(param.getKey(), value.asBoolean());
			} else if (value.isDouble() || value.isFloat() || value.isBigDecimal()) {
				params.put(param.getKey(), value.asDouble());
			} else if (value.isLong() || value.isBigInteger()) {
				params.put(param.getKey(), value.asLong());
			} else {
				params.put(param.getKey(), value);
			}
			params.put(param.getKey(), param.getValue().asText());
		}
		return params;

	}
	return null;
}
 
Example 10
Source File: JSONConfigParser.java    From walkmod-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Map<String, Object> getParams(JsonNode next) {
	if (next.has("params")) {
		Iterator<Entry<String, JsonNode>> it2 = next.get("params").fields();
		Map<String, Object> params = new HashMap<String, Object>();
		while (it2.hasNext()) {
			Entry<String, JsonNode> param = it2.next();
			JsonNode value = param.getValue();
			if (value.isTextual()) {
				params.put(param.getKey(), value.asText());
			} else if (value.isInt()) {
				params.put(param.getKey(), value.asInt());
			} else if (value.isBoolean()) {
				params.put(param.getKey(), value.asBoolean());
			} else if (value.isDouble() || value.isFloat() || value.isBigDecimal()) {
				params.put(param.getKey(), value.asDouble());
			} else if (value.isLong() || value.isBigInteger()) {
				params.put(param.getKey(), value.asLong());
			} else {
				params.put(param.getKey(), value);
			}
			params.put(param.getKey(), param.getValue().asText());
		}
		return params;

	}
	return null;
}
 
Example 11
Source File: ExtractJsonPathsBuilder.java    From kite with Apache License 2.0 5 votes vote down vote up
private void resolve(JsonNode datum, Record record, String fieldName) { 
  if (datum == null) {
    return;
  }
  
  if (flatten) {
    flatten(datum, record.get(fieldName));
    return;
  }

  if (datum.isObject()) {
    record.put(fieldName, datum);
  } else if (datum.isArray()) {
    record.put(fieldName, datum);  
  } else if (datum.isTextual()) {
    record.put(fieldName, datum.asText());
  } else if (datum.isBoolean()) {
    record.put(fieldName, datum.asBoolean());
  } else if (datum.isInt()) {
    record.put(fieldName, datum.asInt());
  } else if (datum.isLong()) {
    record.put(fieldName, datum.asLong());
  } else if (datum.isShort()) {
    record.put(fieldName, datum.shortValue());
  } else if (datum.isDouble()) {
    record.put(fieldName, datum.asDouble());
  } else if (datum.isFloat()) {
    record.put(fieldName, datum.floatValue());
  } else if (datum.isBigInteger()) {
    record.put(fieldName, datum.bigIntegerValue());
  } else if (datum.isBigDecimal()) {
    record.put(fieldName, datum.decimalValue());
  } else if (datum.isNull()) {
    ; // ignore
  } else {
    record.put(fieldName, datum.toString());
  }
}
 
Example 12
Source File: ExtractJsonPathsBuilder.java    From kite with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void flatten(JsonNode datum, List list) { 
  if (datum == null) {
    return;
  }

  if (datum.isObject()) {
    for (JsonNode child : datum) {
      flatten(child, list);
    }
  } else if (datum.isArray()) {
    Iterator<JsonNode> iter = datum.elements();
    while (iter.hasNext()) {
      flatten(iter.next(), list);
    }        
  } else if (datum.isTextual()) {
    list.add(datum.asText());
  } else if (datum.isBoolean()) {
    list.add(datum.asBoolean());
  } else if (datum.isInt()) {
    list.add(datum.asInt());
  } else if (datum.isLong()) {
    list.add(datum.asLong());
  } else if (datum.isShort()) {
    list.add(datum.shortValue());
  } else if (datum.isDouble()) {
    list.add(datum.asDouble());
  } else if (datum.isFloat()) {
    list.add(datum.floatValue());
  } else if (datum.isBigInteger()) {
    list.add(datum.bigIntegerValue());
  } else if (datum.isBigDecimal()) {
    list.add(datum.decimalValue());
  } else if (datum.isNull()) {
    ; // ignore
  } else {
    list.add(datum.toString());
  }
}
 
Example 13
Source File: JsonFileReader.java    From kafka-connect-fs with Apache License 2.0 4 votes vote down vote up
private static Schema extractSchema(JsonNode jsonNode) {
    switch (jsonNode.getNodeType()) {
        case BOOLEAN:
            return Schema.OPTIONAL_BOOLEAN_SCHEMA;
        case NUMBER:
            if (jsonNode.isShort()) {
                return Schema.OPTIONAL_INT8_SCHEMA;
            } else if (jsonNode.isInt()) {
                return Schema.OPTIONAL_INT32_SCHEMA;
            } else if (jsonNode.isLong()) {
                return Schema.OPTIONAL_INT64_SCHEMA;
            } else if (jsonNode.isFloat()) {
                return Schema.OPTIONAL_FLOAT32_SCHEMA;
            } else if (jsonNode.isDouble()) {
                return Schema.OPTIONAL_FLOAT64_SCHEMA;
            } else if (jsonNode.isBigInteger()) {
                return Schema.OPTIONAL_INT64_SCHEMA;
            } else if (jsonNode.isBigDecimal()) {
                return Schema.OPTIONAL_FLOAT64_SCHEMA;
            } else {
                return Schema.OPTIONAL_FLOAT64_SCHEMA;
            }
        case STRING:
            return Schema.OPTIONAL_STRING_SCHEMA;
        case BINARY:
            return Schema.OPTIONAL_BYTES_SCHEMA;
        case ARRAY:
            Iterable<JsonNode> elements = jsonNode::elements;
            Schema arraySchema = StreamSupport.stream(elements.spliterator(), false)
                    .findFirst().map(JsonFileReader::extractSchema)
                    .orElse(SchemaBuilder.struct().build());
            return SchemaBuilder.array(arraySchema).build();
        case OBJECT:
            SchemaBuilder builder = SchemaBuilder.struct();
            jsonNode.fields()
                    .forEachRemaining(field -> builder.field(field.getKey(), extractSchema(field.getValue())));
            return builder.build();
        default:
            return SchemaBuilder.struct().optional().build();
    }
}
 
Example 14
Source File: JsonFileReader.java    From kafka-connect-fs with Apache License 2.0 4 votes vote down vote up
private Object mapValue(Schema schema, JsonNode value) {
    if (value == null) return null;

    switch (value.getNodeType()) {
        case BOOLEAN:
            return value.booleanValue();
        case NUMBER:
            if (value.isShort()) {
                return value.shortValue();
            } else if (value.isInt()) {
                return value.intValue();
            } else if (value.isLong()) {
                return value.longValue();
            } else if (value.isFloat()) {
                return value.floatValue();
            } else if (value.isDouble()) {
                return value.doubleValue();
            } else if (value.isBigInteger()) {
                return value.bigIntegerValue();
            } else {
                return value.numberValue();
            }
        case STRING:
            return value.asText();
        case BINARY:
            try {
                return value.binaryValue();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        case OBJECT:
        case POJO:
            Struct struct = new Struct(schema);
            Iterable<Map.Entry<String, JsonNode>> fields = value::fields;
            StreamSupport.stream(fields.spliterator(), false)
                    .forEach(field -> struct.put(field.getKey(),
                            mapValue(extractSchema(field.getValue()), field.getValue()))
                    );
            return struct;
        case ARRAY:
            Iterable<JsonNode> arrayElements = value::elements;
            return StreamSupport.stream(arrayElements.spliterator(), false)
                    .map(elm -> mapValue(schema, elm))
                    .collect(Collectors.toList());
        case NULL:
        case MISSING:
        default:
            return null;
    }
}
 
Example 15
Source File: AvroJson.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
private static boolean matches(JsonNode datum, Schema schema) {
  switch (schema.getType()) {
    case RECORD:
      if (datum.isObject()) {
        // check that each field is present or has a default
        boolean missingField = false;
        for (Schema.Field field : schema.getFields()) {
          if (!datum.has(field.name()) && field.defaultVal() == null) {
            missingField = true;
            break;
          }
        }
        if (!missingField) {
          return true;
        }
      }
      break;
    case UNION:
      if (resolveUnion(datum, schema.getTypes()) != null) {
        return true;
      }
      break;
    case MAP:
      if (datum.isObject()) {
        return true;
      }
      break;
    case ARRAY:
      if (datum.isArray()) {
        return true;
      }
      break;
    case BOOLEAN:
      if (datum.isBoolean()) {
        return true;
      }
      break;
    case FLOAT:
      if (datum.isFloat() || datum.isInt()) {
        return true;
      }
      break;
    case DOUBLE:
      if (datum.isDouble() || datum.isFloat() ||
          datum.isLong() || datum.isInt()) {
        return true;
      }
      break;
    case INT:
      if (datum.isInt()) {
        return true;
      }
      break;
    case LONG:
      if (datum.isLong() || datum.isInt()) {
        return true;
      }
      break;
    case STRING:
      if (datum.isTextual()) {
        return true;
      }
      break;
    case ENUM:
      if (datum.isTextual() && schema.hasEnumSymbol(datum.textValue())) {
        return true;
      }
      break;
    case BYTES:
    case FIXED:
      if (datum.isBinary()) {
        return true;
      }
      break;
    case NULL:
      if (datum == null || datum.isNull()) {
        return true;
      }
      break;
    default: // UNION or unknown
      throw new IllegalArgumentException("Unsupported schema: " + schema);
  }
  return false;
}
 
Example 16
Source File: JsonUtil.java    From kite with Apache License 2.0 4 votes vote down vote up
private static boolean matches(JsonNode datum, Schema schema) {
  switch (schema.getType()) {
    case RECORD:
      if (datum.isObject()) {
        // check that each field is present or has a default
        boolean missingField = false;
        for (Schema.Field field : schema.getFields()) {
          if (!datum.has(field.name()) && field.defaultValue() == null) {
            missingField = true;
            break;
          }
        }
        if (!missingField) {
          return true;
        }
      }
      break;
    case UNION:
      if (resolveUnion(datum, schema.getTypes()) != null) {
        return true;
      }
      break;
    case MAP:
      if (datum.isObject()) {
        return true;
      }
      break;
    case ARRAY:
      if (datum.isArray()) {
        return true;
      }
      break;
    case BOOLEAN:
      if (datum.isBoolean()) {
        return true;
      }
      break;
    case FLOAT:
      if (datum.isFloat() || datum.isInt()) {
        return true;
      }
      break;
    case DOUBLE:
      if (datum.isDouble() || datum.isFloat() ||
          datum.isLong() || datum.isInt()) {
        return true;
      }
      break;
    case INT:
      if (datum.isInt()) {
        return true;
      }
      break;
    case LONG:
      if (datum.isLong() || datum.isInt()) {
        return true;
      }
      break;
    case STRING:
      if (datum.isTextual()) {
        return true;
      }
      break;
    case ENUM:
      if (datum.isTextual() && schema.hasEnumSymbol(datum.textValue())) {
        return true;
      }
      break;
    case BYTES:
    case FIXED:
      if (datum.isBinary()) {
        return true;
      }
      break;
    case NULL:
      if (datum == null || datum.isNull()) {
        return true;
      }
      break;
    default: // UNION or unknown
      throw new IllegalArgumentException("Unsupported schema: " + schema);
  }
  return false;
}