JSON parsing library
Go to file
NeonXP 59e421989e
Added metadata to node
2022-11-17 19:46:17 +03:00
model Added metadata to node 2022-11-17 19:46:17 +03:00
parser initial commit 2022-11-16 05:11:19 +03:00
LICENSE.md Fix md files 2022-11-16 05:12:20 +03:00
README.md Utility methods 2022-11-16 21:35:48 +03:00
go.mod initial commit 2022-11-16 05:11:19 +03:00
json.go initial commit 2022-11-16 05:11:19 +03:00
json_test.go initial commit 2022-11-16 05:11:19 +03:00

README.md

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

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

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