jsoncomp

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

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

Go to latest
Published: Mar 13, 2023 License: MIT Imports: 9 Imported by: 0

README

jsoncomp

Json-compare is a simple package that allows you to easily and fastly compare two .json files. Support key and multi-key comparison. You can also ignore certain fields' values or perform comparison insensitive to data types.

Usage

Compare files just as they are:

import "github.com/a-a-novikov/jsoncomp"

c := jsoncomp.NewComparator(
    "expected.json",  // path of first file
    "actual.json",    // path of second file
    false,            // ignore types for same values
)

// compare "actual.json" from the perspective of "expected.json"'s structure
c.CompareWithRight()  // c.CompareWithLeft() / c.FullCompare()

// save diff logs as a text file
c.SaveDiffLogs()

// or print them into stdout
fmt.Print(c.Summary())

Diff-log's output example:

actual.json//<array>//3//last_name
property is missing
actual.json//<array>//8//id
incorrect type: expected 4 <int>, got 4 <str> instead
actual.json//<array>
lack of items in array: expected 9 items, got only 8
actual.json//<array>//5//name
unequal values: expected Alex, got Aleks instead
-------------------
TOTAL: 4 differences
- Missing Properties: 1
- Incorrect Type: 1
- Lack of Items: 1
- Exceeding Items: 0
- Unequal Vales: 1
- Missing Items: 0

Set key properties or properties to ignore i.o. to perform more accurate comparisons of objects in arrays:

// expected.json: {"cats": [{"id": 4, "name": "Nyan"}, {"id": 2, "name": "Marx"}, {"id": 8, "name": "Flake"}]}
// actual.json: {"cats": [{"id": 2, "name": "Marx"}, {"id": 4, "name": "Naan"}]}

c := jsoncomp.NewComparatorWithKeys(
    "expected.json",
    "actual.json",
    []string{"DATA//cats//<array>//id"},  //  <- just pass a "path" to needed property using following keywords: 
    []string{},                           //  DATA - points to the root of file 
    false,                                //  <array> - indicates array with key property's object
)

In this case, saved diff log would look like that:

actual.json//cats//<array>
lack of items in array: expected 3 items, got only 2
actual.json//cats//<array>//0//name
unequal values: expected Nyan, got Naan instead
actual.json//cats//<array>//2
missing array item: expected <object> with id=8

Here's an exmaple of comparison with non-important fields set to ignoreKeys parameter:

// expected.json: [{"id": 4, "name": "Nyan", "age": 2}, {"id": 2, "name": "Marx", "age": 7}, {"id": 8, "name": "Flake", "age": 4}]
// actual.json: [{"id": 2, "name": "Marx", "age": 7}, {"id": 4, "name": "Naan", "age": "two"}, {"id": 9, "name": "Lol", "age": 1}]

c := jsoncomp.NewComparatorWithKeys(
    "expected.json",
    "actual.json",
    []string{"DATA//<array>//id"},
    []string{"DATA//<array>//age},  // <-------
    false,
)  

And here the result:

actual.json//<array>//0//name
unequal values: expected Nyan, got Naan instead
actual.json//<array>//2
missing array item: expected <object> with id=8

If you want to compare ignoring type-differences between similar values like "1.4" vs 1.4 - just set IgnoreTypes as true param in Comparator:

c := jsoncomp.NewComparatorWithKeys(
    "expected.json",
    "actual.json",
    []string{"DATA//<array>//id"},
    []string{"DATA//<array>//age},
    true,  // <-------
)  

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comparator

type Comparator struct {
	DiffLogger

	IgnoreTypes bool
	ProgressBar bool
	// contains filtered or unexported fields
}

func NewComparator

func NewComparator(lPath, rPath string, ignoreT bool) *Comparator

Comparator for JSON-files located at lPath and rPath.

If ignoreT set true JSONComparator will ignore type differences between similar values like "1.4" string and 1.4 float.

func NewComparatorWithKeys

func NewComparatorWithKeys(lPath, rPath string, keys, ignoreKeys []string, ignoreT bool) *Comparator

Comparator for JSON-files located at lPath and rPath.

Uses keys and ignoreKeys to properly compare objects nested in an array. For example, you have: {"cats": [{"id": 4, "name": "Nyan"}, {"id": 2, "name": "Marx"}]}. If you want to set cat's "id" as a key, add "DATA.cats.<array>.id" to keys. DATA points out to the root of your JSON and <array> indicates object with key is nested in an array. ignoreKeys are used to ignore mismatches for particular keys. If ignoreT set true JSONComparator will ignore type differences between similar values like "1.4" string and 1.4 float.

func (*Comparator) CompWithLeft

func (c *Comparator) CompWithLeft()

Looks for how the left JSON-file differs from the left one.

func (*Comparator) CompWithRight

func (c *Comparator) CompWithRight()

Looks for how the right JSON-file differs from the right one.

func (*Comparator) CompareFull

func (c *Comparator) CompareFull()

Looks for differences between files from both perspectives.

func (*Comparator) SaveDiffLogs

func (c *Comparator) SaveDiffLogs()

Saves logs for last performed comparison as a text file.

func (*Comparator) TargetKeyValMap

func (c *Comparator) TargetKeyValMap(src interface{}, keys []string) map[string]interface{}

type DiffLogger

type DiffLogger struct {
	Log []string
	// contains filtered or unexported fields
}

func (*DiffLogger) Summary

func (l *DiffLogger) Summary() string

Returns formatted summary for last performed comparison.

type Logger

type Logger interface {
	GetSummary() string
}

Jump to

Keyboard shortcuts

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