JsonLever is an utility packaged with JsonMystique to easily operate on jsons
JsonLever makes operating with Jsons easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.
JsonLever's methods are modular and are wonderful for:
- Iterating json arrays, json objects, & json primitives (string, number, boolean etc)
- Manipulating jsons, retrieving and updating values
- Cloning Jsons
- Retrieving subset of a json or selecting specific fields from a json
Below are the steps to configure json lever to get it booted up using native library or the starter.
Although you could just copy gson-utils jars (along with its dependencies), we generally recommend that you use a build tool that supports dependency management (such as Maven or Gradle).
Add the gson-utils jar to your classpath. If you are using maven for dependency management, add the below json-mystique dependency to your pom file
<dependency>
<groupId>com.balajeetm.mystique</groupId>
<artifactId>gson-utils</artifactId>
<version>2.x.x</version>
</dependency>
NOTE : Adding "json-msytique" as the dependency, transitively adds "gson-utils" to the classpath and thus all utilities of "gson-utils" are available out of the box.
"json-msytique" is thus the only uber dependency needed to enjoy all the utilities/capabilities of JsonMystique.
Next, get the instance of JsonLever to play around with
JsonLever jsonLever = JsonLever.getInstance()
Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Json Mystique configurations via the json mystique starter
If you are using maven for dependency management, add the below json-mystique-starter dependency to your pom file
<dependency>
<groupId>com.balajeetm.mystique</groupId>
<artifactId>json-mystique-starter</artifactId>
<version>2.x.x</version>
</dependency>
That's it, you are good to go!
All beans are autoconfigured.
Even Jackson is autoconfigured to serialise/deserialise Gson Json Objects automatically, iff, Jackson is found to be available in the classpath
Grab hold of the JsonLever as below:
JsonLever jsonLever = JsonLever.getInstance();
If you are using the json-mystique-starter
, you can just autowire JsonLever as below:
@Autowired JsonLever jsonLever;
NOTE
JsonLever is lazily loaded. Until thegetInstance
is invoked, its object is not instantiated.
JsonLever exposes lodash like utilities to operate on a json.
Gets the value at path of json. If the resolved value is undefined, JsonNull is returned in its place.
Example
json = { "a": [{ "b": { "c": 3 } }] };
path = "a.0.b.c"
jsonLever.get(json, path);
// => 3
path = ["a", 0, "b", "c"]
jsonLever.get(json, path);
// => 3
path = "a.b.c"
jsonLever.get(json, path, new JsonPrimitive("default");
// => 'default'
This is a functionally overloaded method, with abilities to provide the json source and path in different formats
public JsonElement get(JsonElement source, String jpath)
public JsonElement get(Object source, String jpath)
public JsonElement get(JsonElement source, JsonArray jpath)
public JsonElement get(Object source, JsonArray jpath)
public JsonElement get(JsonElement source, Object... jpath)
public JsonElement get(Object source, Object... jpath)
public JsonElement get(JsonElement source, String jpath, JsonElement defaultValue)
public JsonElement get(Object source, String jpath, JsonElement defaultValue)
public JsonElement get(JsonElement source, JsonArray jpath, JsonElement defaultValue)
public JsonElement get(Object source, JsonArray jpath, JsonElement defaultValue)
Since - 2.1.0
Arguments
- source
The json source. It can be one of the below- JsonElement
Gson's JsonElement (JsonObject, JsonArray, JsonPrimitive or JsonNull) - Object
Can be any Java POJO, that can be jsonified, or a String representing a valid json
- JsonElement
- jpath
Defines the fully qualified json path to the field required. It can be one of the below:- String
'.' separated string defining the fully qualified json path.
json = { "a": [{ "b": { "c": 3 } }] }; path = "a.0.b.c" jsonLever.get(json, path); // => 3
- JsonArray
JsonArray representing the fully qualified json path. Array indexes are represented as numerals.
Only strings and numerals are allowed in the json array. String represent a json object"s fieldname and numerals represent array indexes.
json = { "a": [{ "b": { "c": 3 } }] }; path = ["a", 0, "b", "c"] jsonLever.get(json, path); // => 3
- Object[] - Object Array
Object Array representing the fully qualified json path
Only strings and numerals are allowed in the json array. String represents a json object"s fieldname and numerals represent array indexes.
The object can also be JsonPrimitives representing strings and numerals.
json = { "a": [{ "b": { "c": 3 } }] }; path = ["a", 0, "b", "c"] jsonLever.get(json, path); // => 3
- String
- defaultValue
Represents the value defined for undefined resolved values. It can be one of the below:- JsonElement
Gson's JsonElement (JsonObject, JsonArray, JsonPrimitive or JsonNull)
- JsonElement
Returns
Returns the resolved value as:
- JsonElement
Gson's JsonElement (JsonObject, JsonArray, JsonPrimitive or JsonNull)
Sets the value at path of object. If a portion of path doesn't exist, it's created. Arrays are created for missing index properties while objects are created for all other missing properties.
Example
json = { "a": [{ "b": { "c": 3 } }] };
jsonLever.set(json, "a.0.b.c", 4);
System.out.println(json)
// => { "a": [{ "b": { "c": 4 } }] };
jsonLever.set(json, ["x", 1, "y", "z"], 5);
System.out.println(json)
// => { "a": [{ "b": { "c": 4 } }], "x": [null, { "y": { "z": 5 } }] };
This is a functionally overloaded method, with abilities to provide the json source and path in different formats
public JsonElement set(JsonElement source, String jpath, JsonElement value)
public JsonElement set(JsonElement source, JsonArray jpath, JsonElement value)
Since - 2.1.0
Arguments
- source
The json source. It can be one of the below- JsonElement
Gson's JsonElement (JsonObject, JsonArray, JsonPrimitive or JsonNull)
- JsonElement
- jpath
Defines the fully qualified json path to the field required. It can be one of the below:- String
'.' separated string defining the fully qualified json path.
json = { "a": [{ "b": { "c": 3 } }] }; path = "a.0.b.c" jsonLever.set(json, path, new JsonPrimitive("4")); System.out.println(json) // => { "a": [{ "b": { "c": "4" } }] }
- JsonArray
JsonArray representing the fully qualified json path. Array indexes are represented as numerals.
Only strings and numerals are allowed in the json array. String represent a json object's fieldname and numerals represent array indexes.
json = { "a": [{ "b": { "c": 3 } }] }; path = ["x", 1, "y", "z"] jsonLever.set(json, path, new JsonPrimitive("5")); // => { "a": [{ "b": { "c": 3 } }], "x": [null, {"y": "z"}] }
- String
- value
The value to set. It can be one of the below:- JsonElement
Gson's JsonElement (JsonObject, JsonArray, JsonPrimitive or JsonNull)
- JsonElement
Returns
Returns the source json with value set:
- JsonElement
Gson's JsonElement (JsonObject, JsonArray, JsonPrimitive or JsonNull)
Gets the value at path of json as the '*'
typed JsonElement. If the resolved value cannot be typed to the required type, null
is returned.
Since - 2.1.0
Arguments
- source
The json source. It can be one of the below- JsonElement
Gson's JsonElement (JsonObject, JsonArray, JsonPrimitive or JsonNull) - Object
Can be any Java POJO, that can be jsonified, or a String representing a valid json
- JsonElement
- jpath
Defines the fully qualified json path to the field required. It can be one of the below:- String
'.' separated string defining the fully qualified json path.
json = { "a": [{ "b": { "c": 3 } }] }; path = "a.0.b.c" jsonLever.getJsonPrimitive(json, path); // => 3
- JsonArray
JsonArray representing the fully qualified json path. Array indexes are represented as numerals.
Only strings and numerals are allowed in the json array. String represent a json object"s fieldname and numerals represent array indexes.
json = { "a": [{ "b": { "c": 3 } }] }; path = ["a", 0, "b", "c"] jsonLever.getLong(json, path); // => 3
- Object[] - Object Array
Object Array representing the fully qualified json path
Only strings and numerals are allowed in the json array. String represents a json object"s fieldname and numerals represent array indexes.
The object can also be JsonPrimitives representing strings and numerals.
json = { "a": [{ "b": { "c": "3" } }] }; path = ["a", 0, "b", "c"] jsonLever.getString(json, path); // => 3
- String
Returns
Returns the resolved value as:
- JsonElement
Gson's JsonElement (JsonObject, JsonArray, JsonPrimitive or JsonNull)
There are multiple get*
utilities are below
-
public JsonObject getJsonObject(JsonElement source, String jpath)
-
public JsonObject getJsonObject(Object source, String jpath)
-
public JsonObject getJsonObject(JsonElement source, JsonArray jpath)
-
public JsonObject getJsonObject(Object source, JsonArray jpath)
-
public JsonObject getJsonObject(JsonElement source, Object... jpath)
-
public JsonObject getJsonObject(Object source, Object... jpath)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; path = "a.0"; jsonLever.getJsonObject(json, path); // { "b": { "c": 3 } } path = ["a", 0]; jsonLever.getJsonObject(json, path); // { "b": { "c": 3 } } jsonLever.getJsonObject(json, "a", 0); // { "b": { "c": 3 } } path = "a.0.b" jsonLever.getJsonObject(json, path); // => { "c": 3 } path = "a.b" jsonLever.getJsonObject(json, path); // => null path = "a.0.b.c" jsonLever.getJsonObject(json, path); // => null
-
public JsonArray getJsonArray(JsonElement source, String jpath)
-
public JsonArray getJsonArray(Object source, String jpath)
-
public JsonArray getJsonArray(JsonElement source, JsonArray jpath)
-
public JsonArray getJsonArray(Object source, JsonArray jpath)
-
public JsonArray getJsonArray(JsonElement source, Object... jpath)
-
public JsonArray getJsonArray(Object source, Object... jpath)
Examplejson = { "a": { "a1": [{ "b": { "c": 3 } }]} }; path = "a.a1"; jsonLever.getJsonArray(json, path); // [{ "b": { "c": 3 } }] path = ["a", "a1"]; jsonLever.getJsonArray(json, path); // [{ "b": { "c": 3 } }] jsonLever.getJsonArray(json, "a", "a1"); // [{ "b": { "c": 3 } }] path = "a.0.b" jsonLever.getJsonArray(json, path); // => null path = "a.b" jsonLever.getJsonArray(json, path); // => null path = "a.0.b.c" jsonLever.getJsonArray(json, path); // => null
-
public String getString(JsonElement source, String jpath)
-
public String getString(Object source, String jpath)
-
public String getString(JsonElement source, JsonArray jpath)
-
public String getString(Object source, JsonArray jpath)
-
public String getString(JsonElement source, Object... jpath)
-
public String getString(Object source, Object... jpath)
Examplejson = { "a": [{ "b": { "c": "3" } }] }; path = "a.0"; jsonLever.getString(json, path); // null path = ["a", 0]; jsonLever.getString(json, path); // null jsonLever.getString(json, "a", 0); // null path = "a.0.b.c" jsonLever.getString(json, path); // => "3" path = "a.b" jsonLever.getString(json, path); // => null path = ["a", 0, "b", "c"] jsonLever.getString(json, path); // => "3"
-
public String getString(JsonElement source, String jpath)
-
public String getString(Object source, String jpath)
-
public String getString(JsonElement source, JsonArray jpath)
-
public String getString(Object source, JsonArray jpath)
-
public String getString(JsonElement source, Object... jpath)
-
public String getString(Object source, Object... jpath)
Examplejson = { "a": [{ "b": { "c": "3" } }] }; path = "a.0"; jsonLever.getString(json, path); // null path = ["a", 0]; jsonLever.getString(json, path); // null jsonLever.getString(json, "a", 0); // null path = "a.0.b.c" jsonLever.getString(json, path); // => "3" path = "a.b" jsonLever.getString(json, path); // => null path = ["a", 0, "b", "c"] jsonLever.getString(json, path); // => "3"
-
public Long getLong(JsonElement source, String jpath)
-
public Long getLong(Object source, String jpath)
-
public Long getLong(JsonElement source, JsonArray jpath)
-
public Long getLong(Object source, JsonArray jpath)
-
public Long getLong(JsonElement source, Object... jpath)
-
public Long getLong(Object source, Object... jpath)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; path = "a.0"; jsonLever.getLong(json, path); // null path = ["a", 0]; jsonLever.getLong(json, path); // null jsonLever.getLong(json, "a", 0); // null path = "a.0.b.c" jsonLever.getLong(json, path); // => 3 path = "a.b" jsonLever.getLong(json, path); // => null path = ["a", 0, "b", "c"] jsonLever.getLong(json, path); // => 3
-
public Boolean getBoolean(JsonElement source, String jpath)
-
public Boolean getBoolean(Object source, String jpath)
-
public Boolean getBoolean(JsonElement source, JsonArray jpath)
-
public Boolean getBoolean(Object source, JsonArray jpath)
-
public Boolean getBoolean(JsonElement source, Object... jpath)
-
public Boolean getBoolean(Object source, Object... jpath)
Examplejson = { "a": [{ "b": { "c": true } }] }; path = "a.0"; jsonLever.getBoolean(json, path); // null path = ["a", 0]; jsonLever.getBoolean(json, path); // null jsonLever.getBoolean(json, "a", 0); // null path = "a.0.b.c" jsonLever.getBoolean(json, path); // => true path = "a.b" jsonLever.getBoolean(json, path); // => null path = ["a", 0, "b", "c"] jsonLever.getBoolean(json, path); // => true
-
public JsonPrimitive getJsonPrimitive(JsonElement source, String jpath)
-
public JsonPrimitive getJsonPrimitive(Object source, String jpath)
-
public JsonPrimitive getJsonPrimitive(JsonElement source, JsonArray jpath)
-
public JsonPrimitive getJsonPrimitive(Object source, JsonArray jpath)
-
public JsonPrimitive getJsonPrimitive(JsonElement source, Object... jpath)
-
public JsonPrimitive getJsonPrimitive(Object source, Object... jpath)
Examplejson = { "a": [{ "b": { "c": true } }] }; path = "a.0"; jsonLever.getJsonPrimitive(json, path); // null path = ["a", 0]; jsonLever.getJsonPrimitive(json, path); // null jsonLever.getJsonPrimitive(json, "a", 0); // null path = "a.0.b.c" jsonLever.getJsonPrimitive(json, path); // => true path = "a.b" jsonLever.getJsonPrimitive(json, path); // => null json = { "a": [{ "b": { "c": 1 } }] }; path = ["a", 0, "b", "c"] jsonLever.getJsonPrimitive(json, path); // => 1
Checks if value is classified as the '*'
category
Since - 2.1.0
Arguments
- json
The json source. It can be one of the below- JsonElement
Gson's JsonElement (JsonObject, JsonArray, JsonPrimitive or JsonNull)
- JsonElement
Returns
Returns true
is the value is classified as the category, else false
Example
json = { "a": [{ "b": { "c": 3 } }] };
jsonLever.isArray(json);
// false
jsonLever.isObject(json);
// => true
There are multiple is* utilities are below
-
public Boolean isNull(JsonElement source)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.isNull(json); // false json = null jsonLever.isNull(json); // => true json = JsonNull.INSTANCE jsonLever.isNull(json); // => true
-
public Boolean isNotNull(JsonElement source)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.isNotNull(json); // true json = null jsonLever.isNotNull(json); // => false json = JsonNull.INSTANCE jsonLever.isNotNull(json); // => false
-
public Boolean isArray(JsonElement source)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.isArray(json); // false json = ["a", "b", 1, true, {"x": "z"}] jsonLever.isArray(json); // => true json = JsonNull.INSTANCE jsonLever.isArray(json); // => true
-
public Boolean isObject(JsonElement source)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.isObject(json); // true json = ["a", "b", 1, true, {"x": "z"}] jsonLever.isObject(json); // => false json = JsonNull.INSTANCE jsonLever.isObject(json); // => false
-
public Boolean isPrimitive(JsonElement source)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.isPrimitive(json); // false json = ["a", "b", 1, true, {"x": "z"}] jsonLever.isPrimitive(json); // => false json = JsonNull.INSTANCE jsonLever.isPrimitive(json); // => false json = new JsonPrimitive("test") jsonLever.isPrimitive(json); // => true json = new JsonPrimitive(1) jsonLever.isPrimitive(json); // => true
-
public Boolean isNumber(JsonElement source)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.isNumber(json); // false json = ["a", "b", 1, true, {"x": "z"}] jsonLever.isNumber(json); // => false json = JsonNull.INSTANCE jsonLever.isNumber(json); // => false json = new JsonPrimitive("test") jsonLever.isNumber(json); // => false json = new JsonPrimitive(1) jsonLever.isNumber(json); // => true
-
public Boolean isString(JsonElement source)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.isString(json); // false json = ["a", "b", 1, true, {"x": "z"}] jsonLever.isString(json); // => false json = JsonNull.INSTANCE jsonLever.isString(json); // => false json = new JsonPrimitive("test") jsonLever.isString(json); // => true json = new JsonPrimitive(1) jsonLever.isString(json); // => false
-
public Boolean isBoolean(JsonElement source)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.isBoolean(json); // false json = ["a", "b", 1, true, {"x": "z"}] jsonLever.isBoolean(json); // => false json = JsonNull.INSTANCE jsonLever.isBoolean(json); // => false json = new JsonPrimitive("test") jsonLever.isBoolean(json); // => false json = new JsonPrimitive(true) jsonLever.isBoolean(json); // => true
Returns the json as the required json type if the type matches, else return the defaultValue if configured, else return null
Since - 2.1.0
Arguments
-
json
The json source. It can be one of the below- JsonElement
Gson's JsonElement (JsonObject, JsonArray, JsonPrimitive or JsonNull)
- JsonElement
-
defaultValue
The defaultValue in- JsonElement
Gson's JsonElement (JsonObject, JsonArray, JsonPrimitive or JsonNull)
- JsonElement
Returns
Returns the json in the required type else returns null
Example
JsonElement json = { "a": [{ "b": { "c": 3 } }] };
jsonLever.asJsonPrimitive(json, new JsonPrimitive("default"));
// "default"
There are multiple as* utilities are below
-
public JsonPrimitive asJsonPrimitive(JsonElement source)
-
public JsonPrimitive asJsonPrimitive(JsonElement source, JsonPrimitive defaultValue)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.asJsonPrimitive(json); // null json = ["a", "b", 1, true, {"x": "z"}] jsonLever.asJsonPrimitive(json); // => null json = JsonNull.INSTANCE jsonLever.asJsonPrimitive(json, new JsonPrimitive("default"); // => "default" json = new JsonPrimitive("test") jsonLever.asJsonPrimitive(json); // => "test" json = new JsonPrimitive(1) jsonLever.asJsonPrimitive(json); // => 1
-
public JsonObject asJsonObject(JsonElement element)
-
public JsonObject asJsonObject(JsonElement source, JsonObject defaultJson)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.asJsonObject(json); // { "a": [{ "b": { "c": 3 } }] } json = ["a", "b", 1, true, {"x": "z"}] jsonLever.asJsonObject(json); // => null json = JsonNull.INSTANCE jsonLever.asJsonObject(json, new JsonObject()); // => {} json = new JsonPrimitive("test") jsonLever.asJsonObject(json); // => null json = new JsonPrimitive(1) jsonLever.asJsonObject(json, new JsonObject()); // => {}
-
public JsonArray asJsonArray(JsonElement source)
-
public JsonArray asJsonArray(JsonElement source, JsonArray defaultArray)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.asJsonArray(json); // null json = ["a", "b", 1, true, {"x": "z"}] jsonLever.asJsonArray(json); // => ["a", "b", 1, true, {"x": "z"}] json = JsonNull.INSTANCE jsonLever.asJsonArray(json); // => null json = new JsonPrimitive("test", new JsonArray()) jsonLever.asJsonArray(json); // => [] json = new JsonPrimitive(1) jsonLever.asJsonArray(json, new JsonArray()); // => []
-
public JsonElement asJsonElement(JsonElement source, JsonElement defaultJson)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.asJsonElement(json, JsonNull.INSTANCE); // { "a": [{ "b": { "c": 3 } }] } json = ["a", "b", 1, true, {"x": "z"}] jsonLever.asJsonElement(json, JsonNull.INSTANCE); // => ["a", "b", 1, true, {"x": "z"}] json = JsonNull.INSTANCE jsonLever.asJsonElement(json, new JsonPrimitive("default")); // => "default" json = new JsonPrimitive("test", new JsonArray()) jsonLever.asJsonElement(json); // => "test" json = new JsonPrimitive(1) jsonLever.asJsonElement(json, new JsonArray()); // => 1
-
public String asString(JsonElement source)
-
public String asString(JsonElement source, String defaultStr)
Examplejson = { "a": [{ "b": { "c": 3 } }] }; jsonLever.asString(json); // null json = ["a", "b", 1, true, {"x": "z"}] jsonLever.asString(json, "default"); // => "default" json = JsonNull.INSTANCE jsonLever.asString(json, "default"); // => "default" json = new JsonPrimitive("test") jsonLever.asString(json); // => "test" json = new JsonPrimitive(1) jsonLever.asString(json); // => null
-
public Boolean asBoolean(JsonElement source)
-
public Boolean asBoolean(JsonElement source, Boolean defaultBool)
Example ```java json = { "a": [{ "b": { "c": 3 } }] };
jsonLever.asBoolean(json);
// null
json = ["a", "b", 1, true, {"x": "z"}]
jsonLever.asBoolean(json, Boolean.TRUE);
// => true
json = JsonNull.INSTANCE
jsonLever.asBoolean(json);
// => null
json = new JsonPrimitive("test")
jsonLever.asBoolean(json, Boolean.FALSE);
// => false
json = new JsonPrimitive(Boolean.TRUE)
jsonLever.asBoolean(json);
// => true
```
public Long asLong(JsonElement source)
public Long asLong(JsonElement source, Long defaultLong)
Example ```java json = { "a": [{ "b": { "c": 3 } }] };
jsonLever.asLong(json);
// null
json = ["a", "b", 1, true, {"x": "z"}]
jsonLever.asLong(json, 1l);
// => 1
json = JsonNull.INSTANCE
jsonLever.asLong(json);
// => null
json = new JsonPrimitive("test")
jsonLever.asLong(json, 2l);
// => 2
json = new JsonPrimitive(3)
jsonLever.asLong(json);
// => 3
```
public Integer asInt(JsonElement source)
public Integer asInt(JsonElement source, Integer defaultInt)
Example ```java json = { "a": [{ "b": { "c": 3 } }] };
jsonLever.asLong(json);
// null
json = ["a", "b", 1, true, {"x": "z"}]
jsonLever.asLong(json, 1);
// => 1
json = JsonNull.INSTANCE
jsonLever.asLong(json);
// => null
json = new JsonPrimitive("test")
jsonLever.asLong(json, 2);
// => 2
json = new JsonPrimitive(3)
jsonLever.asLong(json);
// => 3
```
There many other utility functions exposed by JsonLever. Let's look at them one by one.
Since - 2.1.0
-
public static JsonLever getInstance()
We have seen this before; this is the way to grab the Singleton Instance of JsonLeverJsonLever jsonLever = JsonLever.getInstance();
-
public JsonParser getJsonParser()
JsonLever uses Gson's JsonParser object internally, to parse jsons.
The utility method, let's us grab hold of the JsonParser instance in use.JsonLever jsonLever = JsonLever.getInstance(); JsonParser parser = jsonLever.getJsonParser();
-
public JsonParser getGson()
JsonLever uses Gson's Gson object internally, to serialize and deserialize POJOs.
The utility method, let's us grab hold of the Gson instance in use.JsonLever jsonLever = JsonLever.getInstance(); Gson gson = jsonLever.getGson();
-
public JsonElement getFirst(Iterable<JsonElement> jsonList)
A null-safe way of retrieving the first json element from an Iterables of JsonElement, if available. Else returns nullArguments
- jsonList
The json list. It can be one of the below- JsonArray
Gson's JsonArray representing an array of JsonElements - Iterable Any java iterable of JsonElement
- JsonArray
Returns
Returns the first json element if available else returnsnull
JsonLever jsonLever = JsonLever.getInstance(); json = [1, "2"] jsonLever.getFirst(json) // 1 json = null jsonLever.getFirst(json) // null
- jsonList
-
public JsonArray getJpath(String jpath)
-
public JsonArray getJpath(JsonElement jpath)
Gets the jpath for a '.' separated string defining the fully qualified path of a field in Json.
Array Indexes are referred via numbers. If the input is anything apart from String, JsonPrimitive String or JsonArray, it returns null ExampleJsonLever jsonLever = JsonLever.getInstance(); path = "a.b.0.4.c"; jsonLever.getJpath(path); // ["a", "b", 0, 4, "c"] path = {'a': 'b'}; jsonLever.getJpath(path); // null
There many other utility functions exposed by JsonLever. Let's look at them one by one.
Since - 2.1.0
-
public JsonArray newJsonArray(Object... path)
Returns a new Json Array for the series of inputs. Only primitive inputs and jsons are allowed. ExampleJsonLever jsonLever = JsonLever.getInstance(); jsonLever.newJsonArray("a", "1", 2, {'a': 'b'}); // ["a", "1", 2, {'a': 'b'}]
-
public String toString(JsonElement source)
Null safe toString method for a json element. ExampleJsonLever jsonLever = JsonLever.getInstance(); jsonLever.toString({'a': 'b'}); // {'a': 'b'} jsonLever.toString(null); // null
-
public JsonElement deepClone(JsonElement source)
-
public JsonObject deepClone(JsonObject source)
-
public JsonArray deepClone(JsonArray source)
-
public JsonPrimitive deepClone(JsonPrimitive source)
Null safe, clone operation on JsonElements. Can deep clone the entire json and its hierarchy.
Is a comparatively heavy process ExampleJsonLever jsonLever = JsonLever.getInstance(); jsonLever.deepClone({'a': 'b'}); // {'a': 'b'} jsonLever.deepClone(null); // null
-
public JsonElement subset(JsonElement source, JsonArray jPathArray)
Returns the subset of the input for all the paths specified in the jPathArray.Example
JsonLever jsonLever = JsonLever.getInstance(); json = {'a': {'a1': 1, 'a2': 2}, 'b': {'b1': 1, 'b2': 2}, 'c': [1, 2, 3]} jsonLever.subset(json, ['a.a1', 'b.b2', 'c.1']); // {'a': {'a1': 1}, 'b': {'b2': 2}, 'c': [null, 2]}
-
public JsonObject simpleMerge(JsonObject to, JsonObject from)
public JsonObject simpleMerge(JsonObject to, JsonObject... from)
Merges the first level fields on two jsonsExample
JsonLever jsonLever = JsonLever.getInstance(); a = {'a': {'a1': 1}} b = {'b': {'b2': 2}} jsonLever.simpleMerge(a, b); // {'a': {'a1': 1}, 'b': {'b2': 2}}
-
public JsonElement merge(JsonElement source1, JsonElement source2)
-
public JsonElement merge(JsonElement source1, JsonElement source2, Boolean mergeArray)
Recursive merge of two json objects.Example
JsonLever jsonLever = JsonLever.getInstance(); a = {'a': {'a1': {'a2': 'a3'}}} b = {'a': {'a1': {'a4': 'a5'}}} jsonLever.merge(a, b); // {'a': {'a1': {'a2': 'a3', 'a4': 'a5'}}}
Hop over here for the documentation index