Categorygithub.com/mrutkows/go-jsondiff
modulepackage
0.2.0
Repository: https://github.com/mrutkows/go-jsondiff.git
Documentation: pkg.go.dev

# README

go-jsondiff

License

Installation

go get github.com/mrutkows/go-jsondiff

JSON Delta format

The JSON formatted output from a comparison is based upon a simplified methodology described originally here:

Delta (object) types

Added

a value was added, i.e. it was undefined and now has a value.

delta = [ newValue ]

internal representation:

// delta.(type) == *diff.Added
type Added struct {
	postDelta         // postDelta{position} where `position` is an interface with a String() method
                      // "PostPosition() interface"
	Value interface{} // added value (i.e., 'newValue')
	                  // "Value()" interface
	similarityCache
}

Notes:
  • "add" operation has no "preDelta" value, only a "postDelta" value

Deleted

a value was deleted, i.e. it had a value and is now undefined

delta = [ oldValue, 0, 0 ]
Notes:
  • "delete" operation has no "postDelta" value, only a "preDelta" value

Modified

a value was replaced by another value

delta = [ oldValue, newValue ]

Array (with inner changes)

value is an array, and there are nested changes inside its items

delta = {
  _t: 'a',
  index1: innerDelta1,
  index2: innerDelta2,
  index5: innerDelta5
}
Notes:
  • only indices with "inner" deltas are included
  • _t: 'a': this tag indicates the delta applies to an array,
    • if a regular object (or a value type) is found when patching, an error will be thrown

internal representation:

delta.(type) == *diff.Array

Array Moves

an item was moved to a different position in the same array

delta = [ '', destinationIndex, 3]
Notes:
  • '': represents the moved item value (suppressed by default)
  • 3: indicates "array move"
JSON Delta format example
 {
   "arr": [
     0: "arr0",
     1: 21,
     2: {
       "num": 1,
-      "str": "pek3f"
+      "str": "changed"
     },
     3: [
       0: 0,
-      1: "1"
+      1: "changed"
     ]
   ],
   "bool": true,
   "num_float": 39.39,
   "num_int": 13,
   "obj": {
     "arr": [
       0: 17,
       1: "str",
       2: {
-        "str": "eafeb"
+        "str": "changed"
       }
     ],
+    "new": "added",
-    "num": 19,
     "obj": {
-      "num": 14,
+      "num": 9999
-      "str": "efj3"
+      "str": "changed"
     },
     "str": "bcded"
   },
   "str": "abcde"
 }

When you prefer the delta format of jsondiffpatch, add the -f delta option.

jd -f delta one.json another.json

This command shows:

{
  "arr": {
    "2": {
      "str": [
        "pek3f",
        "changed"
      ]
    },
    "3": {
      "1": [
        "1",
        "changed"
      ],
      "_t": "a"
    },
    "_t": "a"
  },
  "obj": {
    "arr": {
      "2": {
        "str": [
          "eafeb",
          "changed"
        ]
      },
      "_t": "a"
    },
    "new": [
      "added"
    ],
    "num": [
      19,
      0,
      0
    ],
    "obj": {
      "num": [
        14,
        9999
      ],
      "str": [
        "efj3",
        "changed"
      ]
    }
  }
}

Credits

This package is based upon a fork of https://github.com/yudai/gojsondiff and includes the LCS algorithm implemented in https://github.com/yudai/golcs.

Creation of this derivative package was necessitated as both these package repositories (and their dependencies) have not been maintained for over 6 years with known bugs and vulnerabilities accumulating.

Copies of the licenses from these originating projects are included here:

# Packages

No description provided by the author
No description provided by the author

# Functions

New returns new Differ with default configuration.
NewAdded returns a new Added.
NewArray returns an Array.
NewDeleted returns a Deleted.
New creates a new LCS calculator from two arrays.
NewModified returns a Modified.
No description provided by the author
NewObject returns an Object.
NewTextDiff returns.
No description provided by the author

# Structs

An Added represents a new added field of an object or an array.
An Array is a Delta that represents an array of JSON.
A "Deleted" type represents deleted field or index of an Object or an Array.
A Differ compares JSON objects and applies patches.
IndexPair represents an pair of indices in the Left and Right arrays found in the LCS value.
A Modified represents a field whose value is changed.
A "Moved" type represents field that is moved, which means the index or name is changed.
An Object is a Delta that represents an object of JSON.
A TextDiff represents a Modified with TextDiff between the old and the new values.
No description provided by the author

# Interfaces

A Delta represents an atomic difference between two JSON objects.
A Diff holds deltas generated by a Differ.
interface to calculate the Longest Common Sequences (LCS) of two arrays.
A Position represents the position of a Delta in an object or an array.
A PreDelta is a Delta that has a position of the right side JSON object.
A PreDelta is a Delta that has a position of the left side JSON object.

# Type aliases

A Index is a Position with an int value, which means the Delta is in an Array.
A Name is a Position with a string, which means the delta is in an object.