com.fasterxml.jackson.databind.node.DoubleNode Java Examples

The following examples show how to use com.fasterxml.jackson.databind.node.DoubleNode. 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: ObjectToJsonNode.java    From yosegi with Apache License 2.0 6 votes vote down vote up
/**
 * Judge Java objects and create JsonNode.
 */
public static JsonNode get( final Object obj ) throws IOException {
  if ( obj instanceof PrimitiveObject ) {
    return PrimitiveObjectToJsonNode.get( (PrimitiveObject)obj );
  } else if ( obj instanceof String ) {
    return new TextNode( (String)obj );
  } else if ( obj instanceof Boolean ) {
    return BooleanNode.valueOf( (Boolean)obj );
  } else if ( obj instanceof Short ) {
    return IntNode.valueOf( ( (Short)obj ).intValue() );
  } else if ( obj instanceof Integer ) {
    return IntNode.valueOf( (Integer)obj );
  } else if ( obj instanceof Long ) {
    return new LongNode( (Long)obj );
  } else if ( obj instanceof Float ) {
    return new DoubleNode( ( (Float)obj ).doubleValue() );
  } else if ( obj instanceof Double ) {
    return new DoubleNode( (Double)obj );
  } else if ( obj instanceof byte[] ) {
    return new BinaryNode( (byte[])obj );
  } else if ( obj == null ) {
    return NullNode.getInstance();
  } else {
    return new TextNode( obj.toString() );
  }
}
 
Example #2
Source File: DmnTaskTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
@Deployment(resources = {
        "org/flowable/bpmn/test/runtime/DmnTaskTest.oneDecisionTaskProcess.bpmn20.xml",
        "org/flowable/bpmn/test/runtime/DmnTaskTest.outputOrder.dmn"})
void withOutputOrder_ensureListOfItemIsReturnedEvenIfOnlyOneRowIsHit() {
    ProcessInstance processInstance = this.runtimeService.createProcessInstanceBuilder()
            .processDefinitionKey("oneDecisionTaskProcess")
            .variable("testInput", "second")
            .start();
    Map<String, Object> processVariables = processInstance.getProcessVariables();
    Object resultObject = processVariables.get("DecisionTable");
    assertThat(resultObject).isInstanceOf(ArrayNode.class);
    ArrayNode result = (ArrayNode) resultObject;
    assertThat(result).hasSize(1);
    assertThat(result.get(0)).isInstanceOf(ObjectNode.class);
    assertThat(result.get(0).get("testOutput")).isInstanceOf(DoubleNode.class);
    assertThat(result.get(0).get("testOutput").asDouble()).isEqualTo(2.0);
}
 
Example #3
Source File: DecisionTaskTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
@CmmnDeployment(resources = {
        "org/flowable/cmmn/test/runtime/DecisionTaskTest.oneDecisionTaskCase.cmmn",
        "org/flowable/cmmn/test/runtime/DecisionTaskTest.ruleOrder.dmn"})
public void withRuleOrder_ensureListOfItemIsReturnedEvenIfOnlyOneRowIsHit() {
    CaseInstance caseInstance = this.cmmnRule.getCmmnRuntimeService().createCaseInstanceBuilder()
            .caseDefinitionKey("oneDecisionTaskCase")
            .variable("testInput", "second")
            .start();
    Map<String, Object> caseVariables = caseInstance.getCaseVariables();
    Object resultObject = caseVariables.get("DecisionTable");
    assertThat(resultObject).isInstanceOf(ArrayNode.class);
    ArrayNode result = (ArrayNode) resultObject;
    assertThat(result).hasSize(1);
    assertThat(result.get(0)).isInstanceOf(ObjectNode.class);
    assertThat(result.get(0).get("testOutput")).isInstanceOf(DoubleNode.class);
    assertThat(result.get(0).get("testOutput").asDouble()).isEqualTo(2.0);
}
 
Example #4
Source File: DecisionTaskTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
@CmmnDeployment(resources = {
        "org/flowable/cmmn/test/runtime/DecisionTaskTest.oneDecisionTaskCase.cmmn",
        "org/flowable/cmmn/test/runtime/DecisionTaskTest.outputOrder.dmn"})
public void withOutputOrder_ensureListOfItemIsReturnedEvenIfOnlyOneRowIsHit() {
    CaseInstance caseInstance = this.cmmnRule.getCmmnRuntimeService().createCaseInstanceBuilder()
            .caseDefinitionKey("oneDecisionTaskCase")
            .variable("testInput", "second")
            .start();
    Map<String, Object> caseVariables = caseInstance.getCaseVariables();
    Object resultObject = caseVariables.get("DecisionTable");
    assertThat(resultObject).isInstanceOf(ArrayNode.class);
    ArrayNode result = (ArrayNode) resultObject;
    assertThat(result).hasSize(1);
    assertThat(result.get(0)).isInstanceOf(ObjectNode.class);
    assertThat(result.get(0).get("testOutput")).isInstanceOf(DoubleNode.class);
    assertThat(result.get(0).get("testOutput").asDouble()).isEqualTo(2.0);
}
 
Example #5
Source File: DmnTaskTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
@Deployment(resources = {
        "org/flowable/bpmn/test/runtime/DmnTaskTest.oneDecisionTaskProcess.bpmn20.xml",
        "org/flowable/bpmn/test/runtime/DmnTaskTest.ruleOrder.dmn"})
void withRuleOrder_ensureListOfItemIsReturnedEvenIfOnlyOneRowIsHit() {
    ProcessInstance processInstance = this.runtimeService.createProcessInstanceBuilder()
            .processDefinitionKey("oneDecisionTaskProcess")
            .variable("testInput", "second")
            .start();
    Map<String, Object> processVariables = processInstance.getProcessVariables();
    Object resultObject = processVariables.get("DecisionTable");
    assertThat(resultObject).isInstanceOf(ArrayNode.class);
    ArrayNode result = (ArrayNode) resultObject;
    assertThat(result).hasSize(1);
    assertThat(result.get(0)).isInstanceOf(ObjectNode.class);
    assertThat(result.get(0).get("testOutput")).isInstanceOf(DoubleNode.class);
    assertThat(result.get(0).get("testOutput").asDouble()).isEqualTo(2.0);
}
 
Example #6
Source File: TsdbResult.java    From splicer with Apache License 2.0 6 votes vote down vote up
@Override
public Points deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
	TreeNode n = jp.getCodec().readTree(jp);
	Map<String, Object> points = new HashMap<>();
	Iterator<String> namesIter = n.fieldNames();
	while(namesIter.hasNext()) {
		String field = namesIter.next();
		TreeNode child = n.get(field);

		Object o;
		if (child instanceof DoubleNode || child instanceof FloatNode) {
			o = ((NumericNode) child).doubleValue();
		} else if (child instanceof IntNode || child instanceof LongNode) {
			o = ((NumericNode) child).longValue();
		} else {
			throw new MergeException("Unsupported Type, " + child.getClass());
		}

		points.put(field, o);
	}
	return new Points(points);
}
 
Example #7
Source File: DistributedNetworkConfigStore.java    From onos with Apache License 2.0 6 votes vote down vote up
@Activate
public void activate() {
    KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
            .register(KryoNamespaces.API)
            .register(ConfigKey.class, ObjectNode.class, ArrayNode.class,
                      JsonNodeFactory.class, LinkedHashMap.class,
                      TextNode.class, BooleanNode.class,
                      LongNode.class, DoubleNode.class, ShortNode.class, IntNode.class,
                      NullNode.class);

    configs = storageService.<ConfigKey, JsonNode>consistentMapBuilder()
            .withSerializer(Serializer.using(kryoBuilder.build()))
            .withName("onos-network-configs")
            .withRelaxedReadConsistency()
            .build();
    configs.addListener(listener);
    log.info("Started");
}
 
Example #8
Source File: RandomWalkSampler.java    From log-synth with Apache License 2.0 6 votes vote down vote up
@Override
public JsonNode sample() {
    double step;
    if (stepDistribution == null) {
        step = rand.nextGaussian() * sd.sample().asDouble() + mean.sample().asDouble();
    } else {
        step = stepDistribution.sample().asDouble();
    }
    double newState = state.addAndGet(step);

    if (verbose) {
        ObjectNode r = new ObjectNode(JsonNodeFactory.withExactBigDecimals(false));
        r.set("value", new DoubleNode(newState));
        r.set("step", new DoubleNode(step));
        return r;
    } else {
        return new DoubleNode(newState);
    }
}
 
Example #9
Source File: DivideOperator.java    From jslt with Apache License 2.0 6 votes vote down vote up
public JsonNode perform(JsonNode v1, JsonNode v2) {
  if (v1.isNull() || v2.isNull())
    return NullNode.instance;

  // we only support the numeric operation and nothing else
  v1 = NodeUtils.number(v1, true, location);
  v2 = NodeUtils.number(v2, true, location);

  if (v1.isIntegralNumber() && v2.isIntegralNumber()) {
    long l1 = v1.longValue();
    long l2 = v2.longValue();
    if (l1 % l2 == 0)
      return new LongNode(l1 / l2);
    else
      return new DoubleNode((double) l1 / (double) l2);
  } else
    return new DoubleNode(perform(v1.doubleValue(), v2.doubleValue()));
}
 
Example #10
Source File: RandomWalkSampler.java    From log-synth with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("UnusedDeclaration")
public void setPrecision(final JsonNode value) throws IOException {
    if (value.isObject()) {
        sd = new FieldSampler() {
            FieldSampler base = FieldSampler.newSampler(value.toString());

            @Override
            public JsonNode sample() {
                return new DoubleNode(Math.sqrt(1 / base.sample().asDouble()));
            }
        };
    } else {
        this.sd = constant(Math.sqrt(1 / value.asDouble()));
    }
    init();
}
 
Example #11
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 #12
Source File: RandomWalkSampler.java    From log-synth with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("UnusedDeclaration")
public void setVariance(final JsonNode value) throws IOException {
    if (value.isObject()) {
        sd = new FieldSampler() {
            FieldSampler base = FieldSampler.newSampler(value.toString());

            @Override
            public JsonNode sample() {
                return new DoubleNode(Math.sqrt(base.sample().asDouble()));
            }
        };
    } else {
        this.sd = constant(Math.sqrt(value.asDouble()));
    }
    init();
}
 
Example #13
Source File: RandomWalkSampler.java    From log-synth with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("UnusedDeclaration")
public void setSD(final JsonNode value) throws IOException {
    if (value.isObject()) {
        sd = new FieldSampler() {
            FieldSampler base = FieldSampler.newSampler(value.toString());

            @Override
            public JsonNode sample() {
                return new DoubleNode(base.sample().asDouble());
            }
        };
    } else {
        sd = constant(value.asDouble());
    }
    init();
}
 
Example #14
Source File: UiExtensionManager.java    From onos with Apache License 2.0 5 votes vote down vote up
@Activate
public void activate() {
    Serializer serializer = Serializer.using(KryoNamespaces.API,
                 ObjectNode.class, ArrayNode.class,
                 JsonNodeFactory.class, LinkedHashMap.class,
                 TextNode.class, BooleanNode.class,
                 LongNode.class, DoubleNode.class, ShortNode.class,
                 IntNode.class, NullNode.class, UiSessionToken.class);

    prefsConsistentMap = storageService.<String, ObjectNode>consistentMapBuilder()
            .withName(ONOS_USER_PREFERENCES)
            .withSerializer(serializer)
            .withRelaxedReadConsistency()
            .build();
    prefsConsistentMap.addListener(prefsListener);
    prefs = prefsConsistentMap.asJavaMap();

    tokensConsistentMap = storageService.<UiSessionToken, String>consistentMapBuilder()
            .withName(ONOS_SESSION_TOKENS)
            .withSerializer(serializer)
            .withRelaxedReadConsistency()
            .build();
    tokens = tokensConsistentMap.asJavaMap();

    register(core);

    log.info("Started");
}
 
Example #15
Source File: ApplicationSettings.java    From gitlab4j-api with MIT License 5 votes vote down vote up
private Object jsonNodeToValue(JsonNode node) {

        Object value = node;
        if (node instanceof NullNode) {
            value = null;
        } else if (node instanceof TextNode) {
            value = node.asText();
        } else if (node instanceof BooleanNode) {
            value = node.asBoolean();
        } else if (node instanceof IntNode) {
            value = node.asInt();
        } else if (node instanceof FloatNode) {
            value = (float)((FloatNode)node).asDouble();
        } else if (node instanceof DoubleNode) {
            value = (float)((DoubleNode)node).asDouble();
        } else if (node instanceof ArrayNode) {

            int numItems = node.size();
            String[] values = new String[numItems];
            for (int i = 0; i < numItems; i++) {
                values[i] = node.path(i).asText();
            }

            value = values;
        }

        return (value);
    }
 
Example #16
Source File: Datapoint.java    From bce-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Add datapoint of double type value.
 *
 * @param time datapoint's timestamp
 * @param value datapoint's value
 * @return Datapoint
 */
public Datapoint addDoubleValue(long time, double value) {
    initialValues();
    checkType(TsdbConstants.TYPE_DOUBLE);

    values.add(Lists.<JsonNode> newArrayList(new LongNode(time), new DoubleNode(value)));
    return this;
}
 
Example #17
Source File: Serializer.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Double> deserialize(JsonNode json) {
    if (json instanceof DoubleNode || json instanceof FloatNode) {
        return Optional.of(json.asDouble());
    } else {
        return Optional.empty();
    }
}
 
Example #18
Source File: Serializer.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Float> deserialize(JsonNode json) {
    if (json instanceof FloatNode) {
        return Optional.of(json.floatValue());
    } else if (json instanceof DoubleNode) {
        return Optional.of(json.floatValue());
    } else {
        return Optional.empty();
    }
}
 
Example #19
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 #20
Source File: NumberJsonPropertyMapper.java    From rest-schemagen with Apache License 2.0 5 votes vote down vote up
private JsonNode createNode(Object value) {
    if (value instanceof Float) {
        return new FloatNode((Float) value);
    } else if (value instanceof Double) {
        return new DoubleNode((Double) value);
    }
    return null;
}
 
Example #21
Source File: FieldSampler.java    From log-synth with Apache License 2.0 5 votes vote down vote up
protected static FieldSampler constant(final double v) {
    return new FieldSampler() {
        private DoubleNode sd = new DoubleNode(v);

        @Override
        public JsonNode sample() {
            return sd;
        }
    };
}
 
Example #22
Source File: NormalSampler.java    From log-synth with Apache License 2.0 5 votes vote down vote up
@Override
public JsonNode sample() {
    double x;
    do {
        x = rand.nextDouble();
    } while (x < min || x > max);
    return new DoubleNode(x);
}
 
Example #23
Source File: JsonNodeToPrimitiveObject.java    From yosegi with Apache License 2.0 5 votes vote down vote up
/**
 * Converts JsonNode to PrimitiveObject.
 */
public static PrimitiveObject get( final JsonNode jsonNode ) throws IOException {
  if ( jsonNode instanceof TextNode ) {
    return new StringObj( ( (TextNode)jsonNode ).textValue() );
  } else if ( jsonNode instanceof BooleanNode ) {
    return new BooleanObj( ( (BooleanNode)jsonNode ).booleanValue() );
  } else if ( jsonNode instanceof IntNode ) {
    return new IntegerObj( ( (IntNode)jsonNode ).intValue() );
  } else if ( jsonNode instanceof LongNode ) {
    return new LongObj( ( (LongNode)jsonNode ).longValue() );
  } else if ( jsonNode instanceof DoubleNode ) {
    return new DoubleObj( ( (DoubleNode)jsonNode ).doubleValue() );
  } else if ( jsonNode instanceof BigIntegerNode ) {
    return new StringObj( ( (BigIntegerNode)jsonNode ).bigIntegerValue().toString() );
  } else if ( jsonNode instanceof DecimalNode ) {
    return new StringObj( ( (DecimalNode)jsonNode ).decimalValue().toString() );
  } else if ( jsonNode instanceof BinaryNode ) {
    return new BytesObj( ( (BinaryNode)jsonNode ).binaryValue() );
  } else if ( jsonNode instanceof POJONode ) {
    return new BytesObj( ( (POJONode)jsonNode ).binaryValue() );
  } else if ( jsonNode instanceof NullNode ) {
    return NullObj.getInstance();
  } else if ( jsonNode instanceof MissingNode ) {
    return NullObj.getInstance();
  } else {
    return new StringObj( jsonNode.toString() );
  }
}
 
Example #24
Source File: Commuter.java    From log-synth with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
public void setWork(JsonNode value) throws IOException {
    if (value.isObject()) {
        workSampler = new FieldSampler() {
            FieldSampler base = FieldSampler.newSampler(value.toString());

            @Override
            public JsonNode sample() {
                return new DoubleNode(Math.sqrt(1 / base.sample().asDouble()));
            }
        };
    } else if (value.isNumber()) {
        workSampler = constant(value.asDouble());
    }
}
 
Example #25
Source File: PrimitiveObjectToJsonNode.java    From yosegi with Apache License 2.0 5 votes vote down vote up
/**
 * Convert PrimitiveObject to JsonNode.
 */
public static JsonNode get( final PrimitiveObject obj ) throws IOException {
  if ( obj == null ) {
    return NullNode.getInstance();
  }
  switch ( obj.getPrimitiveType() ) {
    case BOOLEAN:
      return BooleanNode.valueOf( obj.getBoolean() );
    case BYTE:
      return IntNode.valueOf( obj.getInt() );
    case SHORT:
      return IntNode.valueOf( obj.getInt() );
    case INTEGER:
      return IntNode.valueOf( obj.getInt() );
    case LONG:
      return new LongNode( obj.getLong() );
    case FLOAT:
      return new DoubleNode( obj.getDouble() );
    case DOUBLE:
      return new DoubleNode( obj.getDouble() );
    case STRING:
      return new TextNode( obj.getString() );
    case BYTES:
      return new BinaryNode( obj.getBytes() );
    default:
      return NullNode.getInstance();
  }
}
 
Example #26
Source File: JsonTypeMappingTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
public JacksonContainer(ObjectNode obj, JsonNode any, BinaryNode binary, TextNode text, IntNode integer,
                        DoubleNode dbl, BigIntegerNode bigInt, ArrayNode array) {
    this.obj = obj;
    this.any = any;
    this.text = text;
    this.binary = binary;
    this.integer = integer;
    this.dbl = dbl;
    this.bigInt = bigInt;
    this.array = array;
}
 
Example #27
Source File: JacksonScalars.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Override
public Number serialize(Object dataFetcherResult) {
    if (dataFetcherResult instanceof DoubleNode) {
        return ((DoubleNode) dataFetcherResult).numberValue();
    } else {
        throw serializationException(dataFetcherResult, DoubleNode.class);
    }
}
 
Example #28
Source File: JacksonScalars.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private static Map<Type, GraphQLScalarType> getScalarMapping() {
    Map<Type, GraphQLScalarType> scalarMapping = new HashMap<>();
    scalarMapping.put(TextNode.class, JsonTextNode);
    scalarMapping.put(BooleanNode.class, JsonBooleanNode);
    scalarMapping.put(BinaryNode.class, JsonBinaryNode);
    scalarMapping.put(BigIntegerNode.class, JsonBigIntegerNode);
    scalarMapping.put(IntNode.class, JsonIntegerNode);
    scalarMapping.put(ShortNode.class, JsonShortNode);
    scalarMapping.put(DecimalNode.class, JsonDecimalNode);
    scalarMapping.put(FloatNode.class, JsonFloatNode);
    scalarMapping.put(DoubleNode.class, JsonDoubleNode);
    scalarMapping.put(NumericNode.class, JsonDecimalNode);
    return Collections.unmodifiableMap(scalarMapping);
}
 
Example #29
Source File: JacksonScalars.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Override
public DoubleNode parseLiteral(Object input) {
    if (input instanceof IntValue) {
        return DoubleNode.valueOf(((IntValue) input).getValue().doubleValue());
    } if (input instanceof FloatValue) {
        return DoubleNode.valueOf(((FloatValue) input).getValue().doubleValue());
    } else {
        throw new CoercingParseLiteralException(errorMessage(input, IntValue.class, FloatValue.class));
    }
}
 
Example #30
Source File: JacksonScalars.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Override
public DoubleNode parseValue(Object input) {
    if (input instanceof Number || input instanceof String) {
        return DoubleNode.valueOf(new BigDecimal(input.toString()).doubleValue());
    }
    if (input instanceof DoubleNode) {
        return (DoubleNode) input;
    }
    throw valueParsingException(input, Number.class, String.class, DoubleNode.class);
}