Class JsonContext

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      DocumentContext add​(JsonPath path, java.lang.Object value)
      Add value to array at the given path
      DocumentContext add​(java.lang.String path, java.lang.Object value, Predicate... filters)
      Add value to array
      Configuration configuration()
      Returns the configuration used for reading
      DocumentContext delete​(JsonPath path)
      Deletes the given path
      DocumentContext delete​(java.lang.String path, Predicate... filters)
      Deletes the given path
      java.lang.Object json()
      Returns the JSON model that this context is operating on
      java.lang.String jsonString()
      Returns the JSON model that this context is operating on as a JSON string
      ReadContext limit​(int maxResults)
      Stops evaluation when maxResults limit has been reached
      DocumentContext map​(JsonPath path, MapFunction mapFunction)
      Replaces the value on the given path with the result of the MapFunction.
      DocumentContext map​(java.lang.String path, MapFunction mapFunction, Predicate... filters)
      Replaces the value on the given path with the result of the MapFunction.
      DocumentContext put​(JsonPath path, java.lang.String key, java.lang.Object value)
      Add or update the key with a the given value at the given path
      DocumentContext put​(java.lang.String path, java.lang.String key, java.lang.Object value, Predicate... filters)
      Add or update the key with a the given value at the given path
      <T> T read​(JsonPath path)
      Reads the given path from this context
      <T> T read​(JsonPath path, TypeRef<T> type)
      Reads the given path from this context Sample code to create a TypeRef TypeRef ref = new TypeRef>() {};
      <T> T read​(JsonPath path, java.lang.Class<T> type)
      Reads the given path from this context
      <T> T read​(java.lang.String path, Predicate... filters)
      Reads the given path from this context
      <T> T read​(java.lang.String path, TypeRef<T> type)
      Reads the given path from this context Sample code to create a TypeRef TypeRef ref = new TypeRef>() {};
      <T> T read​(java.lang.String path, java.lang.Class<T> type, Predicate... filters)
      Reads the given path from this context
      DocumentContext renameKey​(JsonPath path, java.lang.String oldKeyName, java.lang.String newKeyName)
      Renames the last key element of a given path.
      DocumentContext renameKey​(java.lang.String path, java.lang.String oldKeyName, java.lang.String newKeyName, Predicate... filters)
      Renames the last key element of a given path.
      DocumentContext set​(JsonPath path, java.lang.Object newValue)
      Set the value a the given path
      DocumentContext set​(java.lang.String path, java.lang.Object newValue, Predicate... filters)
      Set the value a the given path
      ReadContext withListeners​(EvaluationListener... listener)
      Adds listener to the evaluation of this path
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • json

        public java.lang.Object json()
        Description copied from interface: ReadContext
        Returns the JSON model that this context is operating on
        Specified by:
        json in interface ReadContext
        Specified by:
        json in interface WriteContext
        Returns:
        json model
      • jsonString

        public java.lang.String jsonString()
        Description copied from interface: ReadContext
        Returns the JSON model that this context is operating on as a JSON string
        Specified by:
        jsonString in interface ReadContext
        Specified by:
        jsonString in interface WriteContext
        Returns:
        json model as string
      • read

        public <T> T read​(java.lang.String path,
                          Predicate... filters)
        Description copied from interface: ReadContext
        Reads the given path from this context
        Specified by:
        read in interface ReadContext
        Parameters:
        path - path to read
        filters - filters
        Returns:
        result
      • read

        public <T> T read​(java.lang.String path,
                          java.lang.Class<T> type,
                          Predicate... filters)
        Description copied from interface: ReadContext
        Reads the given path from this context
        Specified by:
        read in interface ReadContext
        Parameters:
        path - path to read
        type - expected return type (will try to map)
        filters - filters
        Returns:
        result
      • read

        public <T> T read​(JsonPath path)
        Description copied from interface: ReadContext
        Reads the given path from this context
        Specified by:
        read in interface ReadContext
        Parameters:
        path - path to apply
        Returns:
        result
      • read

        public <T> T read​(JsonPath path,
                          java.lang.Class<T> type)
        Description copied from interface: ReadContext
        Reads the given path from this context
        Specified by:
        read in interface ReadContext
        Parameters:
        path - path to apply
        type - expected return type (will try to map)
        Returns:
        result
      • read

        public <T> T read​(JsonPath path,
                          TypeRef<T> type)
        Description copied from interface: ReadContext
        Reads the given path from this context Sample code to create a TypeRef TypeRef ref = new TypeRef>() {};
        Specified by:
        read in interface ReadContext
        Parameters:
        path - path to apply
        type - expected return type (will try to map)
        Returns:
        result
      • read

        public <T> T read​(java.lang.String path,
                          TypeRef<T> type)
        Description copied from interface: ReadContext
        Reads the given path from this context Sample code to create a TypeRef TypeRef ref = new TypeRef>() {};
        Specified by:
        read in interface ReadContext
        Parameters:
        path - path to apply
        type - expected return type (will try to map)
        Returns:
        result
      • limit

        public ReadContext limit​(int maxResults)
        Description copied from interface: ReadContext
        Stops evaluation when maxResults limit has been reached
        Specified by:
        limit in interface ReadContext
        Returns:
        the read context
      • set

        public DocumentContext set​(java.lang.String path,
                                   java.lang.Object newValue,
                                   Predicate... filters)
        Description copied from interface: WriteContext
        Set the value a the given path
        Specified by:
        set in interface WriteContext
        Parameters:
        path - path to set
        newValue - new value
        filters - filters
        Returns:
        a document context
      • set

        public DocumentContext set​(JsonPath path,
                                   java.lang.Object newValue)
        Description copied from interface: WriteContext
        Set the value a the given path
        Specified by:
        set in interface WriteContext
        Parameters:
        path - path to set
        newValue - new value
        Returns:
        a document context
      • map

        public DocumentContext map​(java.lang.String path,
                                   MapFunction mapFunction,
                                   Predicate... filters)
        Description copied from interface: WriteContext
        Replaces the value on the given path with the result of the MapFunction.
        Specified by:
        map in interface WriteContext
        Parameters:
        path - path to be converted set
        mapFunction - Converter object to be invoked
        filters - filters
        Returns:
        a document context
      • map

        public DocumentContext map​(JsonPath path,
                                   MapFunction mapFunction)
        Description copied from interface: WriteContext
        Replaces the value on the given path with the result of the MapFunction.
        Specified by:
        map in interface WriteContext
        Parameters:
        path - path to be converted set
        mapFunction - Converter object to be invoked (or lambda:))
        Returns:
        a document context
      • delete

        public DocumentContext delete​(java.lang.String path,
                                      Predicate... filters)
        Description copied from interface: WriteContext
        Deletes the given path
        Specified by:
        delete in interface WriteContext
        Parameters:
        path - path to delete
        filters - filters
        Returns:
        a document context
      • add

        public DocumentContext add​(java.lang.String path,
                                   java.lang.Object value,
                                   Predicate... filters)
        Description copied from interface: WriteContext
        Add value to array
         
         List array = new ArrayList(){{
              add(0);
              add(1);
         }};
        
         JsonPath.parse(array).add("$", 2);
        
         assertThat(array).containsExactly(0,1,2);
         
         
        Specified by:
        add in interface WriteContext
        Parameters:
        path - path to array
        value - value to add
        filters - filters
        Returns:
        a document context
      • add

        public DocumentContext add​(JsonPath path,
                                   java.lang.Object value)
        Description copied from interface: WriteContext
        Add value to array at the given path
        Specified by:
        add in interface WriteContext
        Parameters:
        path - path to array
        value - value to add
        Returns:
        a document context
      • put

        public DocumentContext put​(java.lang.String path,
                                   java.lang.String key,
                                   java.lang.Object value,
                                   Predicate... filters)
        Description copied from interface: WriteContext
        Add or update the key with a the given value at the given path
        Specified by:
        put in interface WriteContext
        Parameters:
        path - path to object
        key - key to add
        value - value of key
        filters - filters
        Returns:
        a document context
      • renameKey

        public DocumentContext renameKey​(java.lang.String path,
                                         java.lang.String oldKeyName,
                                         java.lang.String newKeyName,
                                         Predicate... filters)
        Description copied from interface: WriteContext
        Renames the last key element of a given path.
        Specified by:
        renameKey in interface WriteContext
        Parameters:
        path - The path to the old key. Should be resolved to a map or an array including map items.
        oldKeyName - The old key name.
        newKeyName - The new key name.
        filters - filters.
        Returns:
        a document content.
      • renameKey

        public DocumentContext renameKey​(JsonPath path,
                                         java.lang.String oldKeyName,
                                         java.lang.String newKeyName)
        Description copied from interface: WriteContext
        Renames the last key element of a given path.
        Specified by:
        renameKey in interface WriteContext
        Parameters:
        path - The path to the old key. Should be resolved to a map or an array including map items.
        oldKeyName - The old key name.
        newKeyName - The new key name.
        Returns:
        a document content.
      • put

        public DocumentContext put​(JsonPath path,
                                   java.lang.String key,
                                   java.lang.Object value)
        Description copied from interface: WriteContext
        Add or update the key with a the given value at the given path
        Specified by:
        put in interface WriteContext
        Parameters:
        path - path to array
        key - key to add
        value - value of key
        Returns:
        a document context