Class Json5Object


  • public final class Json5Object
    extends Json5Element
    A class representing an object type in Json5. An object consists of name-value pairs where names are strings, and values are any other type of Json5Element. This allows for a creating a tree of Json5Elements. The member elements of this object are maintained in order they were added. This class does not support null values. If null is provided as value argument to any of the methods, it is converted to a Json5Null.

    Json5Object does not implement the Map interface, but a Map view of it can be obtained with asMap().

    See the Json5 documentation for details on how to convert Json5Object and generally any Json5Element from and to Json5.

    • Constructor Detail

      • Json5Object

        public Json5Object()
        Creates a new instance of a Json5Object.
    • Method Detail

      • deepCopy

        public Json5Object deepCopy()
        Creates a deep copy of this element and all its children.
        Specified by:
        deepCopy in class Json5Element
        Returns:
        Deep copy
      • add

        public void add​(java.lang.String property,
                        Json5Element value)
        Adds a member, which is a name-value pair, to self. The name must be a String, but the value can be an arbitrary Json5Element, thereby allowing you to build a full tree of Json5Elements rooted at this node.
        Parameters:
        property - name of the member.
        value - the member object.
      • remove

        public Json5Element remove​(java.lang.String property)
        Removes the property from this object.
        Parameters:
        property - name of the member that should be removed.
        Returns:
        the Json5Element object that is being removed, or null if no member with this name exists.
      • addProperty

        public void addProperty​(java.lang.String property,
                                java.lang.Character value)
        Convenience method to add a char member. The specified value is converted to a Json5Primitive of Character.
        Parameters:
        property - name of the member.
        value - the char value associated with the member.
      • addProperty

        public void addProperty​(java.lang.String property,
                                java.lang.String value)
        Convenience method to add a string member. The specified value is converted to a Json5Primitive of String.
        Parameters:
        property - name of the member.
        value - the string value associated with the member.
      • addProperty

        public void addProperty​(java.lang.String property,
                                java.lang.Number value)
        Convenience method to add a number member. The specified value is converted to a Json5Primitive of Number.
        Parameters:
        property - name of the member.
        value - the number value associated with the member.
      • addProperty

        public void addProperty​(java.lang.String property,
                                java.lang.Number value,
                                int radix)
      • addProperty

        public void addProperty​(java.lang.String property,
                                java.time.Instant value)
        Convenience method to add a Instant member. The specified value is converted to a Json5Primitive of Instant.
        Parameters:
        property - name of the member.
        value - the Instant value associated with the member.
      • addProperty

        public void addProperty​(java.lang.String property,
                                java.lang.Boolean value)
        Convenience method to add a boolean member. The specified value is converted to a Json5Primitive of Boolean.
        Parameters:
        property - name of the member.
        value - the boolean value associated with the member.
      • entrySet

        public java.util.Set<java.util.Map.Entry<java.lang.String,​Json5Element>> entrySet()
        Returns a set of members of this object. The set is ordered, and the order is in which the elements were added.
        Returns:
        a set of members of this object.
      • keySet

        public java.util.Set<java.lang.String> keySet()
        Returns a set of members key values.
        Returns:
        a set of member keys as Strings
      • size

        public int size()
        Returns the number of key/value pairs in the object.
        Returns:
        the number of key/value pairs in the object.
      • isEmpty

        public boolean isEmpty()
        Returns true if the number of key/value pairs in the object is zero.
        Returns:
        true if the number of key/value pairs in the object is zero.
      • has

        public boolean has​(java.lang.String memberName)
        Convenience method to check if a member with the specified name is present in this object.
        Parameters:
        memberName - name of the member that is being checked for presence.
        Returns:
        true if there is a member with the specified name, false otherwise.
      • get

        public Json5Element get​(java.lang.String memberName)
        Returns the member with the specified name.
        Parameters:
        memberName - name of the member that is being requested.
        Returns:
        the member matching the name, or null if no such member exists.
      • getAsJson5Primitive

        public Json5Primitive getAsJson5Primitive​(java.lang.String memberName)
        Convenience method to get the specified member as a Json5Primitive.
        Parameters:
        memberName - name of the member being requested.
        Returns:
        the Json5Primitive corresponding to the specified member, or null if no member with this name exists.
        Throws:
        java.lang.ClassCastException - if the member is not of type Json5Primitive.
      • getAsJson5Array

        public Json5Array getAsJson5Array​(java.lang.String memberName)
        Convenience method to get the specified member as a Json5Array.
        Parameters:
        memberName - name of the member being requested.
        Returns:
        the Json5Array corresponding to the specified member, or null if no member with this name exists.
        Throws:
        java.lang.ClassCastException - if the member is not of type Json5Array.
      • getAsJson5Object

        public Json5Object getAsJson5Object​(java.lang.String memberName)
        Convenience method to get the specified member as a Json5Object.
        Parameters:
        memberName - name of the member being requested.
        Returns:
        the Json5Object corresponding to the specified member, or null if no member with this name exists.
        Throws:
        java.lang.ClassCastException - if the member is not of type Json5Object.
      • asMap

        public java.util.Map<java.lang.String,​Json5Element> asMap()
        Returns a mutable Map view of this Json5Object. Changes to the Map are visible in this Json5Object and the other way around.

        The Map does not permit null keys or values. Unlike Json5Object's null handling, a NullPointerException is thrown when trying to add null. Use Json5Null for Json5 null values.

        Returns:
        mutable Map view
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class Json5Element