aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 72d4d2b436d333e93f7ff945b24345e9e6da41a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# JSON parsing library

This library is an marshaler/unmarshaler for JSON in a tree of nodes. Also allows you to make queries over these trees.

## Library interface

```go
package json // import "go.neonxp.dev/json"

// Marshal Node tree to []byte
func Marshal(node *model.Node) ([]byte, error)

// Unmarshal data to Node tree
func Unmarshal(data []byte) (*model.Node, error)

// Query returns node by query string (dot notation)
func Query(json string, query string) (*model.Node, error)

// QueryArray returns node by array query
func QueryArray(json string, query []string) (*model.Node, error)
```

## Node methods

```go
package model // import "go.neonxp.dev/json/model"

// Node of JSON tree
type Node struct {
    Type NodeType
}

// NewNode creates new node from value
func NewNode(value any) *Node

// Get node from object by key
func (n *Node) Get(key string) (*Node, error)

// Index returns node by index from array
func (n *Node) Index(idx int) (*Node, error)

// Set node to object by key
func (n *Node) Set(key string, value *Node) error

// SetIndex sets node to array by index
func (n *Node) SetIndex(idx int, value *Node) error

// SetValue to node
func (n *Node) SetValue(value any)

// Map callback to each key value pair of object
func (n *Node) Map(cb func(key string, value *Node) (*Node, error)) error

// Each applies callback to each element of array
func (n *Node) Each(cb func(idx int, value *Node) error) error

// Query returns node by array query
func (n *Node) Query(query []string) (*Node, error)

// Value returns value of node
func (n *Node) Value() any

// MarshalJSON to []byte
func (n *Node) MarshalJSON() ([]byte, error)

// Merge two object or array nodes
func (n *Node) Merge(node *Node) error

// Len returns length of object or array nodes
func (n *Node) Len() (int, error)

// Compare current node with another node
func (n *Node) Compare(op Operand, node *Node) bool

// Remove by key from object
func (n *Node) Remove(key string) error 

// RemoveIndex from array
func (n *Node) RemoveIndex(idx int) error
```