jsondiff

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2024 License: MIT Imports: 8 Imported by: 0

README

jsondiff

Zero-dependencies simple JSON diffing and formatting library for Go

This is a derivative of yudai/gojsondiff and yudai/golcs, removing all the complexity, dependencies and fancy options. Also, notably, this library removes the ability to apply diffs, focusing entirely on diffing for presentation purposes.

Usage

import (
    "github.com/andreyvit/jsondiff"
)

func main() {
    before := map[string]any{
        "foo": 10,
        "bar": 20,
        "boz": 30,
    }
    after := map[string]any{
        "foo": 10,
        "bar": 42,
    }
    diff := jsondiff.CompareObjects(before, after)
    fmt.Println(diff.Format(before))
}

Outputs:

 {
-  "bar": 20,
+  "bar": 42,
-  "boz": 30,
   "foo": 10
 }

Use diff.Format(before, jsondiff.Colored) to add some ANSI colors for printing.

Documentation

Overview

jsondiff is a zero-dependencies simple JSON differ.

Example
package main

import (
	"fmt"

	"github.com/andreyvit/jsondiff"
)

func main() {
	before := map[string]any{
		"foo": 10,
		"bar": 20,
		"boz": 30,
	}
	after := map[string]any{
		"foo": 10,
		"bar": 42,
	}
	diff := jsondiff.CompareObjects(before, after)
	fmt.Println(diff.Format(before))
}
Output:

{
-  "bar": 20,
+  "bar": 42,
-  "boz": 30,
   "foo": 10
 }

Index

Examples

Constants

View Source
const (
	AsciiSame    = " "
	AsciiAdded   = "+"
	AsciiDeleted = "-"
)

Variables

View Source
var AsciiStyles = map[string]string{
	AsciiAdded:   "30;42",
	AsciiDeleted: "30;41",
}

Functions

This section is empty.

Types

type Added

type Added struct {
	Position Position
	Value    any
}

func NewAdded

func NewAdded(position Position, value any) *Added

func (*Added) PositionMatches

func (d *Added) PositionMatches(pos Position) bool

func (*Added) Similarity

func (*Added) Similarity() float64

type Array

type Array struct {
	Position Position
	Deltas   []Delta
	// contains filtered or unexported fields
}

func NewArray

func NewArray(position Position, deltas []Delta) *Array

func (*Array) PositionMatches

func (d *Array) PositionMatches(pos Position) bool

func (*Array) Similarity

func (d *Array) Similarity() float64

type Deleted

type Deleted struct {
	Position Position
	Value    any
}

func NewDeleted

func NewDeleted(position Position, value any) *Deleted

func (*Deleted) PositionMatches

func (d *Deleted) PositionMatches(pos Position) bool

func (Deleted) Similarity

func (Deleted) Similarity() float64

type Delta

type Delta interface {
	// Similarity ranges from 0 (completely different) to 1 (completely equal).
	Similarity() float64

	PositionMatches(pos Position) bool
}

A Delta represents an atomic difference between two JSON objects.

type Diff

type Diff []Delta

func CompareObjects

func CompareObjects(left, right map[string]any) Diff

func (Diff) Format

func (diff Diff) Format(left any, opts ...FormatOption) string

type FormatOption

type FormatOption int

FormatOption can be passed to Diff.Format. Treat these as opaque, i.e. don't rely on the underlying type or values of FormatOptions.

const (
	ShowArrayIndex FormatOption = iota
	Colored
	HideUnchangedProperties
)

type Index

type Index int

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

func (Index) CompareTo

func (i Index) CompareTo(another Position) bool

func (Index) Equal

func (i Index) Equal(another Position) bool

func (Index) String

func (i Index) String() string

type Modified

type Modified struct {
	Position Position
	OldValue any
	NewValue any
	// contains filtered or unexported fields
}

A Modified represents a field whose value is changed.

func NewModified

func NewModified(position Position, oldValue, newValue any) *Modified

func (*Modified) PositionMatches

func (d *Modified) PositionMatches(pos Position) bool

func (*Modified) Similarity

func (d *Modified) Similarity() float64

type Moved

type Moved struct {
	OldPosition Position
	NewPosition Position
	Value       any
	// contains filtered or unexported fields
}

func NewMoved

func NewMoved(oldPosition Position, newPosition Position, value any) *Moved

func (*Moved) PositionMatches

func (d *Moved) PositionMatches(pos Position) bool

func (*Moved) Similarity

func (d *Moved) Similarity() float64

type Name

type Name string

A Name is a Postition with a string, which means the delta is in an object.

func (Name) CompareTo

func (n Name) CompareTo(another Position) bool

func (Name) Equal

func (n Name) Equal(another Position) bool

func (Name) String

func (n Name) String() (name string)

type Object

type Object struct {
	Position Position
	Deltas   []Delta
	// contains filtered or unexported fields
}

func NewObject

func NewObject(position Position, deltas []Delta) *Object

func (*Object) PositionMatches

func (d *Object) PositionMatches(pos Position) bool

func (*Object) Similarity

func (d *Object) Similarity() (similarity float64)

type Position

type Position interface {
	// String returns the position as a string
	String() (name string)

	Equal(another Position) bool

	// CompareTo returns a true if the Position is smaller than another Position.
	// This function is used to sort Positions by the sort package.
	CompareTo(another Position) bool
}

A Position represents the position of a Delta in an object or an array.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL