Package jsonvalues

Class JsObj

  • All Implemented Interfaces:
    Iterable<io.vavr.Tuple2<String,​JsValue>>, Json<JsObj>, JsValue

    public class JsObj
    extends Object
    implements Json<JsObj>, Iterable<io.vavr.Tuple2<String,​JsValue>>
    Represents a json object, which is an unordered set of name/element pairs. Two implementations are provided, an immutable which uses the persistent Scala HashMap, and a mutable which uses the conventional Java HashMap.
    • Field Detail

      • EMPTY

        public static final JsObj EMPTY
    • Method Detail

      • append

        public final JsObj append​(JsPath path,
                                  JsValue elem)
        Description copied from interface: Json
        Appends one element to the array located at the given path in this json. If the array doesn't exist, a new one is created, replacing any existing element in the path and filling empty indexes in arrays with JsNull when necessary. The same this instance is returned when it's an array and the head of the path is a key or when it's an object and the head of the path is an index.
        Specified by:
        append in interface Json<JsObj>
        Parameters:
        path - the path pointing to the array in which the element will be appended
        elem - the JsElem to be appended to the existing or created array
        Returns:
        same this instance or a new json of the same type T
      • appendAll

        public final JsObj appendAll​(JsPath path,
                                     JsArray elems)
        Description copied from interface: Json
        Appends all the elements of the array, starting from the head, to the array located at the given path in this json. If the array doesn't exist, a new one is created, replacing any existing element in the path and filling empty indexes in arrays with JsNull when necessary. The same this instance is returned when it's an array and the head of the path is a key or when it's an object and the head of the path is an index.
        Specified by:
        appendAll in interface Json<JsObj>
        Parameters:
        path - the given JsPath pointing to the array in which all the elements will be appended
        elems - the JsArray of elements to be appended
        Returns:
        same this instance or a new json of the same type T
      • containsValue

        public final boolean containsValue​(JsValue el)
        Description copied from interface: Json
        Returns true if this json contains the given element in the first level.
        Specified by:
        containsValue in interface Json<JsObj>
        Parameters:
        el - the give element JsElem whose presence in this JsArray is to be tested
        Returns:
        true if this JsArray contains the JsElem
      • empty

        public static JsObj empty()
      • equals

        public boolean equals​(JsObj that,
                              JsArray.TYPE ARRAY_AS)
        return true if this obj is equal to the given as a parameter. In the case of ARRAY_AS=LIST, this method is equivalent to JsObj.equals(Object).
        Parameters:
        that - the given array
        ARRAY_AS - enum to specify if arrays are considered as lists or sets or multisets
        Returns:
        true if both objs are equals
      • equals

        public final boolean equals​(@Nullable Object that)
        Overrides:
        equals in class Object
      • fields

        public final Set<String> fields()
        Returns a set containing each key fo this object.
        Returns:
        a Set containing each key of this JsObj
      • filterAllKeys

        public JsObj filterAllKeys​(Predicate<? super JsPair> filter)
        Description copied from interface: Json
        Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.
        Specified by:
        filterAllKeys in interface Json<JsObj>
        Parameters:
        filter - the predicate which takes as the input every JsPair of this json
        Returns:
        same this instance if all the keys satisfy the predicate or a new filtered json of the same type T
        See Also:
        how to filter the keys of only the first level
      • filterAllObjs

        public final JsObj filterAllObjs​(BiPredicate<? super JsPath,​? super JsObj> filter)
        Description copied from interface: Json
        Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.
        Specified by:
        filterAllObjs in interface Json<JsObj>
        Parameters:
        filter - the predicate which takes as the input every JsPair of this json
        Returns:
        same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
        See Also:
        how to filter the pair of jsons of only the first level
      • filterAllValues

        public final JsObj filterAllValues​(Predicate<? super JsPair> filter)
        Description copied from interface: Json
        Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.
        Specified by:
        filterAllValues in interface Json<JsObj>
        Parameters:
        filter - the predicate which takes as the input every JsPair of this json
        Returns:
        same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
        See Also:
        how to filter the pairs of values of only the first level
      • filterKeys

        public final JsObj filterKeys​(Predicate<? super JsPair> filter)
        Description copied from interface: Json
        Filters the keys in the first level of this json, removing those that don't ifPredicateElse the predicate.
        Specified by:
        filterKeys in interface Json<JsObj>
        Parameters:
        filter - the predicate which takes as the input every JsPair in the first level of this json
        Returns:
        same this instance if all the keys satisfy the predicate or a new filtered json of the same type T
        See Also:
        how to filter the keys of the whole json and not only the first level
      • get

        public final JsValue get​(Position position)
        Description copied from interface: Json
        Returns the element located at the key or index specified by the given position or JsNothing if it doesn't exist.
        Specified by:
        get in interface Json<JsObj>
        Parameters:
        position - key or index of the element
        Returns:
        the JsElem located at the given Position or JsNothing if it doesn't exist
      • hashCode

        public final int hashCode()
        equals method is inherited, so it's implemented. The purpose of this method is to cache the hashcode once calculated. the object is immutable and it won't change Single-check idiom Item 83 from Effective Java
        Overrides:
        hashCode in class Object
      • head

        public final io.vavr.Tuple2<String,​JsValue> head()
        Returns a pair with an arbitrary key of this object and its associated element. When using head and tail to process a JsObj, the key of the pair returned must be passed in to get the tail using the method tail().
        Returns:
        an arbitrary Map.Entry<String,JsElem> of this JsObj
        Throws:
        UserError - if this json object is empty
      • id

        public int id()
        Specified by:
        id in interface JsValue
      • intersection

        public JsObj intersection​(JsObj that,
                                  JsArray.TYPE ARRAY_AS)
        this.intersection(that, SET) returns an array with the elements that exist in both this and that this.intersection(that, MULTISET) returns an array with the elements that exist in both this and that, being duplicates allowed. this.intersection(that, LIST) returns an array with the elements that exist in both this and that, and are located at the same position.
        Parameters:
        that - the other obj
        ARRAY_AS - option to define if arrays are considered SETS, LISTS OR MULTISET
        Returns:
        a new JsObj of the same type as the inputs (mutable or immutable)
      • intersectionAll

        public JsObj intersectionAll​(JsObj that,
                                     JsArray.TYPE ARRAY_AS)
        this.intersectionAll(that) behaves as this.intersection(that, LIST), but for those elements that are containers of the same type and are located at the same position, the result is their intersection. So this operation is kind of a 'recursive' intersection.
        Parameters:
        that - the other object
        ARRAY_AS - option to define if arrays are considered SETS, LISTS OR MULTISET
        Returns:
        a new JsObj of the same type as the inputs (mutable or immutable)
      • isEmpty

        public final boolean isEmpty()
        Description copied from interface: Json
        return true if there's no element in this json
        Specified by:
        isEmpty in interface Json<JsObj>
        Returns:
        true if empty, false otherwise
      • isObj

        public boolean isObj()
        Specified by:
        isObj in interface JsValue
        Returns:
        true if this JsElem is a JsObj
      • of

        public static JsObj of​(String key,
                               JsValue el)
        Returns a one-element immutable object.
        Parameters:
        key - name of a key
        el - JsElem to be associated to the key
        Returns:
        an immutable one-element JsObj
        Throws:
        UserError - if an elem is a mutable Json
      • of

        public static JsObj of​(String key1,
                               JsValue el1,
                               String key2,
                               JsValue el2)
        Returns a two-element immutable object.
        Parameters:
        key1 - name of a key
        el1 - JsElem to be associated to the key1
        key2 - name of a key
        el2 - JsElem to be associated to the key2
        Returns:
        an immutable two-element JsObj
        Throws:
        UserError - if an elem is a mutable Json
      • of

        public static JsObj of​(String key1,
                               JsValue el1,
                               String key2,
                               JsValue el2,
                               String key3,
                               JsValue el3)
        Returns a three-element immutable object.
        Parameters:
        key1 - name of a key
        el1 - JsElem to be associated to the key1
        key2 - name of a key
        el2 - JsElem to be associated to the key2
        key3 - name of a key
        el3 - JsElem to be associated to the key3
        Returns:
        an immutable three-element JsObj
        Throws:
        UserError - if an elem is a mutable Json
      • of

        public static JsObj of​(String key1,
                               JsValue el1,
                               String key2,
                               JsValue el2,
                               String key3,
                               JsValue el3,
                               String key4,
                               JsValue el4)
        Returns a four-element immutable object.
        Parameters:
        key1 - name of a key
        el1 - JsElem to be associated to the key1
        key2 - name of a key
        el2 - JsElem to be associated to the key2
        key3 - name of a key
        el3 - JsElem to be associated to the key3
        key4 - name of a key
        el4 - JsElem to be associated to the key4
        Returns:
        an immutable four-element JsObj
        Throws:
        UserError - if an elem is a mutable Json
      • of

        public static JsObj of​(String key1,
                               JsValue el1,
                               String key2,
                               JsValue el2,
                               String key3,
                               JsValue el3,
                               String key4,
                               JsValue el4,
                               String key5,
                               JsValue el5)
        Returns a five-element immutable object.
        Parameters:
        key1 - name of a key
        el1 - JsElem to be associated to the key1
        key2 - name of a key
        el2 - JsElem to be associated to the key2
        key3 - name of a key
        el3 - JsElem to be associated to the key3
        key4 - name of a key
        el4 - JsElem to be associated to the key4
        key5 - name of a key
        el5 - JsElem to be associated to the key5
        Returns:
        an immutable five-element JsObj
        Throws:
        UserError - if an elem is a mutable Json
      • of

        public static JsObj of​(String key1,
                               JsValue el1,
                               String key2,
                               JsValue el2,
                               String key3,
                               JsValue el3,
                               String key4,
                               JsValue el4,
                               String key5,
                               JsValue el5,
                               String key6,
                               JsValue el6)
        Returns a six-element immutable object.
        Parameters:
        key1 - name of a key
        el1 - JsElem to be associated to the key1
        key2 - name of a key
        el2 - JsElem to be associated to the key2
        key3 - name of a key
        el3 - JsElem to be associated to the key3
        key4 - name of a key
        el4 - JsElem to be associated to the key4
        key5 - name of a key
        el5 - JsElem to be associated to the key5
        key6 - name of a key
        el6 - JsElem to be associated to the key6
        Returns:
        an immutable six-element JsObj
        Throws:
        UserError - if an elem is a mutable Json
      • of

        public static JsObj of​(JsPair pair,
                               JsPair... others)
        Returns an immutable object from one or more pairs.
        Parameters:
        pair - a pair
        others - more optional pairs
        Returns:
        an immutable JsObject
        Throws:
        UserError - if an elem of a pair is mutable
      • parse

        public static JsObj parse​(String str)
                           throws MalformedJson
        Tries to parse the string into an immutable object.
        Parameters:
        str - the string to be parsed
        Returns:
        a JsOb object
        Throws:
        MalformedJson - if the string doesnt represent a json object
      • parse

        public static JsObj parse​(String str,
                                  ParseBuilder builder)
                           throws MalformedJson
        Tries to parse the string into an immutable object, performing the specified transformations during the parsing. It's faster to do certain operations right while the parsing instead of doing the parsing and applying them later.
        Parameters:
        str - string to be parsed
        builder - builder with the transformations that will be applied during the parsing
        Returns:
        a JsObj object
        Throws:
        MalformedJson - if the string doesnt represent a json object
      • prepend

        public final JsObj prepend​(JsPath path,
                                   JsValue elem)
        Description copied from interface: Json
        prepends one element to the array located at the path in this json. If the array at the path doesn't exist, a new one is created, replacing any existing element in the path and filling empty indexes in arrays with JsNull when necessary. The same this instance is returned when it's an array and the head of the path is a key or when it's an object and the head of the path is an index.
        Specified by:
        prepend in interface Json<JsObj>
        Parameters:
        path - the JsPath pointing to the array in which the element will be prepended
        elem - the JsElem to be prepended to the existing or created array
        Returns:
        same this instance or a new json of the same type T
      • prependAll

        public final JsObj prependAll​(JsPath path,
                                      JsArray elems)
        Description copied from interface: Json
        prepends all the elements of the array, starting from the head, to the array located at the path in this json. If the array at the path doesn't exist, a new one is created, replacing any existing element in the path and filling empty indexes in arrays with JsNull when necessary. The same this instance is returned when it's an array and the head of the path is a key or when it's an object and the head of the path is an index.
        Specified by:
        prependAll in interface Json<JsObj>
        Parameters:
        path - the JsPath pointing to the array in which all the elements will be prepended
        elems - the JsArray of elements to be prepended to the existing or created array
        Returns:
        same this instance or a new json of the same type T
      • put

        public final JsObj put​(JsPath path,
                               Function<? super JsValue,​? extends JsValue> fn)
        Description copied from interface: Json
        Inserts the element returned by the function at the given path in this json, replacing any existing element and filling with JsNull empty indexes in arrays when necessary. The same instance is returned when the path is empty, or the head of the path is a key and this is an array or the head of the path is an index and this is an object. In both cases the function is not invoked. The same instance is returned as well when the element returned by the function is JsNothing
        Specified by:
        put in interface Json<JsObj>
        Parameters:
        path - the JsPath object where the JsElem will be inserted at
        fn - the function that takes as an input the JsElem at the path and produces the JsElem to be inserted at the path
        Returns:
        the same instance or a new json of the same type T
      • reduce

        public final <R> Optional<R> reduce​(BinaryOperator<R> op,
                                            Function<? super JsPair,​R> map,
                                            Predicate<? super JsPair> predicate)
        Description copied from interface: Json
        Performs a reduction on the values that satisfy the predicate in the first level of this json. The reduction is performed mapping each value with the mapping function and then applying the operator
        Specified by:
        reduce in interface Json<JsObj>
        Type Parameters:
        R - the type of the operands of the operator
        Parameters:
        op - the operator upon two objects of type R
        map - the mapping function which produces an object of type R from a JsValue
        predicate - the predicate that determines what JsValue will be mapped and reduced
        Returns:
        an Optional describing the of of the reduction
        See Also:
        to apply the reduction in all the Json and not only in the first level
      • reduceAll

        public final <R> Optional<R> reduceAll​(BinaryOperator<R> op,
                                               Function<? super JsPair,​R> map,
                                               Predicate<? super JsPair> predicate)
        Description copied from interface: Json
        Performs a reduction on the values of this json that satisfy the predicate. The reduction is performed mapping each value with the mapping function and then applying the operator
        Specified by:
        reduceAll in interface Json<JsObj>
        Type Parameters:
        R - the type of the operands of the operator
        Parameters:
        op - the operator upon two objects of type R
        map - the mapping function which produces an object of type R from a JsValue
        predicate - the predicate that determines what JsValue will be mapped and reduced
        Returns:
        an Optional describing the result of the reduction
        See Also:
        to apply the reduction only in the first level
      • remove

        public final JsObj remove​(JsPath path)
        Description copied from interface: Json
        Removes the element in this json located at the given path, if it exists, returning the same this instance otherwise
        Specified by:
        remove in interface Json<JsObj>
        Parameters:
        path - the given JsPath object
        Returns:
        a json of the same type T
      • same

        public final boolean same​(JsObj obj)
      • size

        public final int size()
        Description copied from interface: Json
        Returns the number of elements in the first level of this json
        Specified by:
        size in interface Json<JsObj>
        Returns:
        the number of elements in the first level of this json
      • stream

        public final Stream<JsPair> stream()
        Description copied from interface: Json
        Returns a stream over the pairs of elements in the first level of this json object.
        Specified by:
        stream in interface Json<JsObj>
        Returns:
        a Stream over all the JsPairs in the first level of this json
      • streamAll

        public final Stream<JsPair> streamAll()
        Description copied from interface: Json
        Returns a stream over all the pairs of elements in this json object.
        Specified by:
        streamAll in interface Json<JsObj>
        Returns:
        a Stream over all the JsPairs in this json
      • tail

        public final JsObj tail()
        Returns a new object with all the entries of this json object except the one with the given key.
        Returns:
        a new JsObj
        Throws:
        UserError - if this json object is empty
      • toString

        public final String toString()
        // Single-check idiom Item 83 from effective java
        Overrides:
        toString in class Object
      • union

        public final JsObj union​(JsObj that)
        returns this json object plus those pairs from the given json object that which keys don't exist in this. Taking that into account, it's not a commutative operation unless the elements associated with the keys that exist in both json objects are equals.
        Parameters:
        that - the given json object
        Returns:
        a new JsObj of the same type as the inputs (mutable or immutable)
      • unionAll

        public final JsObj unionAll​(JsObj that,
                                    JsArray.TYPE ARRAY_AS)
        behaves like the union(JsObj) but, for those keys that exit in both this and that json objects, which associated elements are **containers of the same type**, the result is their union. In this case, we can specify if arrays are considered Sets, Lists, or MultiSets. So this operation is kind of a 'recursive' union.
        Parameters:
        that - the given json object
        ARRAY_AS - option to define if arrays are considered SETS, LISTS OR MULTISET
        Returns:
        a new JsObj of the same type as the inputs (mutable or immutable)