Class JsonContext

    • Field Detail

      • logger

        private static final org.slf4j.Logger logger
      • json

        private java.lang.Object json
    • Constructor Detail

      • JsonContext

        public JsonContext()
      • JsonContext

        public JsonContext​(Configuration configuration)
      • JsonContext

        private JsonContext​(java.lang.Object json,
                            Configuration configuration)
    • Method Detail

      • parse

        public DocumentContext parse​(java.io.File json)
                              throws java.io.IOException
        Specified by:
        parse in interface ParseContext
        Throws:
        java.io.IOException
      • 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
      • convert

        private <T> T convert​(java.lang.Object obj,
                              java.lang.Class<T> targetType,
                              Configuration configuration)
      • convert

        private <T> T convert​(java.lang.Object obj,
                              TypeRef<T> targetType,
                              Configuration configuration)
      • 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