Class JsonNumber

java.lang.Object
com.azure.json.models.JsonElement
com.azure.json.models.JsonNumber
All Implemented Interfaces:
JsonSerializable<JsonElement>

public final class JsonNumber extends JsonElement
Class representing the JSON number type
  • Constructor Details

    • JsonNumber

      public JsonNumber(Number value)
      Creates a JsonNumber representing the specified number.
      Parameters:
      value - The number value the JsonNumber will represent.
      Throws:
      NullPointerException - If value is null.
  • Method Details

    • 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:
      isNumber in class JsonElement
      Returns:
      boolean of whether this JsonElement object is of type JsonNumber.
    • toJson

      public JsonWriter toJson(JsonWriter jsonWriter) throws IOException
      Description copied from interface: JsonSerializable
      Writes the object to the passed JsonWriter.

      The contract for writing JSON to JsonWriter is that the object being written will handle opening and closing its own JSON object. So, for objects calling out to other JsonSerializable objects for serialization, they'll write the field name only then pass the JsonWriter to the other JsonSerializable object. This way objects writing JSON will be self-encapsulated for writing properly formatted JSON.

      Parameters:
      jsonWriter - Where the object's JSON will be written.
      Returns:
      The JsonWriter where the JSON was written.
      Throws:
      IOException - If the object fails to be written to the jsonWriter.
    • fromJson

      public static JsonNumber fromJson(JsonReader jsonReader) throws IOException
      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 JsonReader.nextToken() will be called to begin reading.

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

      The JsonNumber returned will have a getValue() that is the smallest type that can represent the numeric value. Numeric types used are Integer, Long, BigInteger, Float, Double, and BigDecimal.

      Parameters:
      jsonReader - The JsonReader to deserialize from.
      Returns:
      The deserialized JSON number.
      Throws:
      IOException - If an error occurs while deserializing the JSON number.
      IllegalStateException - If the current token is not JsonToken.NUMBER.
    • toJsonString

      public String toJsonString() throws IOException
      Description copied from interface: JsonSerializable
      Convenience method for writing the JsonSerializable to a JSON string.
      Returns:
      The JSON string representing the object.
      Throws:
      IOException - If the object fails to be written as a JSON string.