jsonom

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: Apache-2.0 Imports: 5 Imported by: 2

README

GoDoc Go Report Card

JSON Object Model

This is a Go package to process JSON documents with an API similar to XML DOM API. It represents the JSON document as a tree of Nodes. The basic JSON types are:

  • Object is a key-value pair, where value is another node,
  • Array is an ordered value list,
  • Value is a JSON value node containing null, string, boolean, or a number

When a JSON document is unmarshaled, primitive values are converted to the following Go types:

  • JSON number values are converted json.Number
  • JSON strings are converted to Go strings
  • JSON boolean values are converted to Go boolean
  • Null is converted to nil

This library preserves the ordering of keys in a JSON object.

Key Interning

In JSON documents, the objects keys tend to repeat. For large JSON documents, it makes sense to "intern" these keys in a hashmap so a single string copy is used for all instances of a string value.

JSON unmarshaling functions get an Interner argument. This can be used to keep a common interner when processing multiple documents. If a nil interner is passed to these functions, an new interner will be used to store the keys of that document only.

Example

func main() {
      input:=`{"key":"value", "arr": [ 1,2 ]}`
      j, err:=jsonom.Unmarshal([]byte(input),nil)
      if err!=nil {
         panic(err)
      }
      obj:=j.(*jsonom.Object)
      v,_:=obj.Value("key")
      fmt.Println(v.(*jsonom.Value).Value())
      a,_:=obj.Value("arr")
      arr:=a.(*jsonom.Array)
      fmt.Println(arr.Len())
      arr.Append(jsonom.NewValue(3))
      fmt.Println(arr.Len())
      j.Encode(os.Stdout)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Find added in v1.2.0

func Find(node Node, path gojsonpath.Path) ([]any, error)

func ParseAndFind added in v1.2.0

func ParseAndFind(node Node, path string) ([]any, error)

ParseAndFind parses the path and finds matching nodes in the doc

func Search(node Node, path gojsonpath.Path, capture func(gojsonpath.DocPath)) error

Search iterates all document nodes depth-first, and calls `capture` for those document nodes that `path` matches.

Types

type Array

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

Array represents a JSON array

func NewArray

func NewArray(el ...Node) *Array

NewArray returns an array with the given nodes

func (*Array) Append

func (a *Array) Append(values ...Node)

Append new values to the array

func (*Array) Clear

func (a *Array) Clear()

Clear all nodes of an array

func (*Array) Each added in v1.1.0

func (a *Array) Each(f func(int, Node) bool) bool

Call f for each index-value until it returns false

func (*Array) Encode

func (a *Array) Encode(w io.Writer) error

Encode array as JSON

func (*Array) Len

func (a *Array) Len() int

Len returns the number of elements in the array

func (*Array) Marshal

func (a *Array) Marshal() interface{}

Marshal returns a []interface{} for the array, where each element is recursively marshaled

func (*Array) MarshalJSON

func (a *Array) MarshalJSON() ([]byte, error)

MarshalJSON allows using json.Marshal for an array node

func (*Array) N

func (a *Array) N(index int) Node

N returns the n'th element of the array

func (*Array) Remove

func (a *Array) Remove(index int)

Remove the node at the given index

func (*Array) Set

func (a *Array) Set(index int, value Node)

Set the node at the given index

type KeyValue

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

KeyValue represents a JSON key-value pair in an Object

func NewKeyValue

func NewKeyValue(key string, value Node) *KeyValue

NewKeyValue returns a new key-value pair

func (*KeyValue) Encode

func (k *KeyValue) Encode(w io.Writer) error

Encode a key-value pair

func (*KeyValue) Key

func (k *KeyValue) Key() string

Key returns the key of the key-value pair

func (*KeyValue) Set

func (k *KeyValue) Set(value Node)

Set sets the value of the key-value pair

func (*KeyValue) Value

func (k *KeyValue) Value() Node

Value returns the value of the key-value pair

type MapInterner

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

MapInterner uses a map to keep single copies of strings. Empty value of DefaultInterner is ready to use

func (*MapInterner) Intern

func (interner *MapInterner) Intern(s string) string

Intern updates the internal string table to include the given string

type Node

type Node interface {
	// Marshal a node to one of: map[string]interface{}, []interface{},
	// or one of the JSON value types: nil, string, bool, or json.Number
	Marshal() interface{}
	// Encode the node into a JSON document
	Encode(io.Writer) error
	// contains filtered or unexported methods
}

Node is a node in a JSON object model

func Decode

func Decode(decoder *json.Decoder, interner StringInterner) (Node, error)

Decode a JSON object using the given decoder. Interner is optional, it will be used if given. If omitted, an internal temporary interner will be used.

func Unmarshal

func Unmarshal(input []byte, interner StringInterner) (Node, error)

Unmarshal the given byte slice to a json object model node

func UnmarshalIntf

func UnmarshalIntf(input interface{}) (Node, error)

UnmarshalIntf creates a JSON object model from a tree of objects, as output from json.Marshal

func UnmarshalReader

func UnmarshalReader(input io.Reader, interner StringInterner) (Node, error)

UnmarshalReader unmarshals the input to a json object model node

type Object

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

Object represents a JSON object, an ordered set of key-value pairs. The zero-value of an Object is ready to use

func NewObject

func NewObject(kv ...*KeyValue) *Object

NewObject creates a new object from the given key-value pairs

func (*Object) AddOrSet

func (o *Object) AddOrSet(kv *KeyValue) bool

AddOrSet adds a key-value pair if the key does not exist in this object, or replaces an existing key-value pair with the given one. Returns true if the key is added, false if it is updated

func (*Object) Clear

func (o *Object) Clear()

Clear the contents of an object

func (*Object) Each added in v1.1.0

func (o *Object) Each(f func(*KeyValue) bool) bool

Call f for each key-value until it returns false

func (*Object) Encode

func (o *Object) Encode(w io.Writer) error

Encode a json object

func (*Object) Get

func (o *Object) Get(key string) *KeyValue

Get returns a key-value pair by its key. If the key does not exist, returns nil

func (*Object) Len

func (o *Object) Len() int

Len returns the number of key-value pairs in the object

func (*Object) Marshal

func (o *Object) Marshal() interface{}

Marshal returns a map[string]interface{} for the object where each value is recursively marshaled. This operation loses the ordering of the object elements.

func (*Object) MarshalJSON

func (o *Object) MarshalJSON() ([]byte, error)

MarshalJSON allows using json.Marshal for an object node

func (*Object) N

func (o *Object) N(index int) *KeyValue

N returns the n'th key-value pair by index

func (*Object) Remove

func (o *Object) Remove(key string) bool

Remove a key from the object. Returns true if the key was in the object, and removed

func (*Object) Set

func (o *Object) Set(key string, value Node) bool

Set the key to the given value. If the key already exists, it is replaced in place. Otherwise it is appended

func (*Object) Value

func (o *Object) Value(key string) (Node, bool)

Value returns the value of a key-value pair by its key. If the key does not exist, returns nil,false

type PathModel added in v1.2.0

type PathModel struct {
	Node
}

PathModel is the adapter for Node to use in JSON Path lookups

func (PathModel) Elem added in v1.2.0

func (PathModel) Elem(node any, index int) any

func (PathModel) Key added in v1.2.0

func (PathModel) Key(node any, key string) (any, bool)

func (PathModel) Keys added in v1.2.0

func (PathModel) Keys(node any) []string

func (PathModel) Len added in v1.2.0

func (PathModel) Len(in any) int

func (PathModel) Root added in v1.2.0

func (m PathModel) Root() any

func (PathModel) Type added in v1.2.0

func (PathModel) Type(node any) gojsonpath.NodeType

func (PathModel) Value added in v1.2.0

func (PathModel) Value(node any) any

type StringInterner

type StringInterner interface {
	Intern(string) string
}

StringInterner is used to reduce the memory footprint of a json document by interning the keys, so the same copy of string is used throughout the file

type Value

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

A Value is one of: nil, string, bool, json.Number

func BoolValue

func BoolValue(v bool) *Value

BoolValue creates a new value from a boolean value

func NewValue

func NewValue(value interface{}) *Value

NewValue returns a new value. The value must be convertible to one of: nil, string, bool, or json.Number

func NullValue

func NullValue() *Value

NullValue returns a new null value

func StringValue

func StringValue(str string) *Value

StringValue creates a new Value from a string

func (Value) Encode

func (v Value) Encode(w io.Writer) error

Encode a value

func (Value) Marshal

func (v Value) Marshal() interface{}

Marshal a value to interface{}

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

MarshalJSON allows using json.Marshal for a value node

func (*Value) Set

func (v *Value) Set(val interface{})

Set sets the value

func (Value) String

func (v Value) String() string

func (Value) Value

func (v Value) Value() interface{}

Value returns the value

Jump to

Keyboard shortcuts

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