jsondiff

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 9 Imported by: 1

README

JSONDIFF

Based on: https://github.com/elgris/jsondiff

A simple little tool that produces readable diff of 2 JSON-able (read "convertible to map[string]interface{}") objects. Useful for diagnostics or debugging.

A command line interface that uses this is: https://github.com/pschlump/check-json-syntax It also provides syntax checking for JSON files.

Installation

go get github.com/elgris/jsondiff

Examples of the output

screen shot 2016-05-20 at 13 03 06

Limitation

  • Coloured output tested with terminals only. This only works on Mac and Linux. I will modify this so it works on windows in a few days.
  • The tool converts input data into map[string]interface{} or []interface{} with json encoding/decoding. Hence, types of input map will change during unmarshal step: integers become float64 and so on (check https://golang.org/pkg/encoding/json/ for details).

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(diff Diff) []byte

Format produces formatted output for a diff that can be printed. Uses colorization which may not work with terminals that don't support ASCII coloring (Windows is under question).

Types

type Diff

type Diff struct {
	Err     error // error message if any (can't unmarsal into ...)
	HasDiff bool  // True if there is a difference or error makes it impossible to check
	// contains filtered or unexported fields
}

Diff is a result of comparison operation. Provides list of items that describe difference between objects piece by piece

func Compare

func Compare(a, b interface{}) Diff

Compare produces list of diff items that define difference between objects "a" and "b". Note: if objects are equal, all diff items will have Resolution of type TypeEquals

func CompareFiles

func CompareFiles(afn, bfn string) Diff

CompareFiels will read in 2 files, hopefully both JSON, and perform the compare on them.

func CompareMemToFile

func CompareMemToFile(a interface{}, bfn string) Diff

CompareMemToFile compares an in memory structure to a file. The primary intended use for this is in testing code where an in-memory structure has been created and a correct reference copy is in a directory on disk.

 d := jsondiff.Compare ( inMem, "./testdir/test1.json" )
	if d.HasDiff {
		t.Error ( "failed to pass test1" )
	}

func (*Diff) Add

func (d *Diff) Add(item DiffItem)

Add adds new item to diff object

func (Diff) IsEqual

func (d Diff) IsEqual() bool

IsEqual checks if given diff objects does not contain any non-equal element. When IsEqual returns "true" that means there is no difference between compared objects

func (Diff) Items

func (d Diff) Items() []DiffItem

Items returns list of diff items

type DiffItem

type DiffItem struct {
	Key        string
	ValueA     interface{}
	Resolution ResolutionType
	ValueB     interface{}
}

DiffItem defines a difference between 2 items with resolution type

type ResolutionType

type ResolutionType int

ResolutionType defines a type of comparison: equality, non-equality, new sub-diff and so on

const (
	TypeEquals ResolutionType = iota
	TypeNotEquals
	TypeAdded
	TypeRemoved
	TypeDiff
)

func (ResolutionType) String

func (rt ResolutionType) String() string

String allows for printing of ResolutionType as a name

Jump to

Keyboard shortcuts

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