@Test void shouldSeeEmptyForCheckAnyStringNode() { Diff diff = Diff.create("{\"test\": \"${json-unit.any-string}\"}", "{\"test\": \"1\"}", "", "", commonConfig()); diff.similar(); assertThat(listener.getJsonPatch()).isEqualTo("{}"); }
@SuppressWarnings({"all", "unchecked"}) public String getJsonPatch() { final Map<String, Object> jsonDiffPatch = new HashMap<>(); if (getDifferences().size() == 1) { final Difference difference = getDifferences().get(0); final String field = getPath(difference); if (field.isEmpty()) { final ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(getPatch(difference)); } catch (JsonProcessingException e) { throw new IllegalStateException("Could not process patch json", e); } } } getDifferences().forEach(difference -> { final String field = getPath(difference); Map<String, Object> currentMap = jsonDiffPatch; final String fieldWithDots = StringUtils.replace(field, "[", "."); final int len = fieldWithDots.length(); int left = 0; int right = 0; while (left < len) { right = fieldWithDots.indexOf('.', left); if (right == -1) { right = len; } String fieldName = fieldWithDots.substring(left, right); fieldName = StringUtils.remove(fieldName, "]"); if (right != len) { if (!fieldName.isEmpty()) { if (!currentMap.containsKey(fieldName)) { currentMap.put(fieldName, new HashMap<>()); } currentMap = (Map<String, Object>) currentMap.get(fieldName); } if (field.charAt(right) == '[') { if (!currentMap.containsKey(fieldName)) { currentMap.put("_t", "a"); } } } else { final List<?> actualExpectedValue = getPatch(difference); currentMap.put(fieldName, actualExpectedValue); } left = right + 1; } }); final ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(jsonDiffPatch); } catch (JsonProcessingException e) { throw new IllegalStateException("Could not process patch json", e); } }
JsonPatchListener implements DifferenceListener { @SuppressWarnings({"all", "unchecked"}) public String getJsonPatch() { final Map<String, Object> jsonDiffPatch = new HashMap<>(); if (getDifferences().size() == 1) { final Difference difference = getDifferences().get(0); final String field = getPath(difference); if (field.isEmpty()) { final ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(getPatch(difference)); } catch (JsonProcessingException e) { throw new IllegalStateException("Could not process patch json", e); } } } getDifferences().forEach(difference -> { final String field = getPath(difference); Map<String, Object> currentMap = jsonDiffPatch; final String fieldWithDots = StringUtils.replace(field, "[", "."); final int len = fieldWithDots.length(); int left = 0; int right = 0; while (left < len) { right = fieldWithDots.indexOf('.', left); if (right == -1) { right = len; } String fieldName = fieldWithDots.substring(left, right); fieldName = StringUtils.remove(fieldName, "]"); if (right != len) { if (!fieldName.isEmpty()) { if (!currentMap.containsKey(fieldName)) { currentMap.put(fieldName, new HashMap<>()); } currentMap = (Map<String, Object>) currentMap.get(fieldName); } if (field.charAt(right) == '[') { if (!currentMap.containsKey(fieldName)) { currentMap.put("_t", "a"); } } } else { final List<?> actualExpectedValue = getPatch(difference); currentMap.put(fieldName, actualExpectedValue); } left = right + 1; } }); final ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(jsonDiffPatch); } catch (JsonProcessingException e) { throw new IllegalStateException("Could not process patch json", e); } } }
JsonPatchListener implements DifferenceListener { @SuppressWarnings({"all", "unchecked"}) public String getJsonPatch() { final Map<String, Object> jsonDiffPatch = new HashMap<>(); if (getDifferences().size() == 1) { final Difference difference = getDifferences().get(0); final String field = getPath(difference); if (field.isEmpty()) { final ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(getPatch(difference)); } catch (JsonProcessingException e) { throw new IllegalStateException("Could not process patch json", e); } } } getDifferences().forEach(difference -> { final String field = getPath(difference); Map<String, Object> currentMap = jsonDiffPatch; final String fieldWithDots = StringUtils.replace(field, "[", "."); final int len = fieldWithDots.length(); int left = 0; int right = 0; while (left < len) { right = fieldWithDots.indexOf('.', left); if (right == -1) { right = len; } String fieldName = fieldWithDots.substring(left, right); fieldName = StringUtils.remove(fieldName, "]"); if (right != len) { if (!fieldName.isEmpty()) { if (!currentMap.containsKey(fieldName)) { currentMap.put(fieldName, new HashMap<>()); } currentMap = (Map<String, Object>) currentMap.get(fieldName); } if (field.charAt(right) == '[') { if (!currentMap.containsKey(fieldName)) { currentMap.put("_t", "a"); } } } else { final List<?> actualExpectedValue = getPatch(difference); currentMap.put(fieldName, actualExpectedValue); } left = right + 1; } }); final ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(jsonDiffPatch); } catch (JsonProcessingException e) { throw new IllegalStateException("Could not process patch json", e); } }  }
JsonPatchListener implements DifferenceListener { @SuppressWarnings({"all", "unchecked"}) public String getJsonPatch() { final Map<String, Object> jsonDiffPatch = new HashMap<>(); if (getDifferences().size() == 1) { final Difference difference = getDifferences().get(0); final String field = getPath(difference); if (field.isEmpty()) { final ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(getPatch(difference)); } catch (JsonProcessingException e) { throw new IllegalStateException("Could not process patch json", e); } } } getDifferences().forEach(difference -> { final String field = getPath(difference); Map<String, Object> currentMap = jsonDiffPatch; final String fieldWithDots = StringUtils.replace(field, "[", "."); final int len = fieldWithDots.length(); int left = 0; int right = 0; while (left < len) { right = fieldWithDots.indexOf('.', left); if (right == -1) { right = len; } String fieldName = fieldWithDots.substring(left, right); fieldName = StringUtils.remove(fieldName, "]"); if (right != len) { if (!fieldName.isEmpty()) { if (!currentMap.containsKey(fieldName)) { currentMap.put(fieldName, new HashMap<>()); } currentMap = (Map<String, Object>) currentMap.get(fieldName); } if (field.charAt(right) == '[') { if (!currentMap.containsKey(fieldName)) { currentMap.put("_t", "a"); } } } else { final List<?> actualExpectedValue = getPatch(difference); currentMap.put(fieldName, actualExpectedValue); } left = right + 1; } }); final ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(jsonDiffPatch); } catch (JsonProcessingException e) { throw new IllegalStateException("Could not process patch json", e); } }  @Override void diff(final Difference difference, final DifferenceContext differenceContext); List<Difference> getDifferences(); DifferenceContext getContext(); DiffModel getDiffModel(); @SuppressWarnings({"all", "unchecked"}) String getJsonPatch(); }
JsonPatchListener implements DifferenceListener { @SuppressWarnings({"all", "unchecked"}) public String getJsonPatch() { final Map<String, Object> jsonDiffPatch = new HashMap<>(); if (getDifferences().size() == 1) { final Difference difference = getDifferences().get(0); final String field = getPath(difference); if (field.isEmpty()) { final ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(getPatch(difference)); } catch (JsonProcessingException e) { throw new IllegalStateException("Could not process patch json", e); } } } getDifferences().forEach(difference -> { final String field = getPath(difference); Map<String, Object> currentMap = jsonDiffPatch; final String fieldWithDots = StringUtils.replace(field, "[", "."); final int len = fieldWithDots.length(); int left = 0; int right = 0; while (left < len) { right = fieldWithDots.indexOf('.', left); if (right == -1) { right = len; } String fieldName = fieldWithDots.substring(left, right); fieldName = StringUtils.remove(fieldName, "]"); if (right != len) { if (!fieldName.isEmpty()) { if (!currentMap.containsKey(fieldName)) { currentMap.put(fieldName, new HashMap<>()); } currentMap = (Map<String, Object>) currentMap.get(fieldName); } if (field.charAt(right) == '[') { if (!currentMap.containsKey(fieldName)) { currentMap.put("_t", "a"); } } } else { final List<?> actualExpectedValue = getPatch(difference); currentMap.put(fieldName, actualExpectedValue); } left = right + 1; } }); final ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(jsonDiffPatch); } catch (JsonProcessingException e) { throw new IllegalStateException("Could not process patch json", e); } }  @Override void diff(final Difference difference, final DifferenceContext differenceContext); List<Difference> getDifferences(); DifferenceContext getContext(); DiffModel getDiffModel(); @SuppressWarnings({"all", "unchecked"}) String getJsonPatch();  }