jsondiff

package module
v0.0.0-...-c3db9b8 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2022 License: MIT Imports: 5 Imported by: 1

README

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 (thanks to gopherjs): https://nosmileface.dev/jsondiff

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Difference

type Difference int

Difference type ?

const (
	// FullMatch enum
	FullMatch Difference = iota
	// SupersetMatch enum
	SupersetMatch
	// NoMatch enum
	NoMatch
	// FirstArgIsInvalidJSON enum
	FirstArgIsInvalidJSON
	// SecondArgIsInvalidJSON enum
	SecondArgIsInvalidJSON
	// BothArgsAreInvalidJSON enum
	BothArgsAreInvalidJSON
)

func Compare

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

Compare 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 (Difference) String

func (d Difference) String() string

type Options

type Options struct {
	Normal     Tag
	Added      Tag
	Removed    Tag
	Changed    Tag
	Prefix     string
	Indent     string
	PrintTypes bool
}

Options ...

func DefaultConsoleOptions

func DefaultConsoleOptions() Options

DefaultConsoleOptions 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

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

type Tag

type Tag struct {
	Begin string
	End   string
}

Tag range

Jump to

Keyboard shortcuts

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