Class JsonDiff


  • @ParametersAreNonnullByDefault
    public class JsonDiff
    extends 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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static com.github.fge.msgsimple.bundle.MessageBundle BUNDLE  
      protected static com.google.common.base.Equivalence<com.fasterxml.jackson.databind.JsonNode> EQUIVALENCE  
      protected static com.fasterxml.jackson.databind.ObjectMapper MAPPER  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected JsonDiff()  
    • 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 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
      protected static void computeArray​(Map<com.github.fge.jackson.jsonpointer.JsonPointer,​com.fasterxml.jackson.databind.JsonNode> ret, com.github.fge.jackson.jsonpointer.JsonPointer pointer, com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target)  
      protected static void computeObject​(Map<com.github.fge.jackson.jsonpointer.JsonPointer,​com.fasterxml.jackson.databind.JsonNode> ret, com.github.fge.jackson.jsonpointer.JsonPointer pointer, com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target)  
      protected static void computeUnchanged​(Map<com.github.fge.jackson.jsonpointer.JsonPointer,​com.fasterxml.jackson.databind.JsonNode> ret, com.github.fge.jackson.jsonpointer.JsonPointer pointer, com.fasterxml.jackson.databind.JsonNode first, com.fasterxml.jackson.databind.JsonNode second)  
      protected static void generateArrayDiffs​(DiffProcessor processor, com.github.fge.jackson.jsonpointer.JsonPointer pointer, com.fasterxml.jackson.databind.node.ArrayNode source, com.fasterxml.jackson.databind.node.ArrayNode target)  
      protected static void generateDiffs​(DiffProcessor processor, com.github.fge.jackson.jsonpointer.JsonPointer pointer, com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target)  
      protected static void generateObjectDiffs​(DiffProcessor processor, com.github.fge.jackson.jsonpointer.JsonPointer pointer, com.fasterxml.jackson.databind.node.ObjectNode source, com.fasterxml.jackson.databind.node.ObjectNode target)  
      protected static Map<com.github.fge.jackson.jsonpointer.JsonPointer,​com.fasterxml.jackson.databind.JsonNode> getUnchangedValues​(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target)  
    • Field Detail

      • BUNDLE

        protected static final com.github.fge.msgsimple.bundle.MessageBundle BUNDLE
      • MAPPER

        protected static final com.fasterxml.jackson.databind.ObjectMapper MAPPER
      • EQUIVALENCE

        protected static final com.google.common.base.Equivalence<com.fasterxml.jackson.databind.JsonNode> EQUIVALENCE
    • Constructor Detail

      • JsonDiff

        protected JsonDiff()
    • 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
      • asJson

        public 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
        Parameters:
        source - the node to be patched
        target - the expected result after applying the patch
        Returns:
        the patch as a JsonNode
      • generateDiffs

        protected static void generateDiffs​(DiffProcessor processor,
                                            com.github.fge.jackson.jsonpointer.JsonPointer pointer,
                                            com.fasterxml.jackson.databind.JsonNode source,
                                            com.fasterxml.jackson.databind.JsonNode target)
      • generateObjectDiffs

        protected static void generateObjectDiffs​(DiffProcessor processor,
                                                  com.github.fge.jackson.jsonpointer.JsonPointer pointer,
                                                  com.fasterxml.jackson.databind.node.ObjectNode source,
                                                  com.fasterxml.jackson.databind.node.ObjectNode target)
      • generateArrayDiffs

        protected static void generateArrayDiffs​(DiffProcessor processor,
                                                 com.github.fge.jackson.jsonpointer.JsonPointer pointer,
                                                 com.fasterxml.jackson.databind.node.ArrayNode source,
                                                 com.fasterxml.jackson.databind.node.ArrayNode target)
        Parameters:
        processor -
        pointer -
        source -
        target -
      • getUnchangedValues

        protected static Map<com.github.fge.jackson.jsonpointer.JsonPointer,​com.fasterxml.jackson.databind.JsonNode> getUnchangedValues​(com.fasterxml.jackson.databind.JsonNode source,
                                                                                                                                              com.fasterxml.jackson.databind.JsonNode target)
      • computeUnchanged

        protected static void computeUnchanged​(Map<com.github.fge.jackson.jsonpointer.JsonPointer,​com.fasterxml.jackson.databind.JsonNode> ret,
                                               com.github.fge.jackson.jsonpointer.JsonPointer pointer,
                                               com.fasterxml.jackson.databind.JsonNode first,
                                               com.fasterxml.jackson.databind.JsonNode second)
      • computeObject

        protected static void computeObject​(Map<com.github.fge.jackson.jsonpointer.JsonPointer,​com.fasterxml.jackson.databind.JsonNode> ret,
                                            com.github.fge.jackson.jsonpointer.JsonPointer pointer,
                                            com.fasterxml.jackson.databind.JsonNode source,
                                            com.fasterxml.jackson.databind.JsonNode target)
      • computeArray

        protected static void computeArray​(Map<com.github.fge.jackson.jsonpointer.JsonPointer,​com.fasterxml.jackson.databind.JsonNode> ret,
                                           com.github.fge.jackson.jsonpointer.JsonPointer pointer,
                                           com.fasterxml.jackson.databind.JsonNode source,
                                           com.fasterxml.jackson.databind.JsonNode target)