Class JsonValue

java.lang.Object
org.eclipse.rap.json.JsonValue
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
JsonArray, JsonObject

public abstract class JsonValue extends Object implements Serializable
Represents a JSON value. According to RFC 4627, a JSON value must be an object, an array, a number, a string, or one of the literal names true, false, and null.

The literal names true, false, and null are represented by the constants TRUE, FALSE, and NULL.

JSON objects and arrays are represented by the subtypes JsonObject and JsonArray. Instances of these types can be created using the public constructors.

Instances for JSON numbers and strings can be created using the static factory methods valueOf(String), valueOf(long), valueOf(double) , etc.

In order to find out whether an instance of this class is of a certain type, the methods isObject(), isArray(), isString(), isNumber() etc. can be used.

If there is no doubt about the type of a JSON value, one of the methods asObject(), asArray(), asString(), asInt(), etc. can be used to get this value directly in the appropriate target type.

This class is not supposed to be extended by clients.

Since:
2.1
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final JsonValue
    Represents the JSON literal false.
    static final JsonValue
    The JSON literal null.
    static final JsonValue
    Represents the JSON literal true.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns this JSON value as JsonArray, assuming that this value represents a JSON array.
    boolean
    Returns this JSON value as a boolean value, assuming that this value is either true or false.
    double
    Returns this JSON value as a double value, assuming that this value represents a JSON number.
    float
    Returns this JSON value as a float value, assuming that this value represents a JSON number.
    int
    Returns this JSON value as an int value, assuming that this value represents a JSON number that can be interpreted as Java int.
    long
    Returns this JSON value as a long value, assuming that this value represents a JSON number that can be interpreted as Java long.
    Returns this JSON value as JsonObject, assuming that this value represents a JSON object.
    Returns this JSON value as String, assuming that this value represents a JSON string.
    boolean
    equals(Object object)
    Indicates whether some other object is "equal to" this one according to the contract specified in Object.equals(Object).
    int
     
    boolean
    Detects whether this value represents a JSON array.
    boolean
    Detects whether this value represents a boolean value.
    boolean
    Detects whether this value represents the JSON literal false.
    boolean
    Detects whether this value represents the JSON literal null.
    boolean
    Detects whether this value represents a JSON number.
    boolean
    Detects whether this value represents a JSON object.
    boolean
    Detects whether this value represents a JSON string.
    boolean
    Detects whether this value represents the JSON literal true.
    static JsonValue
    readFrom(Reader reader)
    Reads a JSON value from the given reader.
    static JsonValue
    Reads a JSON value from the given string.
    Returns the JSON string for this value in its minimal form, without any additional whitespace.
    static JsonValue
    valueOf(boolean value)
    Returns a JsonValue instance that represents the given boolean value.
    static JsonValue
    valueOf(double value)
    Returns a JsonValue instance that represents the given double value.
    static JsonValue
    valueOf(float value)
    Returns a JsonValue instance that represents the given float value.
    static JsonValue
    valueOf(int value)
    Returns a JsonValue instance that represents the given int value.
    static JsonValue
    valueOf(long value)
    Returns a JsonValue instance that represents the given long value.
    static JsonValue
    valueOf(String string)
    Returns a JsonValue instance that represents the given string.
    protected abstract void
    write(org.eclipse.rap.json.JsonWriter writer)
     
    void
    writeTo(Writer writer)
    Writes the JSON representation for this object to the given writer.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • TRUE

      public static final JsonValue TRUE
      Represents the JSON literal true.
    • FALSE

      public static final JsonValue FALSE
      Represents the JSON literal false.
    • NULL

      public static final JsonValue NULL
      The JSON literal null.
  • Method Details

    • readFrom

      public static JsonValue readFrom(Reader reader) throws IOException
      Reads a JSON value from the given reader.

      Characters are read in chunks and buffered internally, therefore wrapping an existing reader in an additional BufferedReader does not improve reading performance.

      Parameters:
      reader - the reader to read the JSON value from
      Returns:
      the JSON value that has been read
      Throws:
      IOException - if an I/O error occurs in the reader
      ParseException - if the input is not valid JSON
    • readFrom

      public static JsonValue readFrom(String text)
      Reads a JSON value from the given string.
      Parameters:
      text - the string that contains the JSON value
      Returns:
      the JSON value that has been read
      Throws:
      ParseException - if the input is not valid JSON
    • valueOf

      public static JsonValue valueOf(int value)
      Returns a JsonValue instance that represents the given int value.
      Parameters:
      value - the value to get a JSON representation for
      Returns:
      a JSON value that represents the given value
      Since:
      2.2
    • valueOf

      public static JsonValue valueOf(long value)
      Returns a JsonValue instance that represents the given long value.
      Parameters:
      value - the value to get a JSON representation for
      Returns:
      a JSON value that represents the given value
    • valueOf

      public static JsonValue valueOf(float value)
      Returns a JsonValue instance that represents the given float value.
      Parameters:
      value - the value to get a JSON representation for
      Returns:
      a JSON value that represents the given value
    • valueOf

      public static JsonValue valueOf(double value)
      Returns a JsonValue instance that represents the given double value.
      Parameters:
      value - the value to get a JSON representation for
      Returns:
      a JSON value that represents the given value
    • valueOf

      public static JsonValue valueOf(String string)
      Returns a JsonValue instance that represents the given string.
      Parameters:
      string - the string to get a JSON representation for
      Returns:
      a JSON value that represents the given string
    • valueOf

      public static JsonValue valueOf(boolean value)
      Returns a JsonValue instance that represents the given boolean value.
      Parameters:
      value - the value to get a JSON representation for
      Returns:
      a JSON value that represents the given value
    • isObject

      public boolean isObject()
      Detects whether this value represents a JSON object. If this is the case, this value is an instance of JsonObject.
      Returns:
      true if this value is an instance of JsonObject
    • isArray

      public boolean isArray()
      Detects whether this value represents a JSON array. If this is the case, this value is an instance of JsonArray.
      Returns:
      true if this value is an instance of JsonArray
    • isNumber

      public boolean isNumber()
      Detects whether this value represents a JSON number.
      Returns:
      true if this value represents a JSON number
    • isString

      public boolean isString()
      Detects whether this value represents a JSON string.
      Returns:
      true if this value represents a JSON string
    • isBoolean

      public boolean isBoolean()
      Detects whether this value represents a boolean value.
      Returns:
      true if this value represents either the JSON literal true or false
    • isTrue

      public boolean isTrue()
      Detects whether this value represents the JSON literal true.
      Returns:
      true if this value represents the JSON literal true
    • isFalse

      public boolean isFalse()
      Detects whether this value represents the JSON literal false.
      Returns:
      true if this value represents the JSON literal false
    • isNull

      public boolean isNull()
      Detects whether this value represents the JSON literal null.
      Returns:
      true if this value represents the JSON literal null
    • asObject

      public JsonObject asObject()
      Returns this JSON value as JsonObject, assuming that this value represents a JSON object. If this is not the case, an exception is thrown.
      Returns:
      a JSONObject for this value
      Throws:
      UnsupportedOperationException - if this value is not a JSON object
    • asArray

      public JsonArray asArray()
      Returns this JSON value as JsonArray, assuming that this value represents a JSON array. If this is not the case, an exception is thrown.
      Returns:
      a JSONArray for this value
      Throws:
      UnsupportedOperationException - if this value is not a JSON array
    • asInt

      public int asInt()
      Returns this JSON value as an int value, assuming that this value represents a JSON number that can be interpreted as Java int. If this is not the case, an exception is thrown.

      To be interpreted as Java int, the JSON number must neither contain an exponent nor a fraction part. Moreover, the number must be in the Integer range.

      Returns:
      this value as int
      Throws:
      UnsupportedOperationException - if this value is not a JSON number
      NumberFormatException - if this JSON number can not be interpreted as int value
    • asLong

      public long asLong()
      Returns this JSON value as a long value, assuming that this value represents a JSON number that can be interpreted as Java long. If this is not the case, an exception is thrown.

      To be interpreted as Java long, the JSON number must neither contain an exponent nor a fraction part. Moreover, the number must be in the Long range.

      Returns:
      this value as long
      Throws:
      UnsupportedOperationException - if this value is not a JSON number
      NumberFormatException - if this JSON number can not be interpreted as long value
    • asFloat

      public float asFloat()
      Returns this JSON value as a float value, assuming that this value represents a JSON number. If this is not the case, an exception is thrown.

      If the JSON number is out of the Float range, Float.POSITIVE_INFINITY or Float.NEGATIVE_INFINITY is returned.

      Returns:
      this value as float
      Throws:
      UnsupportedOperationException - if this value is not a JSON number
    • asDouble

      public double asDouble()
      Returns this JSON value as a double value, assuming that this value represents a JSON number. If this is not the case, an exception is thrown.

      If the JSON number is out of the Double range, Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY is returned.

      Returns:
      this value as double
      Throws:
      UnsupportedOperationException - if this value is not a JSON number
    • asString

      public String asString()
      Returns this JSON value as String, assuming that this value represents a JSON string. If this is not the case, an exception is thrown.
      Returns:
      the string represented by this value
      Throws:
      UnsupportedOperationException - if this value is not a JSON string
    • asBoolean

      public boolean asBoolean()
      Returns this JSON value as a boolean value, assuming that this value is either true or false. If this is not the case, an exception is thrown.
      Returns:
      this value as boolean
      Throws:
      UnsupportedOperationException - if this value is neither true or false
    • writeTo

      public void writeTo(Writer writer) throws IOException
      Writes the JSON representation for this object to the given writer.

      Single elements are passed directly to the given writer. Therefore, if the writer is not buffered, wrapping it in a BufferedWriter can drastically improve writing performance.

      Parameters:
      writer - the writer to write this value to
      Throws:
      IOException - if an I/O error occurs in the writer
    • toString

      public String toString()
      Returns the JSON string for this value in its minimal form, without any additional whitespace. The result is guaranteed to be a valid input for the method readFrom(String) and to create a value that is equal to this object.
      Overrides:
      toString in class Object
      Returns:
      a JSON string that represents this value
    • equals

      public boolean equals(Object object)
      Indicates whether some other object is "equal to" this one according to the contract specified in Object.equals(Object).

      Two JsonValues are considered equal if and only if they represent the same JSON text. As a consequence, two given JsonObjects may be different even though they contain the same set of names with the same values, but in a different order.

      Overrides:
      equals in class Object
      Parameters:
      object - the reference object with which to compare
      Returns:
      true if this object is the same as the object argument; false otherwise
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • write

      protected abstract void write(org.eclipse.rap.json.JsonWriter writer) throws IOException
      Throws:
      IOException