Class SameJSONAs<T>

java.lang.Object
org.hamcrest.BaseMatcher<T>
org.hamcrest.TypeSafeDiagnosingMatcher<T>
uk.co.datumedge.hamcrest.json.SameJSONAs<T>
Type Parameters:
T - the type of the JSON document. This is typically JSONObject, JSONArray or String.
All Implemented Interfaces:
org.hamcrest.Matcher<T>, org.hamcrest.SelfDescribing

public final class SameJSONAs<T> extends org.hamcrest.TypeSafeDiagnosingMatcher<T>
Matcher that asserts that one JSON document is the same as another.
  • Constructor Details

  • Method Details

    • describeTo

      public void describeTo(org.hamcrest.Description description)
    • matchesSafely

      protected boolean matchesSafely(T actual, org.hamcrest.Description mismatchDescription)
      Specified by:
      matchesSafely in class org.hamcrest.TypeSafeDiagnosingMatcher<T>
    • allowingAnyArrayOrdering

      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
    • allowingExtraUnexpectedFields

      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
    • sameJSONObjectAs

      public static SameJSONAs<org.json.JSONObject> sameJSONObjectAs(org.json.JSONObject expected)
      Creates a matcher that compares JSONObjects.
      Parameters:
      expected - the expected JSONObject instance
      Returns:
      the Matcher instance
    • sameJSONObjectAs

      public static SameJSONAs<org.json.JSONObject> sameJSONObjectAs(org.json.JSONObject expected, JSONModalComparator<org.json.JSONObject> comparator)
    • sameJSONArrayAs

      public static SameJSONAs<org.json.JSONArray> sameJSONArrayAs(org.json.JSONArray expected)
      Creates a matcher that compares JSONArrays.
      Parameters:
      expected - the expected JSONArray instance
      Returns:
      the Matcher instance
    • sameJSONArrayAs

      public static SameJSONAs<? super org.json.JSONArray> sameJSONArrayAs(org.json.JSONArray expected, JSONModalComparator<org.json.JSONArray> comparator)
    • sameJSONAs

      public static SameJSONAs<? super String> sameJSONAs(String expected)
      Creates a matcher that compares JSONObjects or JSONArrays represented as Strings.
      Parameters:
      expected - the expected JSON document
      Returns:
      the Matcher instance
    • sameJSONAs

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