@groovy.transform.CompileStatic class JsonUtils extends java.lang.Object
Utility method for handling JSON.
JSON assertion helpers used by HTTP test response expectations.
This utility provides two styles of validation:
verifyJsonTree(java.net.http.HttpResponse, Object) and
#verifyJsonTree(java.net.http.HttpResponse, CharSequence), where object key order is ignored.verifyJsonTreeContains(java.net.http.HttpResponse, Object) and
verifyJsonTreeContains(java.net.http.HttpResponse, CharSequence), where expected map keys
must exist and expected list elements are matched by index.Failures include canonicalized JSON representations to make diffs easier to read.
| Modifiers | Name | Description |
|---|---|---|
static class |
JsonUtils.SlurperConfig |
Optional configuration for JSON parsing backed by JsonSlurper. |
| Constructor and description |
|---|
JsonUtils() |
| Type Params | Return Type | Name and description |
|---|---|---|
|
static java.lang.Object |
parseText(java.lang.CharSequence json, JsonUtils.SlurperConfig jsonSlurperConfig) |
|
static void |
verifyJsonTree(java.net.http.HttpResponse<?> response, java.lang.Object expected)Verifies that the response JSON is structurally equal to the expected object tree. |
|
static void |
verifyJsonTree(java.net.http.HttpResponse<?> response, java.lang.Object expected, JsonUtils.SlurperConfig jsonSlurperConfig) |
|
static void |
verifyJsonTree(java.net.http.HttpResponse<?> response, java.lang.CharSequence expectedJson)Verifies that the response JSON is structurally equal to the expected JSON string. |
|
static void |
verifyJsonTree(java.net.http.HttpResponse<?> response, java.lang.CharSequence expectedJson, JsonUtils.SlurperConfig jsonSlurperConfig) |
|
static void |
verifyJsonTreeContains(java.net.http.HttpResponse<?> response, java.lang.Object expected)Verifies that the response JSON contains the expected subset. |
|
static void |
verifyJsonTreeContains(java.net.http.HttpResponse<?> response, java.lang.Object expected, JsonUtils.SlurperConfig jsonSlurperConfig) |
|
static void |
verifyJsonTreeContains(java.net.http.HttpResponse<?> response, java.lang.CharSequence expectedJson)Verifies that the response JSON contains the expected subset represented as JSON text. |
|
static void |
verifyJsonTreeContains(java.net.http.HttpResponse<?> response, java.lang.CharSequence expectedJson, JsonUtils.SlurperConfig jsonSlurperConfig) |
| Methods inherited from class | Name |
|---|---|
class java.lang.Object |
java.lang.Object#equals(java.lang.Object), java.lang.Object#getClass(), java.lang.Object#hashCode(), java.lang.Object#notify(), java.lang.Object#notifyAll(), java.lang.Object#toString(), java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int) |
Verifies that the response JSON is structurally equal to the expected object tree.
Map keys are compared independent of declaration order.
response - HTTP response with a JSON bodyexpected - expected JSON-compatible object (for example Map, List, scalar)Verifies that the response JSON is structurally equal to the expected JSON string.
Both sides are parsed and canonicalized before comparison, so key ordering does not affect equality.
response - HTTP response with a JSON bodyexpectedJson - expected JSON document textVerifies that the response JSON contains the expected subset.
Subset rules:
response - HTTP response with a JSON bodyexpected - expected subset expressed as JSON-compatible objectsVerifies that the response JSON contains the expected subset represented as JSON text.
response - HTTP response with a JSON bodyexpectedJson - expected subset as JSON text