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

The following examples show how to use com.fasterxml.jackson.databind.JsonNode#doubleValue() . 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: StdDevFunction.java    From SkaETL with Apache License 2.0 6 votes vote down vote up
@Override
public AggregateFunction addValue(JsonNode inputvalue) {
    Double value = inputvalue.doubleValue();
    if (count == 0) {
        count = 1;
        mean = value;
        if (!isFinite(value)) {
            sumOfSquaresOfDeltas = NaN;
        }
    } else {
        count++;
        if (isFinite(value) && isFinite(mean)) {
            Double delta = value - mean;
            mean += delta / count;
            sumOfSquaresOfDeltas += delta * (value - mean);
        } else {
            mean = calculateNewMeanNonFinite(mean, value);
            sumOfSquaresOfDeltas = NaN;
        }
    }
    return this;
}
 
Example 2
Source File: ThresholdMixinPerfTest.java    From json-schema-validator with Apache License 2.0 6 votes vote down vote up
@Override
public boolean crossesThreshold(JsonNode node) {
    if (maximumDecimal.isDouble() && maximumDecimal.doubleValue() == Double.POSITIVE_INFINITY) {
        return false;
    }
    if (maximumDecimal.isDouble() && maximumDecimal.doubleValue() == Double.NEGATIVE_INFINITY) {
        return true;
    }
    if (node.isDouble() && node.doubleValue() == Double.NEGATIVE_INFINITY) {
        return false;
    }
    if (node.isDouble() && node.doubleValue() == Double.POSITIVE_INFINITY) {
        return true;
    }
    final BigDecimal max = new BigDecimal(maximumText);
    BigDecimal value = new BigDecimal(node.asText());
    int compare = value.compareTo(max);
    return compare > 0 || (excludeEqual && compare == 0);
}
 
Example 3
Source File: ThresholdMixinPerfTest.java    From json-schema-validator with Apache License 2.0 6 votes vote down vote up
@Override
public boolean crossesThreshold(JsonNode node) {
    if (maximumDouble.isDouble() && maximumDouble.doubleValue() == Double.POSITIVE_INFINITY) {
        return false;
    }
    if (maximumDouble.isDouble() && maximumDouble.doubleValue() == Double.NEGATIVE_INFINITY) {
        return true;
    }
    if (node.isDouble() && node.doubleValue() == Double.NEGATIVE_INFINITY) {
        return false;
    }
    if (node.isDouble() && node.doubleValue() == Double.POSITIVE_INFINITY) {
        return true;
    }
    final BigDecimal max = new BigDecimal(maximumText);
    BigDecimal value = new BigDecimal(node.asText());
    int compare = value.compareTo(max);
    return compare > 0 || (excludeEqual && compare == 0);
}
 
Example 4
Source File: BuiltinFunctions.java    From jslt with Apache License 2.0 6 votes vote down vote up
public JsonNode call(JsonNode input, JsonNode[] arguments) {
  JsonNode array = arguments[0];
  if (array.isNull())
    return NullNode.instance;
  else if (!array.isArray())
    throw new JsltException("sum(): argument must be array, was " + array);

  double sum = 0.0;
  boolean integral = true;
  for (int ix = 0; ix < array.size(); ix++) {
    JsonNode value = array.get(ix);
    if (!value.isNumber())
      throw new JsltException("sum(): array must contain numbers, found " + value);
    integral &= value.isIntegralNumber();

    sum += value.doubleValue();
  }
  if (integral)
    return new LongNode((long) sum);
  else
    return new DoubleNode(sum);
}
 
Example 5
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 6
Source File: StaticTests.java    From jslt with Apache License 2.0 6 votes vote down vote up
@Test
public void testRandomFunction() {
  try {
    JsonNode context = mapper.readTree("{}");

    Expression expr = Parser.compileString("random()");

    for (int ix = 0; ix < 10; ix++) {
      JsonNode actual = expr.apply(context);
      assertTrue(actual.isNumber());
      double value = actual.doubleValue();
      assertTrue(value > 0.0 && value < 1.0);
    }
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 7
Source File: MinFunction.java    From SkaETL with Apache License 2.0 5 votes vote down vote up
@Override
public AggregateFunction addValue(JsonNode value) {
    double doubleValue = value.doubleValue();
    if (Double.isNaN(minValue)) {
        minValue = doubleValue;
    }
    if (doubleValue < minValue) {
        minValue = doubleValue;
    }
    return this;
}
 
Example 8
Source File: AbstractCommand.java    From mirror with Apache License 2.0 5 votes vote down vote up
protected Double doubleValue(JsonNode node, String key) {
    JsonNode keyNode = jsonNodeValue(node, key);
    if (keyNode != null) {
        return keyNode.doubleValue();
    }
    return null;
}
 
Example 9
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 10
Source File: MultipleOfValidator.java    From json-schema-validator with Apache License 2.0 5 votes vote down vote up
public MultipleOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {

        super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MULTIPLE_OF, validationContext);
        if (schemaNode.isNumber()) {
            divisor = schemaNode.doubleValue();
        }

        parseErrorCode(getValidatorType().getErrorCodeKey());
    }
 
Example 11
Source File: NodeUtils.java    From jslt with Apache License 2.0 5 votes vote down vote up
public static boolean isTrue(JsonNode value) {
  return value != BooleanNode.FALSE &&
    !(value.isObject() && value.size() == 0) &&
    !(value.isTextual() && value.asText().length() == 0) &&
    !(value.isArray() && value.size() == 0) &&
    !(value.isNumber() && value.doubleValue() == 0.0) &&
    !value.isNull();
}
 
Example 12
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 13
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 14
Source File: FloatObjectIOProvider.java    From constellation with Apache License 2.0 5 votes vote down vote up
@Override
public void readObject(final int attributeId, final int elementId, final JsonNode jnode, 
        final GraphWriteMethods graph, final Map<Integer, Integer> vertexMap, final Map<Integer, Integer> transactionMap, 
        final GraphByteReader byteReader, final ImmutableObjectCache cache) throws IOException {
    final Float attributeue = jnode.isNull() ? null : (float) jnode.doubleValue();
    graph.setObjectValue(attributeId, elementId, attributeue);
}
 
Example 15
Source File: DoubleObjectIOProvider.java    From constellation with Apache License 2.0 5 votes vote down vote up
@Override
public void readObject(final int attributeId, final int elementId, final JsonNode jnode, 
        final GraphWriteMethods graph, final Map<Integer, Integer> vertexMap, final Map<Integer, Integer> transactionMap, 
        final GraphByteReader byteReader, final ImmutableObjectCache cache) throws IOException {
    final Double attributeValue = jnode.isNull() ? null : jnode.doubleValue();
    graph.setObjectValue(attributeId, elementId, attributeValue);
}
 
Example 16
Source File: ThresholdMixinPerfTest.java    From json-schema-validator with Apache License 2.0 4 votes vote down vote up
@Override
public boolean crossesThreshold(JsonNode node) {
    double lm = maximumDouble.doubleValue();
    double val = node.doubleValue();
    return lm < val || (excludeEqual && lm == val);
}
 
Example 17
Source File: SumFunction.java    From SkaETL with Apache License 2.0 4 votes vote down vote up
@Override
public AggregateFunction addValue(JsonNode value) {
    double doubleValue = value.doubleValue();
    sum += doubleValue;
    return this;
}
 
Example 18
Source File: MaximumValidator.java    From json-schema-validator with Apache License 2.0 4 votes vote down vote up
public MaximumValidator(String schemaPath, final JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
    super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MAXIMUM, validationContext);

    if (!schemaNode.isNumber()) {
        throw new JsonSchemaException("maximum value is not a number");
    }

    JsonNode exclusiveMaximumNode = getParentSchema().getSchemaNode().get(PROPERTY_EXCLUSIVE_MAXIMUM);
    if (exclusiveMaximumNode != null && exclusiveMaximumNode.isBoolean()) {
        excludeEqual = exclusiveMaximumNode.booleanValue();
    }

    parseErrorCode(getValidatorType().getErrorCodeKey());

    final String maximumText = schemaNode.asText();
    if ((schemaNode.isLong() || schemaNode.isInt()) && (JsonType.INTEGER.toString().equals(getNodeFieldType()))) {
        // "integer", and within long range
        final long lm = schemaNode.asLong();
        typedMaximum = new ThresholdMixin() {
            @Override
            public boolean crossesThreshold(JsonNode node) {
                if (node.isBigInteger()) {
                    //node.isBigInteger is not trustable, the type BigInteger doesn't mean it is a big number.
                    int compare = node.bigIntegerValue().compareTo(new BigInteger(schemaNode.asText()));
                    return compare > 0 || (excludeEqual && compare == 0);

                } else if (node.isTextual()) {
                    BigDecimal max = new BigDecimal(maximumText);
                    BigDecimal value = new BigDecimal(node.asText());
                    int compare = value.compareTo(max);
                    return compare > 0 || (excludeEqual && compare == 0);
                }
                long val = node.asLong();
                return lm < val || (excludeEqual && lm == val);
            }

            @Override
            public String thresholdValue() {
                return String.valueOf(lm);
            }
        };
    } else {
        typedMaximum = new ThresholdMixin() {
            @Override
            public boolean crossesThreshold(JsonNode node) {
                if (schemaNode.isDouble() && schemaNode.doubleValue() == Double.POSITIVE_INFINITY) {
                    return false;
                }
                if (schemaNode.isDouble() && schemaNode.doubleValue() == Double.NEGATIVE_INFINITY) {
                    return true;
                }
                if (node.isDouble() && node.doubleValue() == Double.NEGATIVE_INFINITY) {
                    return false;
                }
                if (node.isDouble() && node.doubleValue() == Double.POSITIVE_INFINITY) {
                    return true;
                }
                final BigDecimal max = new BigDecimal(maximumText);
                BigDecimal value = new BigDecimal(node.asText());
                int compare = value.compareTo(max);
                return compare > 0 || (excludeEqual && compare == 0);
            }

            @Override
            public String thresholdValue() {
                return maximumText;
            }
        };
    }
}
 
Example 19
Source File: IndexField.java    From zentity with Apache License 2.0 4 votes vote down vote up
public void quality(JsonNode value) throws ValidationException {
    validateQuality(value);
    this.quality = value.doubleValue();
}
 
Example 20
Source File: Matcher.java    From zentity with Apache License 2.0 4 votes vote down vote up
public void quality(JsonNode value) throws ValidationException {
    validateQuality(value);
    this.quality = value.doubleValue();
}