AbstractNode { public BigInteger getAsBigInt(@Nonnull String fieldName) { DockerCloudUtils.requireNonNull(fieldName, "Field name cannot be null."); checkObject(); BigInteger bigInt = null; JsonNode value = node.get(fieldName); if (value != null) { if (value.isBigInteger()) { bigInt = value.bigIntegerValue(); } else if (value.isIntegralNumber()) { String number = value.asText(); bigInt = new BigInteger(number); } else if (value.isNumber()) { try { bigInt = value.decimalValue().toBigIntegerExact(); } catch (ArithmeticException e) { } } } if (bigInt == null) { throw new NodeProcessingException("Child field not found or is not an integer value node: " + node + " / " + fieldName); } return bigInt; } AbstractNode(JsonNode node); @Nonnull List<N> getArrayValues(); @Nonnull Map<String, N> getObjectValues(); @Nonnull N getObject(@Nonnull String fieldName); N getObject(@Nonnull String fieldName, @Nullable N def); N getArray(@Nonnull String fieldName); N getArray(@Nonnull String fieldName, @Nullable N def); int getAsInt(@Nonnull String fieldName); int getAsInt(@Nonnull String fieldName, int def); BigInteger getAsBigInt(@Nonnull String fieldName); BigInteger getAsBigInt(@Nonnull String fieldName, BigInteger def); long getAsLong(@Nonnull String fieldName); boolean getAsBoolean(@Nonnull String fieldName); Boolean getAsBoolean(@Nonnull String fieldName, @Nullable Boolean def); @Nonnull String getAsString(@Nonnull String fieldName); String getAsString(@Nonnull String fieldName, @Nullable String def); @Nonnull String getAsString(); boolean isNull(); @Override String toString(); @Override boolean equals(Object obj); @Override int hashCode();  }
@Test public void getAsBigInt() { AbstractNode<N> node = parentWithChildInt(42); assertThat(node.getAsBigInt("child")).isEqualTo(BigInteger.valueOf(42)); assertThat(node.getAsBigInt("unknownChild", BigInteger.ZERO)).isEqualTo(BigInteger.ZERO); }
@Test public void getAsBigIntFromDecimal() { AbstractNode<N> node = parentWithChildDouble(1.0); assertThat(node.getAsBigInt("child")).isEqualTo(BigInteger.ONE); node = parentWithChildDouble(1.0); assertThat(node.getAsBigInt("child")).isEqualTo(BigInteger.ONE); node = parentWithChildBigDecimal(new BigDecimal("1E+1")); assertThat(node.getAsBigInt("child")).isEqualTo(BigInteger.TEN); AbstractNode<N> nodeWithFraction = parentWithChildBigDecimal(BigDecimal.valueOf(1.1)); assertThatExceptionOfType(NodeProcessingException.class). isThrownBy(() -> nodeWithFraction.getAsBigInt("child")); }