return (T) id.getAsBigDecimal();
@Test public void bigdecimals_are_unwrapped() { final BigDecimal bd = BigDecimal.valueOf(Long.MAX_VALUE).add(BigDecimal.valueOf(10.5)); final String json = "{bd-property = " + bd.toString() + "}"; JsonElement node = using(GSON_CONFIGURATION).parse(json).read("$.bd-property"); BigDecimal val = using(GSON_CONFIGURATION).parse(json).read("$.bd-property", BigDecimal.class); assertThat(val).isEqualTo(bd); assertThat(val).isEqualTo(node.getAsBigDecimal()); }
@Test public void small_bigdecimals_are_unwrapped() { final BigDecimal bd = BigDecimal.valueOf(10.5); final String json = "{bd-property = " + bd.toString() + "}"; JsonElement node = using(GSON_CONFIGURATION).parse(json).read("$.bd-property"); BigDecimal val = using(GSON_CONFIGURATION).parse(json).read("$.bd-property", BigDecimal.class); assertThat(val).isEqualTo(bd); assertThat(val).isEqualTo(node.getAsBigDecimal()); }
/** * convenience method to get this array as a {@link BigDecimal} if it contains a single element. * * @return get this element as a {@link BigDecimal} if it is single element array. * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}. * @throws NumberFormatException if the element at index 0 is not a valid {@link BigDecimal}. * @throws IllegalStateException if the array has more than one element. * @since 1.2 */ @Override public BigDecimal getAsBigDecimal() { if (elements.size() == 1) { return elements.get(0).getAsBigDecimal(); } throw new IllegalStateException(); }
public BigDecimal decimalValue() { return jsonNode.getAsBigDecimal(); }
public BigDecimal decimalValue() { return jsonNode.getAsBigDecimal(); }
/** * convenience method to get this array as a {@link BigDecimal} if it contains a single element. * * @return get this element as a {@link BigDecimal} if it is single element array. * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}. * @throws NumberFormatException if the element at index 0 is not a valid {@link BigDecimal}. * @throws IllegalStateException if the array has more than one element. * @since 1.2 */ @Override public BigDecimal getAsBigDecimal() { if (elements.size() == 1) { return elements.get(0).getAsBigDecimal(); } throw new IllegalStateException(); }
/** * convenience method to get this array as a {@link BigDecimal} if it contains a single element. * * @return get this element as a {@link BigDecimal} if it is single element array. * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}. * @throws NumberFormatException if the element at index 0 is not a valid {@link BigDecimal}. * @throws IllegalStateException if the array has more than one element. * @since 1.2 */ @Override public BigDecimal getAsBigDecimal() { if (elements.size() == 1) { return elements.get(0).getAsBigDecimal(); } throw new IllegalStateException(); }
/** * convenience method to get this array as a {@link BigDecimal} if it contains a single element. * * @return get this element as a {@link BigDecimal} if it is single element array. * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}. * @throws NumberFormatException if the element at index 0 is not a valid {@link BigDecimal}. * @throws IllegalStateException if the array has more than one element. * @since 1.2 */ @Override public BigDecimal getAsBigDecimal() { if (elements.size() == 1) { return elements.get(0).getAsBigDecimal(); } throw new IllegalStateException(); }
/** * convenience method to get this array as a {@link BigDecimal} if it contains a single element. * * @return get this element as a {@link BigDecimal} if it is single element array. * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}. * @throws NumberFormatException if the element at index 0 is not a valid {@link BigDecimal}. * @throws IllegalStateException if the array has more than one element. * @since 1.2 */ @Override public BigDecimal getAsBigDecimal() { if (elements.size() == 1) { return elements.get(0).getAsBigDecimal(); } throw new IllegalStateException(); }
/** * convenience method to get this array as a {@link BigDecimal} if it contains a single element. * * @return get this element as a {@link BigDecimal} if it is single element array. * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}. * @throws NumberFormatException if the element at index 0 is not a valid {@link BigDecimal}. * @throws IllegalStateException if the array has more than one element. * @since 1.2 */ @Override public BigDecimal getAsBigDecimal() { if (elements.size() == 1) { return elements.get(0).getAsBigDecimal(); } throw new IllegalStateException(); }
/** * convenience method to get this array as a {@link BigDecimal} if it contains a single element. * * @return get this element as a {@link BigDecimal} if it is single element array. * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}. * @throws NumberFormatException if the element at index 0 is not a valid {@link BigDecimal}. * @throws IllegalStateException if the array has more than one element. * @since 1.2 */ @Override public BigDecimal getAsBigDecimal() { if (elements.size() == 1) { return elements.get(0).getAsBigDecimal(); } throw new IllegalStateException(); }
/** * convenience method to get this array as a {@link BigDecimal} if it contains a single element. * * @return get this element as a {@link BigDecimal} if it is single element array. * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}. * @throws NumberFormatException if the element at index 0 is not a valid {@link BigDecimal}. * @throws IllegalStateException if the array has more than one element. * @since 1.2 */ @Override public BigDecimal getAsBigDecimal() { if (elements.size() == 1) { return elements.get(0).getAsBigDecimal(); } throw new IllegalStateException(); }
@Override public CommitId fromJson(JsonElement json, JsonDeserializationContext jsonDeserializationContext) { BigDecimal majorDotMinor = json.getAsBigDecimal(); return CommitId.valueOf(majorDotMinor); }
@Override public boolean isInteger(JsonElement document) { if (!document.isJsonPrimitive()) return false; final JsonPrimitive primitive = (JsonPrimitive)document; if (!primitive.isNumber()) return false; final BigDecimal dec = document.getAsBigDecimal(); if (dec.scale() > 0) return false; final BigInteger i = dec.toBigInteger(); return i.bitLength() <= 31; }
private BigDecimal getBalance(String key) { JsonObject storeValue = store.get(key); if (storeValue != null && storeValue.has("balance")) { return storeValue.get("balance").getAsBigDecimal(); } else { return BigDecimal.ZERO; } }
@Override public XMLGregorianCalendar deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { try { JsonObject obj = jsonElement.getAsJsonObject(); XMLGregorianCalendar xmlGregCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar( obj.get(YEAR).getAsInt(), obj.get(MONTH).getAsInt(), obj.get(DAY).getAsInt(), obj.get(HOUR).getAsInt(), obj.get(MINUTE).getAsInt(), obj.get(SECOND).getAsInt(), 0, obj.get(TIMEZONE).getAsInt()); xmlGregCalendar.setFractionalSecond(obj.get(FRACTIONAL).getAsBigDecimal()); return xmlGregCalendar; // use the line below as implementation in Java 8 //return DatatypeFactory.newInstance().newXMLGregorianCalendar(jsonElement.getAsString()); } catch (Exception e) { e.printStackTrace(); return null; } }
@Override public void decimalProperty(PropertyName name, JsonObject context) throws IOException { JsonElement prop = property(context, name); if (prop == null) { return; } builder.add(name, prop.getAsBigDecimal()); }
protected Object readSimpleProperty(JsonElement valueElement, Datatype propertyType) { String value = valueElement.getAsString(); if (value == null) return null; try { Class javaClass = propertyType.getJavaClass(); if (BigDecimal.class.isAssignableFrom(javaClass)) { return valueElement.getAsBigDecimal(); } else if (Long.class.isAssignableFrom(javaClass)) { return valueElement.getAsLong(); } else if (Integer.class.isAssignableFrom(javaClass)) { return valueElement.getAsInt(); } else if (Double.class.isAssignableFrom(javaClass)) { return valueElement.getAsDouble(); } return propertyType.parse(value); } catch (ParseException e) { throw new EntitySerializationException(String.format("An error occurred while parsing property. Type [%s]. Value [%s].", propertyType, value), e); } }
public BigDecimal deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { return json.getAsBigDecimal(); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } catch (UnsupportedOperationException e) { throw new JsonSyntaxException(e); } catch (IllegalStateException e) { throw new JsonSyntaxException(e); } }