com.fasterxml.jackson.databind.JsonNode.asDouble()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(149)

本文整理了Java中com.fasterxml.jackson.databind.JsonNode.asDouble()方法的一些代码示例,展示了JsonNode.asDouble()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonNode.asDouble()方法的具体详情如下:
包路径:com.fasterxml.jackson.databind.JsonNode
类名称:JsonNode
方法名:asDouble

JsonNode.asDouble介绍

[英]Method that will try to convert value of this node to a Java double. Numbers are coerced using default Java rules; booleans convert to 0.0 (false) and 1.0 (true), and Strings are parsed using default Java language integer parsing rules.

If representation cannot be converted to an int (including structured types like Objects and Arrays), default value of 0.0 will be returned; no exceptions are thrown.
[中]方法,该方法将尝试将此节点的值转换为Java double。使用默认Java规则强制数字;布尔值转换为0.0(false)和1.0(true),字符串使用默认的Java语言整数解析规则进行解析。
如果表示无法转换为int(包括对象和数组等结构化类型),将返回默认值0.0;没有抛出异常。

代码示例

代码示例来源:origin: redisson/redisson

/**
 * Method that will try to convert value of this node to a Java <b>double</b>.
 * Numbers are coerced using default Java rules; booleans convert to 0.0 (false)
 * and 1.0 (true), and Strings are parsed using default Java language integer
 * parsing rules.
 *<p>
 * If representation cannot be converted to an int (including structured types
 * like Objects and Arrays),
 * default value of <b>0.0</b> will be returned; no exceptions are thrown.
 */
public double asDouble() {
  return asDouble(0.0);
}

代码示例来源:origin: knowm/XChange

private List<YoBitAsksBidsData> parse(JsonNode nodeArray) {
  List<YoBitAsksBidsData> res = new ArrayList<>();
  if (nodeArray != null) {
   for (JsonNode jsonNode : nodeArray) {
    res.add(
      new YoBitAsksBidsData(
        BigDecimal.valueOf(jsonNode.get(1).asDouble()),
        BigDecimal.valueOf(jsonNode.get(0).asDouble())));
   }
  }
  return res;
 }
}

代码示例来源:origin: Graylog2/graylog2-server

private static double timestampValue(final JsonNode json) {
  final JsonNode value = json.path(Message.FIELD_TIMESTAMP);
  if (value.isNumber()) {
    return value.asDouble(-1.0);
  } else if (value.isTextual()) {
    try {
      return Double.parseDouble(value.asText());
    } catch (NumberFormatException e) {
      log.debug("Unable to parse timestamp", e);
      return -1.0;
    }
  } else {
    return -1.0;
  }
}

代码示例来源:origin: auth0/java-jwt

@Override
public Double asDouble() {
  return !data.isNumber() ? null : data.asDouble();
}

代码示例来源:origin: Graylog2/graylog2-server

@Nullable
private Object valueNode(JsonNode jsonNode) {
  if (jsonNode.isInt()) {
    return jsonNode.asInt();
  } else if (jsonNode.isLong()) {
    return jsonNode.asLong();
  } else if (jsonNode.isIntegralNumber()) {
    return jsonNode.asLong();
  } else if (jsonNode.isFloatingPointNumber()) {
    return jsonNode.asDouble();
  } else if (jsonNode.isBoolean()) {
    return jsonNode.asBoolean();
  } else if (jsonNode.isNull()) {
    return null;
  } else {
    return jsonNode.asText();
  }
}

代码示例来源:origin: twosigma/beakerx

@Test
public void serializePointerAngleOfText_resultJsonHasPointerAngle() throws IOException {
 //when
 text.setPointerAngle(0.5);
 textSerializer.serialize(text, jgen, new DefaultSerializerProvider.Impl());
 jgen.flush();
 //then
 JsonNode actualObj = mapper.readTree(sw.toString());
 Assertions.assertThat(actualObj.has("pointer_angle")).isTrue();
 Assertions.assertThat(actualObj.get("pointer_angle").asDouble()).isEqualTo(0.5);
}

代码示例来源:origin: twosigma/beakerx

@Test
public void serializeLowerMarginOfYAxis_resultJsonHasLowerMargin() throws IOException {
 //when
 yAxis.setLowerMargin(1.5);
 yAxisSerializer.serialize(yAxis, jgen, new DefaultSerializerProvider.Impl());
 jgen.flush();
 //then
 JsonNode actualObj = mapper.readTree(sw.toString());
 Assertions.assertThat(actualObj.has("lower_margin")).isTrue();
 Assertions.assertThat(actualObj.get("lower_margin").asDouble()).isEqualTo(1.5);
}

代码示例来源:origin: twosigma/beakerx

@Test
public void serializeRatioOfTreeMap_resultJsonHasRatio() throws IOException {
 //when
 treeMap.setRatio(2.0);
 treeMapSerializer.serialize(treeMap, jgen, new DefaultSerializerProvider.Impl());
 jgen.flush();
 //then
 JsonNode actualObj = mapper.readTree(sw.toString());
 Assertions.assertThat(actualObj.has("ratio")).isTrue();
 Assertions.assertThat(actualObj.get("ratio").asDouble()).isEqualTo(2.0);
}

代码示例来源:origin: twosigma/beakerx

@Test
public void serializeXLogBaseOfXYChartPlot_resultJsonHasXLogBase() throws IOException {
 //when
 plot.setxLogBase(1.5);
 xyChartSerializer.serialize(plot, jgen, new DefaultSerializerProvider.Impl());
 jgen.flush();
 //then
 JsonNode actualObj = mapper.readTree(sw.toString());
 Assertions.assertThat(actualObj.has("x_log_base")).isTrue();
 Assertions.assertThat(actualObj.get("x_log_base").asDouble()).isEqualTo(1.5);
}

代码示例来源:origin: twosigma/beakerx

@Test
public void serializeUpperMarginOfYAxis_resultJsonHasUpperMargin() throws IOException {
 //when
 yAxis.setUpperMargin(2.5);
 yAxisSerializer.serialize(yAxis, jgen, new DefaultSerializerProvider.Impl());
 jgen.flush();
 //then
 JsonNode actualObj = mapper.readTree(sw.toString());
 Assertions.assertThat(actualObj.has("upper_margin")).isTrue();
 Assertions.assertThat(actualObj.get("upper_margin").asDouble()).isEqualTo(2.5);
}

代码示例来源:origin: twosigma/beakerx

@Test
public void serializeWidthLine_resultJsonHasWidth() throws IOException {
 //when
 line.setWidth(1f);
 lineSerializer.serialize(line, jgen, new DefaultSerializerProvider.Impl());
 jgen.flush();
 //then
 JsonNode actualObj = mapper.readTree(sw.toString());
 Assertions.assertThat(actualObj.has("width")).isTrue();
 Assertions.assertThat(actualObj.get("width").asDouble()).isEqualTo(1.0);
}

代码示例来源:origin: twosigma/beakerx

@Test
public void serializeXLowerBoundOfXYChartPlot_resultJsonHasXLowerBound() throws IOException {
 //when
 plot.setXBound(0.5, 1.5);
 xyChartSerializer.serialize(plot, jgen, new DefaultSerializerProvider.Impl());
 jgen.flush();
 //then
 JsonNode actualObj = mapper.readTree(sw.toString());
 Assertions.assertThat(actualObj.has("x_lower_bound")).isTrue();
 Assertions.assertThat(actualObj.get("x_lower_bound").asDouble()).isEqualTo(0.5);
}

代码示例来源:origin: twosigma/beakerx

@Test
 public void serializeLogBaseOfYAxis_resultJsonHasLogBase() throws IOException {
  //when
  yAxis.setLogBase(1.5);
  yAxisSerializer.serialize(yAxis, jgen, new DefaultSerializerProvider.Impl());
  jgen.flush();
  //then
  JsonNode actualObj = mapper.readTree(sw.toString());
  Assertions.assertThat(actualObj.has("log_base")).isTrue();
  Assertions.assertThat(actualObj.get("log_base").asDouble()).isEqualTo(1.5);
 }
}

代码示例来源:origin: twosigma/beakerx

@Test
public void serializeXUpperBoundOfXYChartPlot_resultJsonHasXUpperBound() throws IOException {
 //when
 plot.setXBound(0.5, 1.5);
 xyChartSerializer.serialize(plot, jgen, new DefaultSerializerProvider.Impl());
 jgen.flush();
 //then
 JsonNode actualObj = mapper.readTree(sw.toString());
 Assertions.assertThat(actualObj.has("x_upper_bound")).isTrue();
 Assertions.assertThat(actualObj.get("x_upper_bound").asDouble()).isEqualTo(1.5);
}

代码示例来源:origin: twosigma/beakerx

@Test
public void serializeWidthConstantLine_resultJsonHasWidth() throws IOException {
 //when
 ConstantLine constantLine = new ConstantLine();
 constantLine.setWidth(2f);
 constantLineSerializer.serialize(constantLine, jgen, new DefaultSerializerProvider.Impl());
 jgen.flush();
 //then
 JsonNode actualObj = mapper.readTree(sw.toString());
 Assertions.assertThat(actualObj.has("width")).isTrue();
 Assertions.assertThat(actualObj.get("width").asDouble()).isEqualTo(2.0);
}

代码示例来源:origin: twosigma/beakerx

@Test
public void serializeWeightOfTreeMapNode_resultJsonHasWeight() throws IOException {
 //when
 TreeMapNode treeMapNode = new TreeMapNode("label");
 treeMapNode.setWeight(0.5);
 treeMapNodeSerializer.serialize(treeMapNode, jgen, new DefaultSerializerProvider.Impl());
 jgen.flush();
 //then
 JsonNode actualObj = mapper.readTree(sw.toString());
 Assertions.assertThat(actualObj.has("weight")).isTrue();
 Assertions.assertThat(actualObj.get("weight").asDouble()).isEqualTo(0.5);
}

代码示例来源:origin: json-path/JsonPath

@Test
public void list_of_numbers() {
  ArrayNode objs = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price");
  assertThat(objs.get(0).asDouble()).isEqualTo(8.95D);
  assertThat(objs.get(1).asDouble()).isEqualTo(12.99D);
  assertThat(objs.get(2).asDouble()).isEqualTo(8.99D);
  assertThat(objs.get(3).asDouble()).isEqualTo(22.99D);
}

代码示例来源:origin: twosigma/beakerx

@Test
 public void serializeWidthCrosshair_resultJsonHasWidth() throws IOException {
  //when
  Crosshair crosshair = new Crosshair();
  crosshair.setWidth(2f);
  crosshairSerializer.serialize(crosshair, jgen, new DefaultSerializerProvider.Impl());
  jgen.flush();
  //then
  JsonNode actualObj = mapper.readTree(sw.toString());
  Assertions.assertThat(actualObj.has("width")).isTrue();
  Assertions.assertThat(actualObj.get("width").asDouble()).isEqualTo(2.0);
 }
}

代码示例来源:origin: twosigma/beakerx

@Test
public void serializeDoubleValueOfTreeMapNode_resultJsonHasDoubleValue() throws IOException {
 //when
 TreeMapNode treeMapNode = new TreeMapNode("010", 1, new DefaultValue(1.5));
 treeMapNode.setUserObject(values);
 treeMapNodeSerializer.serialize(treeMapNode, jgen, new DefaultSerializerProvider.Impl());
 jgen.flush();
 //then
 JsonNode actualObj = mapper.readTree(sw.toString());
 Assertions.assertThat(actualObj.has("doubleValue")).isTrue();
 Assertions.assertThat(actualObj.get("doubleValue").asDouble()).isEqualTo(1.5);
}

代码示例来源:origin: apache/ignite

/**
 * @param json JSON to check.
 * @param p Person to compare with.
 * @throws IOException If failed.
 */
private void checkJson(String json, Person p) throws IOException {
  JsonNode res = assertResponseSucceeded(json, false);
  assertEquals(p.id.intValue(), res.get("id").asInt());
  assertEquals(p.getOrganizationId().intValue(), res.get("orgId").asInt());
  assertEquals(p.getFirstName(), res.get("firstName").asText());
  assertEquals(p.getLastName(), res.get("lastName").asText());
  assertEquals(p.getSalary(), res.get("salary").asDouble());
}

相关文章