Interface JsonComparator

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface JsonComparator
Strategy interface used to compare JSON strings.

Use of(JsonCompareMode) for a comparator backed by the JSON assertion library available on the classpath, or provide a custom implementation to use another library or configuration.

Since:
6.2
Author:
Phillip Webb
  • Method Details

    • compare

      JsonComparison compare(@Nullable String expectedJson, @Nullable String actualJson)
      Compare the given JSON strings.
      Parameters:
      expectedJson - the expected JSON
      actualJson - the actual JSON
      Returns:
      the JSON comparison
    • assertIsMatch

      default void assertIsMatch(@Nullable String expectedJson, @Nullable String actualJson)
      Assert that the expectedJson matches the comparison rules of ths instance against the actualJson. Throw an AssertionError if the comparison does not match.
      Parameters:
      expectedJson - the expected JSON
      actualJson - the actual JSON
    • of

      static JsonComparator of(JsonCompareMode compareMode)
      Create a JsonComparator for the given JsonCompareMode, backed by the JSON assertion library available on the classpath.

      JsonUnit (net.javacrumbs.json-unit:json-unit-core) is used if present, otherwise JSONassert (org.skyscreamer:jsonassert).

      Parameters:
      compareMode - the compare mode to use
      Returns:
      a new JsonComparator instance
      Throws:
      IllegalStateException - if neither library is present
      Since:
      7.1