aboutsummaryrefslogtreecommitdiff
path: root/json.go
blob: 26fcad59725c26a5c3762c0a3a8fe46db229e33c (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
package json

import (
	"strings"

	"go.neonxp.dev/json/model"
	"go.neonxp.dev/json/parser"
)

// Marshal Node tree to []byte
func Marshal(node model.Node) ([]byte, error) {
	return node.MarshalJSON()
}

// Unmarshal data to Node tree
func Unmarshal(data []byte) (model.Node, error) {
	return parser.Parse(string(data))
}

// Query returns node by query string (dot notation)
func Query(json string, query string) (model.Node, error) {
	n, err := parser.Parse(json)
	if err != nil {
		return nil, err
	}
	return model.Query(n, strings.Split(query, "."))
}

// QueryArray returns node by array query
func QueryArray(json string, query []string) (model.Node, error) {
	n, err := parser.Parse(json)
	if err != nil {
		return nil, err
	}
	return model.Query(n, query)
}