com.jayway.jsonpath
Class JsonModel

java.lang.Object
  extended by com.jayway.jsonpath.JsonModel

public class JsonModel
extends Object

A JsonModel holds a parsed JSON document and provides easy read and write operations. In contrast to the static read operations provided by JsonPath a JsonModel will only parse the document once.

Author:
Kalle Stenflo

Nested Class Summary
static interface JsonModel.ArrayOps
          Operations that can be performed on Json arrays (Lists)
static interface JsonModel.ListMappingModelReader
          Converts a JsonModel to an Collection of Objects
static interface JsonModel.MappingModelReader
          Object mapping interface used when for root object that can be either a List or a Map.
static interface JsonModel.ObjectMappingModelReader
          Converts a JsonModel to an Object
static interface JsonModel.ObjectOps
          Operations that can be performed on Json objects (Maps)
 
Method Summary
static JsonModel create(InputStream jsonInputStream)
           
static JsonModel create(Object jsonObject)
           
static JsonModel create(String json)
           
static JsonModel create(URL url)
           
<T> T
get(JsonPath jsonPath)
          Reads the given path from this JsonModel.
<T> T
get(String jsonPath, Filter... filters)
          Reads the given path from this JsonModel.
 Object getJsonObject()
          Returns the root object of this JsonModel
 JsonModel getSubModel(JsonPath jsonPath)
          Returns a sub model from this JsonModel.
 JsonModel getSubModel(String jsonPath)
          Returns a sub model from this JsonModel.
 JsonModel getSubModelDetached(JsonPath jsonPath)
          Creates a detached sub model from this JsonModel.
 JsonModel getSubModelDetached(String jsonPath, Filter... filters)
          Creates a detached sub model from this JsonModel.
 boolean hasPath(JsonPath jsonPath)
          Check if this JsonModel has the given definite path
 boolean hasPath(String jsonPath)
          Check if this JsonModel has the given definite path
 boolean isList()
          Check if this JsonModel is holding a JSON array as to object
 boolean isMap()
          Check if this JsonModel is holding a JSON object as to object
 JsonModel.MappingModelReader map()
          Returns a JsonModel.ObjectMappingModelReader for this JsonModel.
 JsonModel.MappingModelReader map(JsonPath jsonPath)
          Returns a JsonModel.ObjectMappingModelReader for the JsonModel targeted by the provided JsonPath.
 JsonModel.MappingModelReader map(String jsonPath, Filter... filters)
          Returns a JsonModel.ObjectMappingModelReader for the JsonModel targeted by the provided JsonPath.
static JsonModel model(InputStream jsonInputStream)
          Creates a JsonModel
static JsonModel model(Object jsonObject)
          Creates a JsonModel
static JsonModel model(String json)
          Creates a JsonModel
static JsonModel model(URL url)
          Creates a JsonModel
 JsonModel.ArrayOps opsForArray()
          Gets an JsonModel.ArrayOps for this JsonModel.
 JsonModel.ArrayOps opsForArray(JsonPath jsonPath)
          Gets an JsonModel.ArrayOps for the array inside this JsonModel identified by the given JsonPath.
 JsonModel.ArrayOps opsForArray(String jsonPath)
          Gets an JsonModel.ArrayOps for the array inside this JsonModel identified by the given JsonPath.
 JsonModel.ObjectOps opsForObject()
          Gets an JsonModel.ObjectOps for this JsonModel.
 JsonModel.ObjectOps opsForObject(JsonPath jsonPath)
          Gets an JsonModel.ObjectOps for the object inside this JsonModel identified by the given JsonPath.
 JsonModel.ObjectOps opsForObject(String jsonPath)
          Gets an JsonModel.ObjectOps for the object inside this JsonModel identified by the given JsonPath.
 void print()
          Prints this JsonModel to standard out
 String toJson()
          Creates a JSON representation of this JsonModel
 String toJson(boolean prettyPrint)
          Creates a JSON representation of this JsonModel
 String toJson(JsonPath jsonPath)
          Creates a JSON representation of the result of the provided JsonPath
 String toJson(String jsonPath, Filter... filters)
          Creates a JSON representation of the result of the provided JsonPath
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isList

public boolean isList()
Check if this JsonModel is holding a JSON array as to object

Returns:
true if root is an array

isMap

public boolean isMap()
Check if this JsonModel is holding a JSON object as to object

Returns:
true if root is an object

print

public void print()
Prints this JsonModel to standard out


hasPath

public boolean hasPath(String jsonPath)
Check if this JsonModel has the given definite path

Parameters:
jsonPath - path to check
Returns:
true if model contains path
See Also:
JsonPath.isPathDefinite()

hasPath

public boolean hasPath(JsonPath jsonPath)
Check if this JsonModel has the given definite path

Parameters:
jsonPath - path to check
Returns:
true if model contains path
See Also:
JsonPath.isPathDefinite()

getJsonObject

public Object getJsonObject()
Returns the root object of this JsonModel

Returns:
returns the root object

get

public <T> T get(String jsonPath,
                 Filter... filters)
Reads the given path from this JsonModel. Filters is a way to problematically filter the contents of a list. Instead of writing the filter criteria directly inside the JsonPath expression the filter is indicated and provided as an argument.

All three statements below are equivalent

JsonModel model = JsonModel.model(myJson);

//A List books = model.read("$store.book[?(@author == 'Nigel Rees')]");

//B List books = model.read("$store.book[?]", filter(where("author").is("Nigel Rees"));

//C JsonPath path = JsonPath.compile("$store.book[?]", filter(where("author").is("Nigel Rees"));

List books = model.read(path);

The filters are applied in the order they are provided. If a path contains multiple [?] filter markers the filters must be passed in the correct order.

Type Parameters:
T - expected return type
Parameters:
jsonPath - the path to read
filters - filters to use in the path
Returns:
the json path result
See Also:
Filter, Criteria

get

public <T> T get(JsonPath jsonPath)
Reads the given path from this JsonModel.

Type Parameters:
T - expected return type
Parameters:
jsonPath - the path to read
Returns:
the json path result

opsForArray

public JsonModel.ArrayOps opsForArray()
Gets an JsonModel.ArrayOps for this JsonModel. Note that the root element of this model must be a json array.

Returns:
array operations for this JsonModel

opsForArray

public JsonModel.ArrayOps opsForArray(String jsonPath)
Gets an JsonModel.ArrayOps for the array inside this JsonModel identified by the given JsonPath. The path must be definite (JsonPath.isPathDefinite()).

Note that the element returned by the given path must be a json array.

Parameters:
jsonPath - definite path to array to perform operations on
Returns:
array operations for the targeted array

opsForArray

public JsonModel.ArrayOps opsForArray(JsonPath jsonPath)
Gets an JsonModel.ArrayOps for the array inside this JsonModel identified by the given JsonPath. The path must be definite (JsonPath.isPathDefinite()).

Note that the element returned by the given path must be a json array.

Parameters:
jsonPath - definite path to array to perform operations on
Returns:
array operations for the targeted array

opsForObject

public JsonModel.ObjectOps opsForObject()
Gets an JsonModel.ObjectOps for this JsonModel. Note that the root element of this model must be a json object.

Returns:
object operations for this JsonModel

opsForObject

public JsonModel.ObjectOps opsForObject(String jsonPath)
Gets an JsonModel.ObjectOps for the object inside this JsonModel identified by the given JsonPath. The path must be definite (JsonPath.isPathDefinite()).

Note that the element returned by the given path must be a json object.

Parameters:
jsonPath - definite path to object to perform operations on
Returns:
object operations for the targeted object

opsForObject

public JsonModel.ObjectOps opsForObject(JsonPath jsonPath)
Gets an JsonModel.ObjectOps for the object inside this JsonModel identified by the given JsonPath. The path must be definite (JsonPath.isPathDefinite()).

Note that the element returned by the given path must be a json object.

Parameters:
jsonPath - definite path to object to perform operations on
Returns:
object operations for the targeted object

toJson

public String toJson()
Creates a JSON representation of this JsonModel

Returns:
model as Json

toJson

public String toJson(boolean prettyPrint)
Creates a JSON representation of this JsonModel

Parameters:
prettyPrint - if the model should be pretty printed
Returns:

toJson

public String toJson(String jsonPath,
                     Filter... filters)
Creates a JSON representation of the result of the provided JsonPath

Returns:
path result as Json

toJson

public String toJson(JsonPath jsonPath)
Creates a JSON representation of the result of the provided JsonPath

Returns:
path result as Json

getSubModel

public JsonModel getSubModel(String jsonPath)
Returns a sub model from this JsonModel. A sub model can be any JSON object or JSON array addressed by a definite path. In contrast to a detached model changes on the sub model will be applied on the source model (the JsonModel from which the sub model was created)

Parameters:
jsonPath - the absolute path to extract a JsonModel for
Returns:
the new JsonModel
See Also:
JsonPath.isPathDefinite()

getSubModel

public JsonModel getSubModel(JsonPath jsonPath)
Returns a sub model from this JsonModel. A sub model can be any JSON object or JSON array addressed by a definite path. In contrast to a detached model changes on the sub model will be applied on the source model (the JsonModel from which the sub model was created)

Parameters:
jsonPath - the absolute path to extract a JsonModel for
Returns:
the new JsonModel
See Also:
JsonPath.isPathDefinite()

getSubModelDetached

public JsonModel getSubModelDetached(String jsonPath,
                                     Filter... filters)
Creates a detached sub model from this JsonModel. A detached sub model does not have to be created using a definite path. Changes on a detached sub model will not be reflected on the source model (the JsonModel from which the sub model was created).

Parameters:
jsonPath - the absolute path to extract a JsonModel for
filters - filters to expand the path
Returns:
a detached JsonModel

getSubModelDetached

public JsonModel getSubModelDetached(JsonPath jsonPath)
Creates a detached sub model from this JsonModel. A detached sub model does not have to be created using a definite path. Changes on a detached sub model will not be reflected on the source model (the JsonModel from which the sub model was created).

Parameters:
jsonPath - the absolute path to extract a JsonModel for
Returns:
a detached JsonModel

map

public JsonModel.MappingModelReader map()
Returns a JsonModel.ObjectMappingModelReader for this JsonModel. Note that to use this functionality you need an optional dependencies on your classpath (jackson-mapper-asl ver >= 1.9.5)

Returns:
a object mapper

map

public JsonModel.MappingModelReader map(String jsonPath,
                                        Filter... filters)
Returns a JsonModel.ObjectMappingModelReader for the JsonModel targeted by the provided JsonPath. Note that to use this functionality you need an optional dependencies on your classpath (jackson-mapper-asl ver >= 1.9.5)

Returns:
a object mapper

map

public JsonModel.MappingModelReader map(JsonPath jsonPath)
Returns a JsonModel.ObjectMappingModelReader for the JsonModel targeted by the provided JsonPath. Note that to use this functionality you need an optional dependencies on your classpath (jackson-mapper-asl ver >= 1.9.5)

Returns:
a object mapper

model

public static JsonModel model(String json)
Creates a JsonModel

Parameters:
json - json string
Returns:
a new JsonModel

create

public static JsonModel create(String json)

model

public static JsonModel model(Object jsonObject)
Creates a JsonModel

Parameters:
jsonObject - a json container (a Map or a List)
Returns:
a new JsonModel

create

public static JsonModel create(Object jsonObject)

model

public static JsonModel model(URL url)
                       throws IOException
Creates a JsonModel

Parameters:
url - pointing to a Json document
Returns:
a new JsonModel
Throws:
IOException

create

public static JsonModel create(URL url)
                        throws IOException
Throws:
IOException

model

public static JsonModel model(InputStream jsonInputStream)
                       throws IOException
Creates a JsonModel

Parameters:
jsonInputStream - json document stream
Returns:
a new JsonModel
Throws:
IOException

create

public static JsonModel create(InputStream jsonInputStream)
                        throws IOException
Throws:
IOException


Copyright © 2011-2012. All Rights Reserved.