gjp

package
v4.24.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2017 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package gjp of the Tideland Go Library package provides the generic parsing and processing of JSON documents by paths. The returned values are typed, also a default has to be provided. The path separator for accessing can be defined when parsing a document.

doc, err := gjp.Parse(myDoc, "/")
if err != nil {
    ...
}
name := doc.ValueAt("name").AsString("")
street := doc.ValueAt("address/street").AsString("unknown")

The value passed to AsString() and the others are default values if there's none at the path. Another way is to create an empty document with

doc := gjp.NewDocument("::")

Here and at parsed documents values can be set with

err := doc.SetValueAt("a/b/3/c", 4711)

Additionally values of the document can be processed using

err := doc.Process(func(path string, value gjp.Value) error {
    ...
})

Sometimes one is more interested in the differences between two documents. Here

diff, err := gjp.Compare(firstDoc, secondDoc, "/")

privides a gjp.Diff instance which helps to compare individual paths of the two document.

Index

Constants

View Source
const (
	ErrUnmarshalling = iota + 1
	ErrInvalidDocument
	ErrCorruptingDocument
	ErrInvalidPart
	ErrInvalidPath
	ErrPathTooLong
	ErrProcessing
)

Error codes of the etc package.

Variables

This section is empty.

Functions

This section is empty.

Types

type Diff

type Diff interface {
	// FirstDocument returns the first document passed to Diff().
	FirstDocument() Document

	// SecondDocument returns the second document passed to Diff().
	SecondDocument() Document

	// Differences returns a list of paths where the documents
	// have different content.
	Differences() []string

	// DifferenceAt returns the differences at the given path by
	// returning the first and the second value.
	DifferenceAt(path string) (Value, Value)
}

Diff manages the two parsed documents and their differences.

func Compare

func Compare(first, second []byte, separator string) (Diff, error)

Compare parses and compares the documents and returns their differences.

func CompareDocuments

func CompareDocuments(first, second Document, separator string) (Diff, error)

CompareDocuments compares the documents and returns their differences.

type Document

type Document interface {
	json.Marshaler

	// Length returns the number of elements for the given path.
	Length(path string) int

	// SetValueAt sets the value at the given path.
	SetValueAt(path string, value interface{}) error

	// ValueAt returns the addressed value.
	ValueAt(path string) Value

	// Clear removes the so far build document data.
	Clear()

	// Query allows to find pathes matching a given pattern.
	Query(pattern string) (PathValues, error)

	// Process iterates over a document and processes its values.
	// There's no order, so nesting into an embedded document or
	// list may come earlier than higher level paths.
	Process(processor ValueProcessor) error
}

Document represents one JSON document.

func NewDocument

func NewDocument(separator string) Document

NewDocument creates a new empty document.

func Parse

func Parse(data []byte, separator string) (Document, error)

Parse reads a raw document and returns it as accessible document.

type PathValue

type PathValue struct {
	Path  string
	Value Value
}

PathValue is the combination of path and value.

type PathValues

type PathValues []PathValue

PathValues contains a number of path/value combinations.

type Value

type Value interface {
	fmt.Stringer

	// IsUndefined returns true if this value is undefined.
	IsUndefined() bool

	// AsString returns the value as string.
	AsString(dv string) string

	// AsInt returns the value as int.
	AsInt(dv int) int

	// AsFloat64 returns the value as float64.
	AsFloat64(dv float64) float64

	// AsBool returns the value as bool.
	AsBool(dv bool) bool

	// Equals compares a value with the passed one.
	Equals(to Value) bool
}

Value contains one JSON value.

type ValueProcessor

type ValueProcessor func(path string, value Value) error

ValueProcessor describes a function for the processing of values while iterating over a document.

Jump to

Keyboard shortcuts

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