/** * Method that will try to convert value of this node to a Java <b>boolean</b>. * JSON booleans map naturally; integer numbers other than 0 map to true, and * 0 maps to false * and Strings 'true' and 'false' map to corresponding values. *<p> * If representation cannot be converted to a boolean value (including structured types * like Objects and Arrays), * default value of <b>false</b> will be returned; no exceptions are thrown. */ public boolean asBoolean() { return asBoolean(false); }
protected boolean getValueAsBoolean(String name, JsonNode objectNode) { boolean propertyValue = false; JsonNode propertyNode = objectNode.get(name); if (propertyNode != null && !propertyNode.isNull()) { propertyValue = propertyNode.asBoolean(); } return propertyValue; }
public static Boolean parseBoolean(String body, String field) { ObjectMapper mapper = new ObjectMapper(); JsonNode node = null; try { node = mapper.readTree(body); JsonNode leaf = node.get(field); if (leaf != null) return leaf.asBoolean(); } catch (IOException e) { e.printStackTrace(); } return null; }
private boolean isRequired(String nodeName, JsonNode node, Schema schema) { if (node.has("required")) { final JsonNode requiredNode = node.get("required"); return requiredNode.asBoolean(); } JsonNode requiredArray = schema.getContent().get("required"); if (requiredArray != null) { for (JsonNode requiredNode : requiredArray) { if (nodeName.equals(requiredNode.asText())) return true; } } return false; }
private boolean useOptional(String nodeName, JsonNode node, Schema schema) { if (node.has("javaOptional")) { final JsonNode requiredNode = node.get("javaOptional"); return requiredNode.asBoolean(); } JsonNode javaOptionalArray = schema.getContent().get("javaOptional"); if (javaOptionalArray != null) { for (JsonNode requiredNode : javaOptionalArray) { if (nodeName.equals(requiredNode.asText())) return true; } } return false; }
private boolean checkForReopened(@Nonnull JsonNode indexSettings, Version elasticsearchVersion) { final JsonNode settings; if (elasticsearchVersion.satisfies(">=2.1.0 & <5.0.0")) { settings = indexSettings; } else if (elasticsearchVersion.satisfies("^5.0.0 | ^6.0.0")) { settings = indexSettings.path("archived"); } else { throw new ElasticsearchException("Unsupported Elasticsearch version: " + elasticsearchVersion); } // if this is a missing node, asBoolean() returns false return settings.path("index").path(REOPENED_INDEX_SETTING).asBoolean(); }
@Override public boolean getBoolean() { if (value.isValueNode()) { return value.asBoolean(); } throw new PrestoException( DECODER_CONVERSION_NOT_SUPPORTED, format("could not parse non-value node as '%s' for column '%s'", columnHandle.getType(), columnHandle.getName())); }
private Object toValue(JsonNode node) throws IOException { if (node.isTextual()) { return node.asText(); } else if (node.isNumber()) { return node.numberValue(); } else if (node.isBoolean()) { return node.asBoolean(); } else if (node.isBinary()) { return node.binaryValue(); } else { throw new IllegalStateException("Unexpected range bound value: " + node); } } }
@Test public void serializeBorderDisplayed_resultJsonHasBorderDisplayed() throws IOException { //given layoutManager.setBorderDisplayed(true); //when JsonNode actualObj = helper.serializeObject(layoutManager); //then Assertions.assertThat(actualObj.has("borderDisplayed")).isTrue(); Assertions.assertThat(actualObj.get("borderDisplayed").asBoolean()).isTrue(); }
@Test public void serializeCumulativeOfHistogram_resultJsonHasCumulative() throws IOException { //when histogram.setCumulative(true); histogramSerializer.serialize(histogram, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("cumulative")).isTrue(); Assertions.assertThat(actualObj.get("cumulative").asBoolean()).isTrue(); }
@Test public void serializeLogXOfXYChartPlot_resultJsonHasLogX() throws IOException { //when plot.setLogX(true); xyChartSerializer.serialize(plot, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("log_x")).isTrue(); Assertions.assertThat(actualObj.get("log_x").asBoolean()).isTrue(); }
@Test public void serializeAutoRangeOfYAxis_resultJsonHasAutoRange() throws IOException { //when yAxis.setAutoRange(true); yAxisSerializer.serialize(yAxis, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("auto_range")).isTrue(); Assertions.assertThat(actualObj.get("auto_range").asBoolean()).isTrue(); }
@Test public void serializeClickActionLineGraphics_resultJsonHasClickAction() throws IOException { //when line.onClick(actionObject -> {}); graphicsSerializer.serialize(line, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("hasClickAction")).isTrue(); Assertions.assertThat(actualObj.get("hasClickAction").asBoolean()).isTrue(); }
@Test public void serializeShowPointerText_resultJsonHasShowPointer() throws IOException { //when text.setShowPointer(true); textSerializer.serialize(text, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("show_pointer")).isTrue(); Assertions.assertThat(actualObj.get("show_pointer").asBoolean()).isTrue(); }
@Test public void serializeRoundOfTreeMap_resultJsonHasRound() throws IOException { //when treeMap.setRound(true); treeMapSerializer.serialize(treeMap, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("round")).isTrue(); Assertions.assertThat(actualObj.get("round").asBoolean()).isTrue(); }
@Test public void serializeXTickLabelsVisibleOfXYChartPlot_resultJsonHasXTickLabelsVisible() throws IOException { //when plot.setxTickLabelsVisible(true); xyChartSerializer.serialize(plot, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("x_tickLabels_visible")).isTrue(); Assertions.assertThat(actualObj.get("x_tickLabels_visible").asBoolean()).isTrue(); }
@Test public void serializeFillsPoints_resultJsonHasFills() throws IOException { //when points.setFill(Arrays.asList(false, true, false)); pointsSerializer.serialize(points, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("fills")).isTrue(); ArrayNode arrayNode = (ArrayNode) actualObj.get("fills"); Assertions.assertThat(arrayNode.get(1).asBoolean()).isTrue(); }
@Test public void serializeVisibleConstantLine_resultJsonHasVisible() throws IOException { //when ConstantLine constantLine = new ConstantLine(); constantLine.setVisible(true); constantLineSerializer.serialize(constantLine, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("visible")).isTrue(); Assertions.assertThat(actualObj.get("visible").asBoolean()).isTrue(); }
@Test public void serializeShowLabelConstantLine_resultJsonHasShowLabel() throws IOException { //when ConstantLine constantLine = new ConstantLine(); constantLine.setShowLabel(true); constantLineSerializer.serialize(constantLine, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("showLabel")).isTrue(); Assertions.assertThat(actualObj.get("showLabel").asBoolean()).isTrue(); }
@Test public void serializeFillCategoryPoints_resultJsonHasFill() throws IOException { //when CategoryPoints categoryPoints = new CategoryPoints(); categoryPoints.setFill(true); categoryPointsSerializer.serialize(categoryPoints, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("fill")).isTrue(); Assertions.assertThat(actualObj.get("fill").asBoolean()).isTrue(); }