package module
v0.0.0-...-43f6cf3 Latest Latest

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

Go to latest
Published: Apr 30, 2023 License: MIT Imports: 6 Imported by: 65


JsonDiff library

The main purpose of the library is integration into tests which use json and providing human-readable output of test results.

The lib can compare two json items and return a detailed report of the comparison.

At the moment it can detect a couple of types of differences:

  • FullMatch - means items are identical.
  • SupersetMatch - means first item is a superset of a second item.
  • NoMatch - means objects are different.

Being a superset means that every object and array which don't match completely in a second item must be a subset of a first item. For example:

{"a": 1, "b": 2, "c": 3}

Is a superset of (or second item is a subset of a first one):

{"a": 1, "c": 3}

Library API documentation can be found on godoc.org: https://godoc.org/github.com/nsf/jsondiff

You can try LIVE version here (compiled to wasm): https://nosmileface.dev/jsondiff

The library is inspired by http://tlrobinson.net/projects/javascript-fun/jsondiff/




This section is empty.


This section is empty.


func SkippedArrayElement

func SkippedArrayElement(n int) string

func SkippedObjectProperty

func SkippedObjectProperty(n int) string


type Difference

type Difference int
const (
	FullMatch Difference = iota

func Compare

func Compare(a, b []byte, opts *Options) (Difference, string)

Compare compares two JSON documents using given options. Returns difference type and a string describing differences.

FullMatch means provided arguments are deeply equal.

SupersetMatch means first argument is a superset of a second argument. In this context being a superset means that for each object or array in the hierarchy which don't match exactly, it must be a superset of another one. For example:

{"a": 123, "b": 456, "c": [7, 8, 9]}

Is a superset of:

{"a": 123, "c": [7, 8]}

NoMatch means there is no match.

The rest of the difference types mean that one of or both JSON documents are invalid JSON.

Returned string uses a format similar to pretty printed JSON to show the human-readable difference between provided JSON documents. It is important to understand that returned format is not a valid JSON and is not meant to be machine readable.

func CompareStreams

func CompareStreams(a, b io.Reader, opts *Options) (Difference, string)

CompareStreams compares two JSON documents streamed by the specified readers. See the documentation for `Compare` for a description of the input options and return values.

func (Difference) String

func (d Difference) String() string

type Options

type Options struct {
	Normal                Tag
	Added                 Tag
	Removed               Tag
	Changed               Tag
	Skipped               Tag
	SkippedArrayElement   func(n int) string
	SkippedObjectProperty func(n int) string
	Prefix                string
	Indent                string
	PrintTypes            bool
	ChangedSeparator      string
	// When provided, this function will be used to compare two numbers. By default numbers are compared using their
	// literal representation byte by byte.
	CompareNumbers func(a, b json.Number) bool
	// When true, only differences will be printed. By default, it will print the full json.
	SkipMatches bool

func DefaultConsoleOptions

func DefaultConsoleOptions() Options

Provides a set of options that are well suited for console output. Options use ANSI foreground color escape sequences to highlight changes.

func DefaultHTMLOptions

func DefaultHTMLOptions() Options

Provides a set of options that are well suited for HTML output. Works best inside <pre> tag.

func DefaultJSONOptions

func DefaultJSONOptions() Options

Provides a set of options in JSON format that are fully parseable.

type Tag

type Tag struct {
	Begin string
	End   string

Jump to

Keyboard shortcuts

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