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

The following examples show how to use com.fasterxml.jackson.databind.JsonNode#numberValue() . 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: DefaultPatchService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Object getValue( JsonNode node )
{
    switch ( node.getNodeType() )
    {
        case BOOLEAN:
            return node.booleanValue();
        case NUMBER:
            return node.numberValue();
        case STRING:
            return node.textValue();
        case NULL:
            return null;
    }

    return null;
}
 
Example 2
Source File: Json.java    From immutables with Apache License 2.0 6 votes vote down vote up
private static Bucket parseBucket(JsonParser parser, String name, ObjectNode node)
        throws JsonProcessingException  {

  if (!node.has("key")) {
    throw new IllegalArgumentException("No 'key' attribute for " + node);
  }

  final JsonNode keyNode = node.get("key");
  final Object key;
  if (isMissingBucket(keyNode) || keyNode.isNull()) {
    key = null;
  } else if (keyNode.isTextual()) {
    key = keyNode.textValue();
  } else if (keyNode.isNumber()) {
    key = keyNode.numberValue();
  } else if (keyNode.isBoolean()) {
    key = keyNode.booleanValue();
  } else {
    // don't usually expect keys to be Objects
    key = parser.getCodec().treeToValue(node, Map.class);
  }

  return new Bucket(key, name, parseAggregations(parser, node));
}
 
Example 3
Source File: ElasticsearchJson.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static Bucket parseBucket(JsonParser parser, String name, ObjectNode node)
    throws JsonProcessingException  {

  if (!node.has("key")) {
    throw new IllegalArgumentException("No 'key' attribute for " + node);
  }

  final JsonNode keyNode = node.get("key");
  final Object key;
  if (isMissingBucket(keyNode) || keyNode.isNull()) {
    key = null;
  } else if (keyNode.isTextual()) {
    key = keyNode.textValue();
  } else if (keyNode.isNumber()) {
    key = keyNode.numberValue();
  } else if (keyNode.isBoolean()) {
    key = keyNode.booleanValue();
  } else {
    // don't usually expect keys to be Objects
    key = parser.getCodec().treeToValue(node, Map.class);
  }

  return new Bucket(key, name, parseAggregations(parser, node));
}
 
Example 4
Source File: GenericJsonRecord.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public Object getField(String fieldName) {
    JsonNode fn = jn.get(fieldName);
    if (fn.isContainerNode()) {
        AtomicInteger idx = new AtomicInteger(0);
        List<Field> fields = Lists.newArrayList(fn.fieldNames())
            .stream()
            .map(f -> new Field(f, idx.getAndIncrement()))
            .collect(Collectors.toList());
        return new GenericJsonRecord(schemaVersion, fields, fn);
    } else if (fn.isBoolean()) {
        return fn.asBoolean();
    } else if (fn.isFloatingPointNumber()) {
        return fn.asDouble();
    } else if (fn.isBigInteger()) {
        if (fn.canConvertToLong()) {
            return fn.asLong();
        } else {
            return fn.asText();
        }
    } else if (fn.isNumber()) {
        return fn.numberValue();
    } else {
        return fn.asText();
    }
}
 
Example 5
Source File: RangeBoundValueDeserializer.java    From presto with Apache License 2.0 6 votes vote down vote up
private Object toValue(JsonNode node)
        throws IOException
{
    if (node.isTextual()) {
        return node.asText();
    }
    else if (node.isNumber()) {
        return node.numberValue();
    }
    else if (node.isBoolean()) {
        return node.asBoolean();
    }
    else if (node.isBinary()) {
        return node.binaryValue();
    }
    else {
        throw new IllegalStateException("Unexpected range bound value: " + node);
    }
}
 
Example 6
Source File: YqlFallbackLinker.java    From yql-plus with Apache License 2.0 6 votes vote down vote up
public static Object readProperty(JsonNode node, String property) throws IOException {
    JsonNode result = node.get(property);
    if (result == null) {
        return null;
    }
    switch (result.getNodeType()) {
        case NUMBER:
            return result.numberValue();
        case BOOLEAN:
            return result.booleanValue();
        case NULL:
            return null;
        case STRING:
            return result.asText();
        case BINARY:
            return ByteBuffer.wrap(result.binaryValue());
        case MISSING:
        case OBJECT:
        case POJO:
        case ARRAY:
        default:
            return result;
    }

}
 
Example 7
Source File: JsonNodeLinker.java    From yql-plus with Apache License 2.0 6 votes vote down vote up
public static Object readProperty(JsonNode node, String property) throws IOException {
    JsonNode result = node.get(property);
    if (result == null) {
        return null;
    }
    switch (result.getNodeType()) {
        case NUMBER:
            return result.numberValue();
        case BOOLEAN:
            return result.booleanValue();
        case NULL:
            return null;
        case STRING:
            return result.asText();
        case BINARY:
            return ByteBuffer.wrap(result.binaryValue());
        case MISSING:
        case OBJECT:
        case POJO:
        case ARRAY:
        default:
            return result;
    }

}
 
Example 8
Source File: JsonResultChunk.java    From snowflake-jdbc with Apache License 2.0 6 votes vote down vote up
public static Object extractCell(JsonNode resultData, int rowIdx, int colIdx)
{
  JsonNode currentRow = resultData.get(rowIdx);

  JsonNode colNode = currentRow.get(colIdx);

  if (colNode.isTextual())
  {
    return colNode.asText();
  }
  else if (colNode.isNumber())
  {
    return colNode.numberValue();
  }
  else if (colNode.isNull())
  {
    return null;
  }
  throw new RuntimeException("Unknow json type");
}
 
Example 9
Source File: ElasticsearchJson.java    From Quicksql with MIT License 6 votes vote down vote up
private static Bucket parseBucket(JsonParser parser, String name, ObjectNode node)
    throws JsonProcessingException  {

  final JsonNode keyNode = node.get("key");
  final Object key;
  if (isMissingBucket(keyNode) || keyNode.isNull()) {
    key = null;
  } else if (keyNode.isTextual()) {
    key = keyNode.textValue();
  } else if (keyNode.isNumber()) {
    key = keyNode.numberValue();
  } else if (keyNode.isBoolean()) {
    key = keyNode.booleanValue();
  } else {
    // don't usually expect keys to be Objects
    key = parser.getCodec().treeToValue(node, Map.class);
  }

  return new Bucket(key, name, parseAggregations(parser, node));
}
 
Example 10
Source File: RangeBoundValueDeserializer.java    From presto-kudu with Apache License 2.0 5 votes vote down vote up
private Object toValue(JsonNode node) throws IOException {
    if (node.isTextual()) {
        return node.asText();
    } else if (node.isNumber()) {
        return node.numberValue();
    } else if (node.isBoolean()) {
        return node.asBoolean();
    } else if (node.isBinary()) {
        return node.binaryValue();
    } else {
        throw new IllegalStateException("Unexpected range bound value: " + node);
    }
}
 
Example 11
Source File: JsonPathSelector.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
private Object unwrap(JsonNode node) {
  if (node.isValueNode()) {
    if (node.isNumber()) {
      return node.numberValue();
    } else if (node.isTextual()) {
      return node.asText();
    } else {
      return node;
    }
  } else {
    return node;
  }
}
 
Example 12
Source File: SerializableConverter.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private Serializable serializableNodeValue(JsonNode node) {
  if (node.isNumber()) {
    return node.numberValue();
  } else if (node.isBoolean()) {
    return node.booleanValue();
  } else if (node.isTextual()) {
    return node.textValue();
  } else {
    throw new RuntimeException("Unable to deserialize preference value:" + node.asText());
  }
}
 
Example 13
Source File: ElasticsearchJson.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static SearchTotal parseSearchTotal(JsonNode node) {

      final Number value;
      if (node.isNumber()) {
        value = node.numberValue();
      } else {
        value = node.get("value").numberValue();
      }

      return new SearchTotal(value.longValue());
    }
 
Example 14
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(JsonPointer ptr) throws CacheException {
    Object result;
    if (root == null) {
        result = null;
    } else {
        try {
            JsonNode node = root.at(ptr);
            switch (node.getNodeType()) {
                case ARRAY:
                case OBJECT:
                    result = node;
                    break;
                case BINARY:
                    result = node.binaryValue();
                    break;
                case BOOLEAN:
                    result = node.booleanValue();
                    break;
                case NUMBER:
                    result = node.numberValue();
                    break;
                case POJO:
                    result = ((POJONode) node).getPojo();
                    break;
                case STRING:
                    result = node.textValue();
                    break;
                default:
                    result = null;
                    break;
            }
        } catch (IOException e) {
            throw new CacheException(e);
        }
    }
    return result;
}
 
Example 15
Source File: RosettaBinder.java    From Rosetta with Apache License 2.0 5 votes vote down vote up
private Object unwrapJsonValue(JsonNode node) {
  if (node.isNull()) {
    return null;
  } else if (node.isBoolean()) {
    return node.booleanValue();
  } else if (node.isBinary()) {
    return ((BinaryNode) node).binaryValue();
  } else if (node.isNumber()) {
    return node.numberValue();
  } else {
    return node.asText();
  }
}
 
Example 16
Source File: Json.java    From immutables with Apache License 2.0 5 votes vote down vote up
private static SearchTotal parseSearchTotal(JsonNode node) {

      final Number value;
      if (node.isNumber()) {
        value = node.numberValue();
      } else {
        value = node.get("value").numberValue();
      }

      return new SearchTotal(value.longValue());
    }
 
Example 17
Source File: JacksonRuntime.java    From jmespath-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Number toNumber(JsonNode n) {
  return n.numberValue();
}
 
Example 18
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 19
Source File: DocumentAnalyzer.java    From azure-documentdb-java with MIT License 4 votes vote down vote up
private static PartitionKeyInternal extractPartitionKeyValueInternal(String documentAsString, PartitionKeyDefinition partitionKeyDefinition) {
    JsonNode root;
    try {
        root = objectMapper.readTree(documentAsString);

        Iterator<String> path = partitionKeyDefinition.getPaths().iterator();   
        JsonNode node =  root.path(path.next().substring(1));

        while(path.hasNext() && node != null) {
            node = node.path(path.next());
        }

        Object partitionKeyValue = null;

        if (node != null) {

            switch (node.getNodeType()) {
            case BOOLEAN:
                partitionKeyValue = node.booleanValue();
                break;
            case MISSING:
                partitionKeyValue = Undefined.Value();
                break;
            case NULL:
                partitionKeyValue = JSONObject.NULL;
                break;
            case NUMBER:
                partitionKeyValue = node.numberValue();
                break;
            case STRING:
                partitionKeyValue = node.textValue();
                break;
            default:
                throw new RuntimeException(String.format("undefined json type %s", node.getNodeType()));
            }
        } else {
            partitionKeyValue = Undefined.Value();
        }

        return fromPartitionKeyvalue(partitionKeyValue);

    } catch (Exception e) {
        LOGGER.error("Failed to extract partition key value from document {}", documentAsString, e);
        throw ExceptionUtils.toRuntimeException(e);
    }
}