public final class

SameJSONAs

extends TypeSafeDiagnosingMatcher<T>
java.lang.Object
   ↳ org.hamcrest.BaseMatcher<T>
     ↳ org.hamcrest.TypeSafeDiagnosingMatcher<T>
       ↳ uk.co.datumedge.hamcrest.json.SameJSONAs<T>

Class Overview

Matcher that asserts that one JSON document is the same as another.

Summary

Public Constructors
SameJSONAs(T expected, JSONModalComparator<T> comparator)
Public Methods
SameJSONAs<T> allowingAnyArrayOrdering()
Creates a matcher that allows any element ordering within JSON arrays.
SameJSONAs<T> allowingExtraUnexpectedFields()
Creates a matcher that allows fields not present in the expected JSON document.
void describeTo(Description description)
static SameJSONAs<JSONArray> sameJSONArrayAs(JSONArray expected)
Creates a matcher that compares JSONArrays.
static SameJSONAs<? super JSONArray> sameJSONArrayAs(JSONArray expected, JSONModalComparator<JSONArray> comparator)
static SameJSONAs<? super String> sameJSONAs(String expected)
Creates a matcher that compares JSONObjects or JSONArrays.
static SameJSONAs<? super String> sameJSONAs(String expected, JSONModalComparator<String> comparator)
static SameJSONAs<JSONObject> sameJSONObjectAs(JSONObject expected)
Creates a matcher that compares JSONObjects.
static SameJSONAs<JSONObject> sameJSONObjectAs(JSONObject expected, JSONModalComparator<JSONObject> comparator)
Protected Methods
boolean matchesSafely(T actual, Description mismatchDescription)
[Expand]
Inherited Methods
From class org.hamcrest.TypeSafeDiagnosingMatcher
From class org.hamcrest.BaseMatcher
From class java.lang.Object
From interface org.hamcrest.Matcher
From interface org.hamcrest.SelfDescribing

Public Constructors

public SameJSONAs (T expected, JSONModalComparator<T> comparator)

Public Methods

public SameJSONAs<T> allowingAnyArrayOrdering ()

Creates a matcher that allows any element ordering within JSON arrays. For example, {"fib":[0,1,1,2,3]} will match {"fib":[3,1,0,2,1]}.

Returns
  • the configured matcher

public SameJSONAs<T> allowingExtraUnexpectedFields ()

Creates a matcher that allows fields not present in the expected JSON document. For example, if the expected document is

{
    "name" : "John Smith",
    "address" : {
        "street" : "29 Acacia Road"
    }
}
then the following document will match:
{
    "name" : "John Smith",
    "age" : 34,
    "address" : {
        "street" : "29 Acacia Road",
        "city" : "Huddersfield"
    }
}
All array elements must exist in both documents, so the expected document
[
    { "name" : "John Smith" }
]
will not match the actual document
[
    { "name" : "John Smith" },
    { "name" : "Bob Jones" }
]

Returns
  • the configured matcher

public void describeTo (Description description)

public static SameJSONAs<JSONArray> sameJSONArrayAs (JSONArray expected)

Creates a matcher that compares JSONArrays.

Parameters
expected the expected JSONArray instance
Returns
  • the Matcher instance

public static SameJSONAs<? super JSONArray> sameJSONArrayAs (JSONArray expected, JSONModalComparator<JSONArray> comparator)

public static SameJSONAs<? super String> sameJSONAs (String expected)

Creates a matcher that compares JSONObjects or JSONArrays.

Parameters
expected the expected JSON document
Returns
  • the Matcher instance

public static SameJSONAs<? super String> sameJSONAs (String expected, JSONModalComparator<String> comparator)

public static SameJSONAs<JSONObject> sameJSONObjectAs (JSONObject expected)

Creates a matcher that compares JSONObjects.

Parameters
expected the expected JSONObject instance
Returns
  • the Matcher instance

public static SameJSONAs<JSONObject> sameJSONObjectAs (JSONObject expected, JSONModalComparator<JSONObject> comparator)

Protected Methods

protected boolean matchesSafely (T actual, Description mismatchDescription)