aboutsummaryrefslogtreecommitdiff
path: root/json.go
diff options
context:
space:
mode:
authorNeonXP <i@neonxp.dev>2022-11-16 05:11:19 +0300
committerNeonXP <i@neonxp.dev>2022-11-16 05:11:19 +0300
commita321bfe7b2f6db5078de7b2e5ed5ddcccd65f319 (patch)
treed11c187bceee610a7843463949df128569142680 /json.go
initial commit
Diffstat (limited to 'json.go')
-rw-r--r--json.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/json.go b/json.go
new file mode 100644
index 0000000..a0224cf
--- /dev/null
+++ b/json.go
@@ -0,0 +1,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 n.Query(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 n.Query(query)
+}