Skip to content

Commit

Permalink
Added possibility to read the value basing on jsonpath
Browse files Browse the repository at this point in the history
fixes #8
  • Loading branch information
marcingrzejszczak committed Apr 14, 2016
1 parent 15c7ac1 commit 5584543
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 9 deletions.
60 changes: 58 additions & 2 deletions README.adoc
@@ -1,4 +1,4 @@
:lib_version: 0.3.0
:lib_version: 0.4.0

image::https://travis-ci.org/marcingrzejszczak/jsonassert.svg?branch=master[Build Status, link=https://travis-ci.org/marcingrzejszczak/jsonassert]
image::https://maven-badges.herokuapp.com/maven-central/com.toomuchcoding.jsonassert/jsonassert/badge.svg?style=plastic[Maven Central, link="https://maven-badges.herokuapp.com/maven-central/com.toomuchcoding.jsonassert/jsonassert"]
Expand Down Expand Up @@ -79,13 +79,69 @@ JsonPath.builder().field("some").field("nested").array("withlist").contains("nam

you will receive `$.some.nested.withlist[*][?(@.name == 'name1')]` String.

=== Retrieving JSON Path value (since 0.4.0)

Wouldn't it be great to retrieve the value from the JSON via the JSON Path? There you go!

[source,groovy]
----
given:
String json = ''' [ {
"some" : {
"nested" : {
"json" : "with value",
"anothervalue": 4,
"withlist" : [
{ "name" :"name1"} ,
{"name": "name2"},
{"anothernested": { "name": "name3"} }
]
}
}
},
{
"someother" : {
"nested" : {
"json" : true,
"anothervalue": 4,
"withlist" : [
{ "name" :"name1"} , {"name": "name2"}
],
"withlist2" : [
"a", "b"
]
}
}
}
]
'''
expect:
com.toomuchcoding.jsonassert.JsonPath.builder(json).array().field("some").field("nested").field("json").read(String) == 'with value'
com.toomuchcoding.jsonassert.JsonPath.builder(json).array().field("some").field("nested").field("anothervalue").read(Integer) == 4
assertThat(json).array().field("some").field("nested").array("withlist").field("name").read(List) == ['name1', 'name2']
assertThat(json).array().field("someother").field("nested").array("withlist2").read(List) == ['a', 'b']
assertThat(json).array().field("someother").field("nested").field("json").read(Boolean) == true
----

All thanks to the `JsonReader` interface:

[source,java]
----
/**
* Returns the value from the JSON, based on the created JSON Path. If the result is an
* JSON Array and has a single value then that value is returned. If that's an array with
* greater number of results then that array is returned.
*/
<T> T read(Class<T> clazz);
----

== How to add it

Just add it as your dependency (Example for Gradle)

[source,groovy,subs="attributes,verbatim"]
----
testCompile `com.toomuchcoding.jsonassert:jsonassert:{lib_version}`
testCompile 'com.toomuchcoding.jsonassert:jsonassert:{lib_version}'
----

== Dependencies
Expand Down
@@ -1,14 +1,12 @@
package com.toomuchcoding.jsonassert;

import java.util.LinkedList;
import java.util.regex.Pattern;

import com.jayway.jsonpath.DocumentContext;
import net.minidev.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jayway.jsonpath.DocumentContext;

import net.minidev.json.JSONArray;
import java.util.LinkedList;
import java.util.regex.Pattern;

class JsonAsserter implements JsonVerifiable {

Expand Down Expand Up @@ -268,4 +266,18 @@ protected String wrapValueWithSingleQuotes(Object value) {
"'" + stringWithEscapedSingleQuotes(value) + "'" :
value.toString();
}

@Override
@SuppressWarnings("unchecked")
public <T> T read(Class<T> clazz) {
Object readObject = parsedJson.read(jsonPath());
if (readObject instanceof JSONArray) {
JSONArray array = parsedJson.read(jsonPath());
if (array.size() == 1) {
return (T) array.get(0);
}
return (T) array;
}
return (T) readObject;
}
}
Expand Up @@ -20,4 +20,11 @@ public static JsonVerifiable builder() {
return JsonAssertion.assertThat("").withoutThrowingException();
}

/**
* Using a JSON Path builder for the given JSON you can read its value.
*/
public static JsonVerifiable builder(String json) {
return JsonAssertion.assertThat(json).withoutThrowingException();
}

}
@@ -0,0 +1,18 @@
package com.toomuchcoding.jsonassert;

/**
* Contract to read the value from a JSON basing on it.
*
* @author Marcin Grzejszczak
*
* @since 0.4.0
*/
public interface JsonReader {

/**
* Returns the value from the JSON, based on the created JSON Path. If the result is an
* JSON Array and has a single value then that value is returned. If that's an array with
* greater number of results then that array is returned.
*/
<T> T read(Class<T> clazz);
}
Expand Up @@ -7,7 +7,7 @@
*
* @since 0.1.0
*/
public interface JsonVerifiable extends IteratingOverArray {
public interface JsonVerifiable extends IteratingOverArray, JsonReader {

/**
* Assertion of a field inside an array. Use it only for assertion and not traversing
Expand Down
Expand Up @@ -396,4 +396,46 @@ public class JsonAssertionSpec extends Specification {
verifiable.jsonPath() == '''$[?(@.text == 'text with "quotes" inside')]'''
}

def 'should resolve the value of JSON via JSON Path'() {
given:
String json =
'''
[ {
"some" : {
"nested" : {
"json" : "with value",
"anothervalue": 4,
"withlist" : [
{ "name" :"name1"} ,
{"name": "name2"},
{"anothernested": { "name": "name3"} }
]
}
}
},
{
"someother" : {
"nested" : {
"json" : true,
"anothervalue": 4,
"withlist" : [
{ "name" :"name1"} , {"name": "name2"}
],
"withlist2" : [
"a", "b"
]
}
}
}
]
'''
expect:
com.toomuchcoding.jsonassert.JsonPath.builder(json).array().field("some").field("nested").field("json").read(String) == 'with value'
com.toomuchcoding.jsonassert.JsonPath.builder(json).array().field("some").field("nested").field("anothervalue").read(Integer) == 4
assertThat(json).array().field("some").field("nested").array("withlist").field("name").read(List) == ['name1', 'name2']
assertThat(json).array().field("someother").field("nested").array("withlist2").read(List) == ['a', 'b']
assertThat(json).array().field("someother").field("nested").field("json").read(Boolean) == true

}

}
@@ -1,6 +1,7 @@
package com.toomuchcoding.jsonassert

import spock.lang.Specification

/**
* @author Marcin Grzejszczak
*/
Expand Down

0 comments on commit 5584543

Please sign in to comment.