JsonMatcher implements JsonSerializable, Predicate<JsonSerializable> { @NonNull public static JsonMatcher parse(@Nullable JsonValue jsonValue) throws JsonException { if (jsonValue == null || !jsonValue.isJsonMap() || jsonValue.optMap().isEmpty()) { throw new JsonException("Unable to parse empty JsonValue: " + jsonValue); } JsonMap map = jsonValue.optMap(); if (!map.containsKey(VALUE_KEY)) { throw new JsonException("JsonMatcher must contain a value matcher."); } JsonMatcher.Builder builder = JsonMatcher.newBuilder() .setKey(map.opt(FIELD_KEY).getString()) .setValueMatcher(ValueMatcher.parse(map.get(VALUE_KEY))); JsonValue scope = map.opt(SCOPE_KEY); if (scope.isString()) { builder.setScope(scope.optString()); } else if (scope.isJsonList()) { List<String> scopes = new ArrayList<>(); for (JsonValue field : scope.optList().getList()) { scopes.add(field.getString()); } builder.setScope(scopes); } if (map.containsKey(IGNORE_CASE_KEY)) { builder.setIgnoreCase(map.opt(IGNORE_CASE_KEY).getBoolean(false)); } return builder.build(); } private  JsonMatcher(@NonNull Builder builder); @NonNull @Override JsonValue toJsonValue(); @Override boolean apply(@Nullable JsonSerializable jsonSerializable); @NonNull static JsonMatcher parse(@Nullable JsonValue jsonValue); @NonNull static Builder newBuilder(); @Override boolean equals(@Nullable Object o); @Override int hashCode();  }
@Test public void testParse() throws JsonException { ValueMatcher valueMatcher = ValueMatcher.newValueMatcher(JsonValue.wrap("string")); JsonValue valueJson = JsonMap.newBuilder() .put("equals", "string") .build() .toJsonValue(); JsonValue matcherJson = JsonMap.newBuilder() .put("key", "key") .put("value", valueJson) .put("scope", JsonValue.wrap(Arrays.asList("properties", "inner"))) .build() .toJsonValue(); JsonMatcher matcher = JsonMatcher.newBuilder() .setKey("key") .setScope(Arrays.asList("properties", "inner")) .setValueMatcher(valueMatcher) .build(); assertEquals(matcher, JsonMatcher.parse(matcherJson)); JsonValue ignoreCaseMatcherJson = JsonMap.newBuilder() .put("key", "key") .put("value", valueJson) .put("scope", new JsonList(Collections.singletonList(JsonValue.wrap("properties")))) .put("ignore_case", true) .build() .toJsonValue(); JsonMatcher ignoreCaseMatcher = JsonMatcher.newBuilder() .setKey("key") .setScope("properties") .setValueMatcher(valueMatcher) .setIgnoreCase(true) .build(); assertEquals(ignoreCaseMatcher, JsonMatcher.parse(ignoreCaseMatcherJson)); }
@Test(expected = JsonException.class) public void testParseInvalidJson() throws JsonException { JsonMatcher.parse(JsonValue.wrap("not valid")); }