/** * 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); }
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; } }
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; } }
@Override public Double asDouble() { return !data.isNumber() ? null : data.asDouble(); }
@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(); } }
@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); }
@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); }
@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); }
@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); }
@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); }
@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); }
@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); }
@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); } }
@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); }
@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); }
@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); }
@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); }
@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); } }
@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); }
/** * @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()); }