Class JsonContext

java.lang.Object
com.jayway.jsonpath.internal.JsonContext
All Implemented Interfaces:
DocumentContext, ParseContext, ReadContext, WriteContext

public class JsonContext extends Object implements ParseContext, DocumentContext
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
    • configuration

      private final Configuration configuration
    • json

      private Object json
  • Constructor Details

    • JsonContext

      public JsonContext()
    • JsonContext

      public JsonContext(Configuration configuration)
    • JsonContext

      private JsonContext(Object json, Configuration configuration)
  • Method Details

    • parse

      public DocumentContext parse(Object json)
      Specified by:
      parse in interface ParseContext
    • parse

      public DocumentContext parse(String json)
      Specified by:
      parse in interface ParseContext
    • parse

      public DocumentContext parse(InputStream json)
      Specified by:
      parse in interface ParseContext
    • parse

      public DocumentContext parse(InputStream json, String charset)
      Specified by:
      parse in interface ParseContext
    • parse

      public DocumentContext parse(File json) throws IOException
      Specified by:
      parse in interface ParseContext
      Throws:
      IOException
    • configuration

      public Configuration configuration()
      Description copied from interface: ReadContext
      Returns the configuration used for reading
      Specified by:
      configuration in interface ReadContext
      Specified by:
      configuration in interface WriteContext
      Returns:
      an immutable configuration
    • json

      public 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 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(String path, Predicate... filters)
      Description copied from interface: ReadContext
      Reads the given path from this context
      Specified by:
      read in interface ReadContext
      Type Parameters:
      T -
      Parameters:
      path - path to read
      filters - filters
      Returns:
      result
    • read

      public <T> T read(String path, Class<T> type, Predicate... filters)
      Description copied from interface: ReadContext
      Reads the given path from this context
      Specified by:
      read in interface ReadContext
      Type Parameters:
      T -
      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
      Type Parameters:
      T -
      Parameters:
      path - path to apply
      Returns:
      result
    • read

      public <T> T read(JsonPath path, Class<T> type)
      Description copied from interface: ReadContext
      Reads the given path from this context
      Specified by:
      read in interface ReadContext
      Type Parameters:
      T -
      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 TypeRefinvalid input: '<'List>() {};
      Specified by:
      read in interface ReadContext
      Type Parameters:
      T -
      Parameters:
      path - path to apply
      type - expected return type (will try to map)
      Returns:
      result
    • read

      public <T> T read(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 TypeRefinvalid input: '<'List>() {};
      Specified by:
      read in interface ReadContext
      Type Parameters:
      T -
      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
      Parameters:
      maxResults -
      Returns:
      the read context
    • withListeners

      public ReadContext withListeners(EvaluationListener... listener)
      Description copied from interface: ReadContext
      Adds listener to the evaluation of this path
      Specified by:
      withListeners in interface ReadContext
      Parameters:
      listener - listeners to add
      Returns:
      the read context
    • convert

      private <T> T convert(Object obj, Class<T> targetType, Configuration configuration)
    • convert

      private <T> T convert(Object obj, TypeRef<T> targetType, Configuration configuration)
    • set

      public DocumentContext set(String path, 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, 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(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(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
    • delete

      public DocumentContext delete(JsonPath path)
      Description copied from interface: WriteContext
      Deletes the given path
      Specified by:
      delete in interface WriteContext
      Parameters:
      path - path to delete
      Returns:
      a document context
    • add

      public DocumentContext add(String path, 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, 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(String path, String key, 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(String path, String oldKeyName, 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, String oldKeyName, 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, String key, 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