public static BigDecimal toBigDecimal(JSONValue value) { JSONNumber number = value.isNumber(); if (number == null) { throw new DecodingException("Expected a json number, but was given: " + value); } return new BigDecimal(value.toString()); }
@Override public BigInteger decode(JSONValue value) throws DecodingException { if (value == null || value.isNull() != null) { return null; } JSONNumber number = value.isNumber(); if (number == null) { JSONString str = value.isString(); if (str == null) { throw new DecodingException( "Expected a json number r string, but was given: " + value); } // Doing a straight conversion from string to BigInteger will // not work for large values // So we convert to BigDecimal first and then convert it to // BigInteger. return new BigDecimal(str.stringValue()).toBigInteger(); } // Doing a straight conversion from string to BigInteger will not // work for large values // So we convert to BigDecimal first and then convert it to // BigInteger. return new BigDecimal(value.toString()).toBigInteger(); }
public static Integer jsonValueToInteger(JSONValue jsonValue) { if (jsonValue != null) { JSONNumber jsonNumber = jsonValue.isNumber(); if (jsonNumber != null) { return (int) jsonNumber.doubleValue(); } } return null; }
public static Long jsonValueToLong(JSONValue jsonValue) { if (jsonValue != null) { JSONNumber jsonNumber = jsonValue.isNumber(); if (jsonNumber != null) { return (long) jsonNumber.doubleValue(); } } return null; }
public static Double jsonValueToDouble(JSONValue jsonValue) { if (jsonValue != null) { JSONNumber jsonNumber = jsonValue.isNumber(); if (jsonNumber != null) { return jsonNumber.doubleValue(); } } return null; }
public static Float jsonValueToFloat(JSONValue jsonValue) { if (jsonValue != null) { JSONNumber jsonNumber = jsonValue.isNumber(); if (jsonNumber != null) { return (float) jsonNumber.doubleValue(); } } return null; }
@Override public String decode(JSONValue value) throws DecodingException { if (value == null || value.isNull() != null) { return null; } JSONString str = value.isString(); if (str == null) { if (value.isBoolean() != null || value.isNumber() != null) { return value.toString(); } throw new DecodingException("Expected a json string, but was given: " + value); } return str.stringValue(); }
public static double toDouble(JSONValue value) { JSONNumber number = value.isNumber(); if (number == null) { JSONString val = value.isString(); if (val != null) { try { return Double.parseDouble(val.stringValue()); } catch (NumberFormatException e) { // just through exception below } } throw new DecodingException("Expected a json number, but was given: " + value); } return number.doubleValue(); }
@Override public Date decode(JSONValue value) throws DecodingException { if (value == null || value.isNull() != null) { return null; } String format = Defaults.getDateFormat(); if (format == null) { JSONNumber num = value.isNumber(); if (num == null) { throw new DecodingException("Expected a json number, but was given: " + value); } return new Date((long) num.doubleValue()); } JSONString str = value.isString(); if (str == null) { throw new DecodingException("Expected a json string, but was given: " + value); } return DateTimeFormat.getFormat(format).parse(str.stringValue()); }
@Override protected Long deserializeJson(JSONValue o) { return (long) o.isNumber().doubleValue(); }