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

The following examples show how to use com.fasterxml.jackson.databind.JsonNode#floatValue() . 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: OutputsCapturer.java    From cwlexec with Apache License 2.0 6 votes vote down vote up
private static Object toValueByJson(CWLTypeSymbol outputType, JsonNode jsonNode) {
    switch (outputType) {
    case STRING:
        return jsonNode.asText();
    case INT:
        return jsonNode.asInt();
    case FLOAT:
        return jsonNode.floatValue();
    case DOUBLE:
        return jsonNode.asDouble();
    case BOOLEAN:
        return jsonNode.asBoolean();
    default:
        return null;
    }
}
 
Example 2
Source File: Float32TypeParser.java    From connect-utils with Apache License 2.0 6 votes vote down vote up
@Override
public Object parseJsonNode(JsonNode input, Schema schema) {
  Object result;
  if (input.isNumber()) {
    result = input.floatValue();
  } else if (input.isTextual()) {
    result = parseString(input.textValue(), schema);
  } else {
    throw new UnsupportedOperationException(
        String.format(
            "Could not parse '%s' to %s",
            input,
            this.expectedClass().getSimpleName()
        )
    );
  }
  return result;
}
 
Example 3
Source File: PartitionData.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Object getValue(JsonNode partitionValue, Type type)
{
    if (partitionValue.isNull()) {
        return null;
    }
    switch (type.typeId()) {
        case BOOLEAN:
            return partitionValue.asBoolean();
        case INTEGER:
        case DATE:
            return partitionValue.asInt();
        case LONG:
        case TIMESTAMP:
            return partitionValue.asLong();
        case FLOAT:
            return partitionValue.floatValue();
        case DOUBLE:
            return partitionValue.doubleValue();
        case STRING:
        case UUID:
            return partitionValue.asText();
        case FIXED:
        case BINARY:
            try {
                return partitionValue.binaryValue();
            }
            catch (IOException e) {
                throw new UncheckedIOException("Failed during JSON conversion of " + partitionValue, e);
            }
        case DECIMAL:
            return partitionValue.decimalValue();
    }
    throw new UnsupportedOperationException("Type not supported as partition column: " + type);
}
 
Example 4
Source File: AtlasGraphSONUtility.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static Object getTypedValueFromJsonNode(JsonNode node) {
    Object theValue = null;

    if (node != null && !node.isNull()) {
        if (node.isBoolean()) {
            theValue = node.booleanValue();
        } else if (node.isDouble()) {
            theValue = node.doubleValue();
        } else if (node.isFloatingPointNumber()) {
            theValue = node.floatValue();
        } else if (node.isInt()) {
            theValue = node.intValue();
        } else if (node.isLong()) {
            theValue = node.longValue();
        } else if (node.isTextual()) {
            theValue = node.textValue();
        } else if (node.isArray()) {
            // this is an array so just send it back so that it can be
            // reprocessed to its primitive components
            theValue = node;
        } else if (node.isObject()) {
            // this is an object so just send it back so that it can be
            // reprocessed to its primitive components
            theValue = node;
        } else {
            theValue = node.textValue();
        }
    }

    return theValue;
}
 
Example 5
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 6
Source File: AtlasGraphSONUtility.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
private static Object getTypedValueFromJsonNode(JsonNode node) {
    Object theValue = null;

    if (node != null && !node.isNull()) {
        if (node.isBoolean()) {
            theValue = node.booleanValue();
        } else if (node.isDouble()) {
            theValue = node.doubleValue();
        } else if (node.isFloatingPointNumber()) {
            theValue = node.floatValue();
        } else if (node.isInt()) {
            theValue = node.intValue();
        } else if (node.isLong()) {
            theValue = node.longValue();
        } else if (node.isTextual()) {
            theValue = node.textValue();
        } else if (node.isArray()) {
            // this is an array so just send it back so that it can be
            // reprocessed to its primitive components
            theValue = node;
        } else if (node.isObject()) {
            // this is an object so just send it back so that it can be
            // reprocessed to its primitive components
            theValue = node;
        } else {
            theValue = node.textValue();
        }
    }

    return theValue;
}
 
Example 7
Source File: JsonExampleDeserializer.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
private Example createExample(JsonNode node) {
    if (node instanceof ObjectNode) {
        ObjectExample obj = new ObjectExample();
        ObjectNode on = (ObjectNode) node;
        for (Iterator<Entry<String, JsonNode>> x = on.fields(); x.hasNext(); ) {
            Entry<String, JsonNode> i = x.next();
            String key = i.getKey();
            JsonNode value = i.getValue();
            obj.put(key, createExample(value));
        }
        return obj;
    } else if (node instanceof ArrayNode) {
        ArrayExample arr = new ArrayExample();
        ArrayNode an = (ArrayNode) node;
        for (JsonNode childNode : an) {
            arr.add(createExample(childNode));
        }
        return arr;
    } else if (node instanceof DoubleNode) {
        return new DoubleExample(node.doubleValue());
    } else if (node instanceof IntNode || node instanceof ShortNode) {
        return new IntegerExample(node.intValue());
    } else if (node instanceof FloatNode) {
        return new FloatExample(node.floatValue());
    } else if (node instanceof BigIntegerNode) {
        return new BigIntegerExample(node.bigIntegerValue());
    } else if (node instanceof DecimalNode) {
        return new DecimalExample(node.decimalValue());
    } else if (node instanceof LongNode) {
        return new LongExample(node.longValue());
    } else if (node instanceof BooleanNode) {
        return new BooleanExample(node.booleanValue());
    } else {
        return new StringExample(node.asText());
    }
}
 
Example 8
Source File: LanguageID.java    From storm-crawler with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(Map stormConf, JsonNode filterParams) {
    JsonNode node = filterParams.get("key");
    if (node != null && node.isTextual()) {
        mdKey = node.asText("lang");
    }
    node = filterParams.get("minProb");
    if (node != null && node.isNumber()) {
        minProb = node.floatValue();
    }
    node = filterParams.get("extracted");
    if (node != null && node.isTextual()) {
        extractedKeyName = node.asText("parse.lang");
    }
}
 
Example 9
Source File: Attribute.java    From zentity with Apache License 2.0 4 votes vote down vote up
private void validateScore(JsonNode value) throws ValidationException {
    if (!value.isNull() && !value.isFloatingPointNumber())
        throw new ValidationException("'attributes." + this.name + ".score' must be a floating point number.");
    if (value.isFloatingPointNumber() && (value.floatValue() < 0.0 || value.floatValue() > 1.0))
        throw new ValidationException("'attributes." + this.name + ".score' must be in the range of 0.0 - 1.0.");
}
 
Example 10
Source File: IndexField.java    From zentity with Apache License 2.0 4 votes vote down vote up
private void validateQuality(JsonNode value) throws ValidationException {
    if (!value.isNull() && !value.isFloatingPointNumber())
        throw new ValidationException("'indices." + this.index + "." + this.name + ".quality' must be a floating point number.");
    if (value.isFloatingPointNumber() && (value.floatValue() < 0.0 || value.floatValue() > 1.0))
        throw new ValidationException("'indices." + this.index + "." + this.name + ".quality' must be in the range of 0.0 - 1.0.");
}
 
Example 11
Source File: Matcher.java    From zentity with Apache License 2.0 4 votes vote down vote up
private void validateQuality(JsonNode value) throws ValidationException {
    if (!value.isNull() && !value.isFloatingPointNumber())
        throw new ValidationException("'matchers." + this.name + ".quality' must be a floating point number.");
    if (value.isFloatingPointNumber() && (value.floatValue() < 0.0 || value.floatValue() > 1.0))
        throw new ValidationException("'matchers." + this.name + ".quality' must be in the range of 0.0 - 1.0.");
}
 
Example 12
Source File: JsonConvert.java    From DBus with Apache License 2.0 4 votes vote down vote up
@Override
public Object convert(Schema schema, JsonNode value) {
    return value.floatValue();
}
 
Example 13
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;
    }
}