Class JsonDiff


  • @ParametersAreNonnullByDefault
    public final class JsonDiff
    extends java.lang.Object
    JSON "diff" implementation

    This class generates a JSON Patch (as in, an RFC 6902 JSON Patch) given two JSON values as inputs. The patch can be obtained directly as a JsonPatch or as a JsonNode.

    Note: there is no guarantee about the usability of the generated patch for any other source/target combination than the one used to generate the patch.

    This class always performs operations in the following order: removals, additions and replacements. It then factors removal/addition pairs into move operations, or copy operations if a common element exists, at the same pointer, in both the source and destination.

    You can obtain a diff either as a JsonPatch directly or, for backwards compatibility, as a JsonNode.

    Since:
    1.2
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static com.fasterxml.jackson.databind.JsonNode asJson​(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target)
      Generate a JSON patch for transforming the source node into the target node
      static com.fasterxml.jackson.databind.JsonNode asJsonIgnoringFields​(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target, java.util.List<java.lang.String> fieldsToIgnore)
      Generate a JSON patch for transforming the source node into the target node ignoring given fields
      static JsonPatch asJsonPatch​(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target)
      Generate a JSON patch for transforming the source node into the target node
      static JsonPatch asJsonPatchIgnoringFields​(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target, java.util.List<java.lang.String> fieldsToIgnore)
      Generate a JSON patch for transforming the source node into the target node ignoring given fields
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • asJsonPatch

        public static JsonPatch asJsonPatch​(com.fasterxml.jackson.databind.JsonNode source,
                                            com.fasterxml.jackson.databind.JsonNode target)
        Generate a JSON patch for transforming the source node into the target node
        Parameters:
        source - the node to be patched
        target - the expected result after applying the patch
        Returns:
        the patch as a JsonPatch
        Since:
        1.9
      • asJsonPatchIgnoringFields

        public static JsonPatch asJsonPatchIgnoringFields​(com.fasterxml.jackson.databind.JsonNode source,
                                                          com.fasterxml.jackson.databind.JsonNode target,
                                                          java.util.List<java.lang.String> fieldsToIgnore)
                                                   throws JsonPatchException
        Generate a JSON patch for transforming the source node into the target node ignoring given fields
        Parameters:
        source - the node to be patched
        target - the expected result after applying the patch
        fieldsToIgnore - list of JsonPath or JsonPointer paths which should be ignored when generating diff. Non-existing fields are ignored.
        Returns:
        the patch as a JsonPatch
        Throws:
        JsonPatchException - if fieldsToIgnored not in valid JsonPath or JsonPointer format
        Since:
        2.0.0
      • asJson

        public static com.fasterxml.jackson.databind.JsonNode asJson​(com.fasterxml.jackson.databind.JsonNode source,
                                                                     com.fasterxml.jackson.databind.JsonNode target)
                                                              throws JsonPatchException
        Generate a JSON patch for transforming the source node into the target node
        Parameters:
        source - the node to be patched
        target - the expected result after applying the patch
        Returns:
        the patch as a JsonNode
        Throws:
        JsonPatchException - when cannot generate JSON diff
      • asJsonIgnoringFields

        public static com.fasterxml.jackson.databind.JsonNode asJsonIgnoringFields​(com.fasterxml.jackson.databind.JsonNode source,
                                                                                   com.fasterxml.jackson.databind.JsonNode target,
                                                                                   java.util.List<java.lang.String> fieldsToIgnore)
                                                                            throws JsonPatchException
        Generate a JSON patch for transforming the source node into the target node ignoring given fields
        Parameters:
        source - the node to be patched
        target - the expected result after applying the patch
        fieldsToIgnore - list of JsonPath or JsonPointer paths which should be ignored when generating diff. Non-existing fields are ignored.
        Returns:
        the patch as a JsonNode
        Throws:
        JsonPatchException - if fieldsToIgnored not in valid JsonPath or JsonPointer format
        Since:
        2.0.0