Share via


JsonNumber Class

public final class JsonNumber
extends JsonElement

Class representing the JSON number type

Constructor Summary

Constructor Description
JsonNumber(Number value)

Creates a JsonNumber representing the specified number.

Method Summary

Modifier and Type Method and Description
static JsonNumber fromJson(JsonReader jsonReader)

Deserializes a JSON number from a JsonReader.

Number getValue()

Returns the Number value from a JsonNumber object.

boolean isNumber()

Whether the JsonElement is a JsonNumber.

JsonWriter toJson(JsonWriter jsonWriter)
String toJsonString()

Methods inherited from JsonElement

Methods inherited from java.lang.Object

Constructor Details

JsonNumber

public JsonNumber(Number value)

Creates a JsonNumber representing the specified number.

Parameters:

value - The number value the JsonNumber will represent.

Method Details

fromJson

public static JsonNumber fromJson(JsonReader jsonReader)

Deserializes a JSON number from a JsonReader.

If the JsonReader's current token is null, it is assumed the JsonReader hasn't begun reading and nextToken() will be called to begin reading.

After ensuring the JsonReader has begun reading, if the current token is not NUMBER, an IllegalStateException will be thrown. Otherwise, a JSON number representing the numeric value will be created and returned.

For integer numbers, this method will return the smallest number type that can represent the number. Where Integer is preferred over Long and Long is preferred over BigInteger.

For floating point numbers, Double will be preferred but BigDecimal will be used if the number is too large to fit in a Double.

If the string is one of the special floating point representations (NaN, Infinity, etc), then the value will be represented using Double.

Parameters:

jsonReader - The JsonReader to deserialize from.

Returns:

The deserialized JSON number.

Throws:

IOException

- If an error occurs while deserializing the JSON number.

getValue

public Number getValue()

Returns the Number value from a JsonNumber object.

The value returned by this method will never be null.

Returns:

The Number value.

isNumber

public boolean isNumber()

Whether the JsonElement is a JsonNumber.

Overrides:

JsonNumber.isNumber()

Returns:

boolean of whether this JsonElement object is of type JsonNumber.

toJson

public JsonWriter toJson(JsonWriter jsonWriter)

Parameters:

jsonWriter

Throws:

toJsonString

public String toJsonString()

Throws:

Applies to