javax.json
Interface JsonNumber

All Superinterfaces:
JsonValue

public interface JsonNumber
extends JsonValue

JsonNumber represents a JSON number value and internally a BigDecimal may be used to store the numeric value. The BigDecimal may be constructed using int, long, BigInteger, double and String. Some of the method semantics in this class are defined using the BigDecimal semantics.

Author:
Jitendra Kotamraju

Nested Class Summary
static class JsonNumber.JsonNumberType
          JSON number type
 
Nested classes/interfaces inherited from interface javax.json.JsonValue
JsonValue.JsonValueType
 
Field Summary
 
Fields inherited from interface javax.json.JsonValue
FALSE, NULL, TRUE
 
Method Summary
 BigDecimal getBigDecimalValue()
          Returns JSON number as a BigDecimal
 BigInteger getBigIntegerValue()
          Returns JSON number as a BigInteger number.
 BigInteger getBigIntegerValueExact()
          Returns JSON number as a BigDecimal number.
 double getDoubleValue()
          Returns JSON number as a double number.
 int getIntValue()
          Returns JSON number as an int number.
 int getIntValueExact()
          Returns JSON number as an int number.
 long getLongValue()
          Returns JSON number as a long number.
 long getLongValueExact()
          Returns JSON number as a long number.
 JsonNumber.JsonNumberType getNumberType()
          Returns a JSON number type that can hold the number's numeric value.
 String toString()
          Returns a JSON representation of the JSON number value.
 
Methods inherited from interface javax.json.JsonValue
getValueType
 

Method Detail

getNumberType

JsonNumber.JsonNumberType getNumberType()
Returns a JSON number type that can hold the number's numeric value. A BigDecimal may be used to store the numeric value. If the scale of a value is non-zero, its number type is BIG_DECIMAL. If the scale is zero, and the value is numerically an integer. If the value can be exactly represented as an int, its type is INT; if the value can be exactly represented as a long, its type is LONG; otherwise, its type is BIG_DECIMAL.

This method can be used to get the holding number type and use that information to invoke appropriate methods to get numeric value for a number.

For example:

 switch(getNumberType()) {
     case INT :
         int i = getIntValue(); break;
     case LONG :
         long l = getLongValue(); break;
     case BIG_DECIMAL :
         BigDecimal bd = getBigDecimalValue(); break;
 }
 

Returns:
a number type

getIntValue

int getIntValue()
Returns JSON number as an int number. Note that this conversion can lose information about the overall magnitude and precision of the number value as well as return a result with the opposite sign.

Returns:
an int for JSON number.
See Also:
BigDecimal.intValue()

getIntValueExact

int getIntValueExact()
Returns JSON number as an int number.

Returns:
an int for JSON number
Throws:
ArithmeticException - cause if the number has a nonzero fractional part, or will not fit in an int
See Also:
BigDecimal.intValueExact()

getLongValue

long getLongValue()
Returns JSON number as a long number. Note that this conversion can lose information about the overall magnitude and precision of the number value as well as return a result with the opposite sign.

Returns:
a long for JSON number.
See Also:
BigDecimal.longValue()

getLongValueExact

long getLongValueExact()
Returns JSON number as a long number.

Returns:
a long for JSON number
Throws:
ArithmeticException - if the number has a nonzero fractional part, or will not fit in a long.
See Also:
BigDecimal.longValueExact()

getBigIntegerValue

BigInteger getBigIntegerValue()
Returns JSON number as a BigInteger number. It is more of a convenience method for getBigDecimalValue().toBigInteger(). Note that this conversion can lose information about the overall magnitude and precision of the number value as well as return a result with the opposite sign.

Returns:
a BigInteger for JSON number.
See Also:
BigDecimal.toBigInteger()

getBigIntegerValueExact

BigInteger getBigIntegerValueExact()
Returns JSON number as a BigDecimal number. It is more of a convenience method for getBigDecimalValue().toBigIntegerExact().

Returns:
a BigInteger for JSON number
Throws:
ArithmeticException - if the number has a nonzero fractional part.
See Also:
BigDecimal.toBigIntegerExact()

getDoubleValue

double getDoubleValue()
Returns JSON number as a double number. It is more of a convenience method for getBigDecimalValue().doubleValue(). Note that this conversion can lose information about the overall magnitude and precision of the number value as well as return a result with the opposite sign.

Returns:
a double for JSON number
See Also:
BigDecimal.doubleValue()

getBigDecimalValue

BigDecimal getBigDecimalValue()
Returns JSON number as a BigDecimal

Returns:
a BigDecimal for JSON number

toString

String toString()
Returns a JSON representation of the JSON number value. The representation would be equivalent to BigDecimal.toString().

Overrides:
toString in class Object
Returns:
JSON representation of the number



Copyright © 2012 Oracle and/or its affiliates. All rights reserved.