Package com.jayway.jsonpath.internal
Class JsonContext
- java.lang.Object
-
- com.jayway.jsonpath.internal.JsonContext
-
- All Implemented Interfaces:
DocumentContext
,ParseContext
,ReadContext
,WriteContext
public class JsonContext extends java.lang.Object implements ParseContext, DocumentContext
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
JsonContext.LimitingEvaluationListener
-
Field Summary
Fields Modifier and Type Field Description private Configuration
configuration
private java.lang.Object
json
private static org.slf4j.Logger
logger
-
Constructor Summary
Constructors Modifier Constructor Description JsonContext()
JsonContext(Configuration configuration)
private
JsonContext(java.lang.Object json, Configuration configuration)
-
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 pathDocumentContext
add(java.lang.String path, java.lang.Object value, Predicate... filters)
Add value to arrayConfiguration
configuration()
Returns the configuration used for readingprivate <T> T
convert(java.lang.Object obj, TypeRef<T> targetType, Configuration configuration)
private <T> T
convert(java.lang.Object obj, java.lang.Class<T> targetType, Configuration configuration)
DocumentContext
delete(JsonPath path)
Deletes the given pathDocumentContext
delete(java.lang.String path, Predicate... filters)
Deletes the given pathjava.lang.Object
json()
Returns the JSON model that this context is operating onjava.lang.String
jsonString()
Returns the JSON model that this context is operating on as a JSON stringReadContext
limit(int maxResults)
Stops evaluation when maxResults limit has been reachedDocumentContext
map(JsonPath path, MapFunction mapFunction)
Replaces the value on the given path with the result of theMapFunction
.DocumentContext
map(java.lang.String path, MapFunction mapFunction, Predicate... filters)
Replaces the value on the given path with the result of theMapFunction
.DocumentContext
parse(java.io.File json)
DocumentContext
parse(java.io.InputStream json)
DocumentContext
parse(java.io.InputStream json, java.lang.String charset)
DocumentContext
parse(java.lang.Object json)
DocumentContext
parse(java.lang.String json)
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 pathDocumentContext
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 TypeRefTypeRef 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 TypeRefTypeRef ref = new TypeRef
- >() {};
<T> T
read(java.lang.String path, java.lang.Class<T> type, Predicate... filters)
Reads the given path from this contextDocumentContext
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 pathDocumentContext
set(java.lang.String path, java.lang.Object newValue, Predicate... filters)
Set the value a the given pathReadContext
withListeners(EvaluationListener... listener)
Adds listener to the evaluation of this path
-
-
-
Field Detail
-
logger
private static final org.slf4j.Logger logger
-
configuration
private final Configuration configuration
-
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.lang.Object json)
- Specified by:
parse
in interfaceParseContext
-
parse
public DocumentContext parse(java.lang.String json)
- Specified by:
parse
in interfaceParseContext
-
parse
public DocumentContext parse(java.io.InputStream json)
- Specified by:
parse
in interfaceParseContext
-
parse
public DocumentContext parse(java.io.InputStream json, java.lang.String charset)
- Specified by:
parse
in interfaceParseContext
-
parse
public DocumentContext parse(java.io.File json) throws java.io.IOException
- Specified by:
parse
in interfaceParseContext
- Throws:
java.io.IOException
-
configuration
public Configuration configuration()
Description copied from interface:ReadContext
Returns the configuration used for reading- Specified by:
configuration
in interfaceReadContext
- Specified by:
configuration
in interfaceWriteContext
- Returns:
- an immutable configuration
-
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 interfaceReadContext
- Specified by:
json
in interfaceWriteContext
- 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 interfaceReadContext
- Specified by:
jsonString
in interfaceWriteContext
- 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 interfaceReadContext
- Parameters:
path
- path to readfilters
- 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 interfaceReadContext
- Parameters:
path
- path to readtype
- 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 interfaceReadContext
- 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 interfaceReadContext
- Parameters:
path
- path to applytype
- 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 TypeRefTypeRef ref = new TypeRef
- >() {};
- Specified by:
read
in interfaceReadContext
- Parameters:
path
- path to applytype
- 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 TypeRefTypeRef ref = new TypeRef
- >() {};
- Specified by:
read
in interfaceReadContext
- Parameters:
path
- path to applytype
- 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 interfaceReadContext
- 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 interfaceReadContext
- Parameters:
listener
- listeners to add- 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 interfaceWriteContext
- Parameters:
path
- path to setnewValue
- new valuefilters
- 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 interfaceWriteContext
- Parameters:
path
- path to setnewValue
- 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 theMapFunction
.- Specified by:
map
in interfaceWriteContext
- Parameters:
path
- path to be converted setmapFunction
- Converter object to be invokedfilters
- 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 theMapFunction
.- Specified by:
map
in interfaceWriteContext
- Parameters:
path
- path to be converted setmapFunction
- 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 interfaceWriteContext
- Parameters:
path
- path to deletefilters
- filters- Returns:
- a document context
-
delete
public DocumentContext delete(JsonPath path)
Description copied from interface:WriteContext
Deletes the given path- Specified by:
delete
in interfaceWriteContext
- Parameters:
path
- path to delete- 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 arrayList
array = new ArrayList (){{ add(0); add(1); }}; JsonPath.parse(array).add("$", 2); assertThat(array).containsExactly(0,1,2); - Specified by:
add
in interfaceWriteContext
- Parameters:
path
- path to arrayvalue
- value to addfilters
- 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 interfaceWriteContext
- Parameters:
path
- path to arrayvalue
- 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 interfaceWriteContext
- Parameters:
path
- path to objectkey
- key to addvalue
- value of keyfilters
- 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 interfaceWriteContext
- 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 interfaceWriteContext
- 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 interfaceWriteContext
- Parameters:
path
- path to arraykey
- key to addvalue
- value of key- Returns:
- a document context
-
-