Class JsonObject

java.lang.Object
com.revetsec.json.JsonObject
All Implemented Interfaces:
JsonValue

@Immutable public final class JsonObject extends Object implements JsonValue
A JSON object: an unmodifiable map from member names to values that keeps the order in which members were added.

Names are unique and compared exactly, with no case folding or Unicode normalization. Two objects are equal when they have the same names with equal values, whatever the order of their members.

Building. builder() adds one member per put call, and a name that is already present throws IllegalArgumentException instead of replacing the earlier value. fromMembers(Map) takes a complete map.

Member lookups. find(String) returns an empty Optional only when a member is absent. A present member retains its JsonValue type, including JsonNull; callers can inspect that type to distinguish an absent member from a malformed one.

Since:
1.0.0
Author:
Mark Allen
  • Method Details

    • emptyInstance

      Returns the empty JSON object.
      Returns:
      the shared empty object
      Since:
      1.0.0
    • builder

      Returns a new builder, which adds one member per put call.
      Returns:
      a builder with no members
      Since:
      1.0.0
    • fromMembers

      Returns a JSON object holding a snapshot of members, in the map's iteration order.
      Parameters:
      members - the members; later changes to the map do not affect the object
      Returns:
      the JSON object
      Throws:
      NullPointerException - if members, or any name or value in it, is null
      IllegalArgumentException - if a name contains an unpaired surrogate, the map holds two equal names (as an identity-based map can), or the object would nest more than 64 deep
      Since:
      1.0.0
    • getMembers

      Returns this object's members, in the order they were added.
      Returns:
      an unmodifiable map
      Since:
      1.0.0
    • find

      Returns the value of the member named name.
      Parameters:
      name - the member name, compared exactly
      Returns:
      the value, which may be JsonNull, or empty if there is no such member
      Throws:
      NullPointerException - if name is null
      Since:
      1.0.0
    • toJson

      Description copied from interface: JsonValue
      Serializes this value as compact JSON text: no insignificant whitespace, members in insertion order, and numbers in the canonical form of BigDecimal.toString() (1e2 becomes 1E+2).

      Strings escape only what JSON requires: the quotation mark, the reverse solidus and the control characters U+0000 to U+001F, using the two-character escapes where they exist. Every other character, U+2028 and U+2029 included, is written as itself.

      Unlike toString(), the result contains every string and number this value holds. It is not the text a value was parsed from, so never verify a signature over it.

      Specified by:
      toJson in interface JsonValue
      Returns:
      the JSON text
    • equals

      public boolean equals(@Nullable Object other)
      Overrides:
      equals in class Object
    • hashCode

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

      Overrides:
      toString in class Object