jsonhelpers

package
v3.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2022 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Overview

Package jsonhelpers contains general-purpose functions for manipulating JSON.

These functions are not intended to be highly fast or efficient; they are designed for simplicity of use in test code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertEqual

func AssertEqual(t assert.TestingT, expected, actual any) bool

AssertEqual compares two JSON Value instances and returns true if they are deeply equal. If they are not equal, it outputs a test failure message describing the mismatch as specifically as possible.

The two values may either be pre-parsed JValue instances, or if they are not, they are converted using the same rules as JValueOf.

func CanonicalizeJSON

func CanonicalizeJSON(originalJSON []byte) []byte

CanonicalizeJSON reformats a JSON value so that object properties are alphabetized, making comparisons predictable and making it easier for a human reader to find a property.

func ToJSON

func ToJSON(value interface{}) []byte

ToJSON is just a shortcut for calling json.Marshal and taking only the first result.

func ToJSONString

func ToJSONString(value interface{}) string

ToJSONString calls json.Marshal and returns the result as a string.

Types

type JSONDiffElement

type JSONDiffElement struct {
	// Path represents the location of the data as a path from the root.
	Path JSONPath

	// Value1 is the JSON encoding of the value at that path in the data structure
	// that was passed to JSONDiff as json1. An empty string (as opposed to the JSON
	// representation of an empty string, `""`) means that this property was missing
	// in json1.
	Value1 string

	// Value2 is the JSON encoding of the value at that path in the data structure
	// that was passed to JSONDiff as json2. An empty string (as opposed to the JSON
	// representation of an empty string, `""`) means that this property was missing
	// in json2.
	Value2 string
}

JSONDiffElement describes a point of difference between two JSON data structures.

func (JSONDiffElement) Describe

func (e JSONDiffElement) Describe(value1Name, value2Name string) string

Describe returns a string description of this difference.

type JSONDiffResult

type JSONDiffResult []JSONDiffElement

JSONDiffResult is a list of JSONDiffElement values returned by JSONDiff.

func JSONDiff

func JSONDiff(json1, json2 []byte) (JSONDiffResult, error)

JSONDiff compares two JSON values and returns an explanation of how they differ, if at all, ignoring any differences that do not affect the value semantically (such as whitespace). This is for programmatic use; if you want a human-readable test assertion based on the same logic, see matchers.JSONEqual (which calls this function).

The two values are provided as marshalled JSON data. If they cannot be parsed, the function immediately returns an error.

If the values are deeply equal, the result is nil.

Otherwise, if they are both simple values, the result will contain a single JSONDiffElement.

If they are both JSON objects, JSONDiff will compare their properties. It will produce a JSONDiffElement for each property where they differ. For instance, comparing {"a": 1, "b": 2} with {"a": 1, "b": 3, "c": 4} will produce one element for "b" and one for "c". If a property contains an object value on both sides, the comparison will proceed recursively and may produce elements with subpaths (see JSONPath).

If they are both JSON arrays, and are of the same length, JSONDiff will compare their elements using the same rules as above. For JSON arrays of different lengths, if the shorter one matches every corresponding element of the longer one, it will return a JSONDiffElement pointing to the first element after the shorter one and listing the additional elements starting with a comma (for instance, comparing [10,20] with [10,20,30] will return a string of ",30" at index 2); otherwise it will just return both arrays in their entirety.

Values that are not of the same type will always produce a single JSONDiffElement describing the entire values.

func (JSONDiffResult) Describe

func (r JSONDiffResult) Describe(value1Name, value2Name string) []string

Describe returns a list of string descriptions of the differences.

type JSONPath

type JSONPath []JSONPathComponent

JSONPath represents the location of a node in a JSON data structure.

In a JSON object {"a":{"b":2}}, the nested "b":2 property would be referenced as JSONPath{{Property: "a"}, {Property: "b"}}.

In a JSON array ["a","b",["c"]], the "c" value would be referenced as JSONPath{{Index: 2},{Index: 0}}.

A nil or zero-length slice represents the root of the data.

func (JSONPath) String

func (p JSONPath) String() string

String returns a string representation of the path.

type JSONPathComponent

type JSONPathComponent struct {
	// Property is the name of an object property, or "" if this is in an array.
	Property string

	// Index is the zero-based index of an array element, if this is in an array.
	Index int
}

JSONPathComponent represents a location within the top level of a JSON object or array.

type JValue

type JValue struct {
	// contains filtered or unexported fields
}

JValue is a helper type for manipulating JSON data in tests. It validates that marshaled data is valid JSON, allows other data to be converted to JSON, and eliminates ambiguity as to whether a type like string or []byte in a test represents JSON or not.

func JValueOf

func JValueOf(value any) JValue

JValueOf creates a JValue based on any input type, as follows:

- If the input type is []byte, json.RawMessage, or string, it interprets the value as JSON. - If the input type is JValue, it returns the same value. - For any other type, it attempts to marshal the value to JSON.

If the input value is invalid, the returned JValue will have a non-nil Error().

func (JValue) Equal

func (v JValue) Equal(v1 JValue) bool

Equal returns true if the values are deeply equal.

func (JValue) Error

func (v JValue) Error() error

Error returns nil if the value is valid JSON, or else an error value describing the problem.

func (JValue) String

func (v JValue) String() string

String returns the JSON value as a string.

Jump to

Keyboard shortcuts

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