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 ¶
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.
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 ¶
NewDocument creates a new empty document.
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 ¶
ValueProcessor describes a function for the processing of values while iterating over a document.