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

The following examples show how to use com.fasterxml.jackson.databind.JsonNode#isShort() . 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 String toString(JsonNode node) {
    if (node == null || node.isNull()) {
        return null;
    }
    if (node.isValueNode()) {
        if (node.isBoolean()) {
            return String.valueOf(node.asBoolean());
        } else if (node.isBigInteger()) {
            return String.valueOf(node.bigIntegerValue());
        } else if (node.isDouble()) {
            return String.valueOf(node.asDouble());
        } else if (node.isInt()) {
            return String.valueOf(node.intValue());
        } else if (node.isLong()) {
            return String.valueOf(node.asLong());
        } else if (node.isShort()) {
            return String.valueOf(node.shortValue());
        } else {
            return node.asText();
        }
    }
    return node.toString();
}
 
Example 2
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 3
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 4
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 5
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 6
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 7
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 8
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;
    }
}