Documentation ¶
Overview ¶
Package jsonparser_airp encodes and decodes JSON.
In contrast to encoding/json airp is centered around an AST (Abstract Syntax Tree) model. An AST can be manipulated and new nodes can be created. Every non error-node is valid JSON.
airp is partly comartible with encoding/json. Node fulfills the json.Marshaler/Unmarshaler interfaces.
Some differences between this package and encoding/json:
- Empty arrays or objects will be represented by their empty types ([]/{}) instead of null
- bytes slices will be interpreded as strings instead of as base64 encoded data
TODO(JMH): better handle escape sequences TODO(JMH): reimplement lexer and parser
Index ¶
- Variables
- func EqNode(a, b *Node) bool
- func Marshal(v interface{}) ([]byte, error)
- func Unmarshal(data []byte, v interface{}) (err error)
- type JSONType
- type KeyNode
- type Node
- func (n *Node) AddChildren(nn ...KeyNode)
- func (n *Node) Copy() *Node
- func (n *Node) GetChild(name string) (*Node, bool)
- func (n *Node) GetChildrenKeys() []string
- func (n *Node) JSON2Go(val interface{}) (err error)
- func (n *Node) Key() string
- func (n *Node) Len() int
- func (n *Node) MarshalJSON() ([]byte, error)
- func (n *Node) RemoveChild(key string) error
- func (n *Node) SetChild(kn KeyNode) error
- func (n *Node) String() string
- func (n *Node) Total() int
- func (n *Node) Type() JSONType
- func (n *Node) UnmarshalJSON(data []byte) error
- func (n *Node) Value() (interface{}, error)
- func (n *Node) WriteIndent(w io.Writer, indent string) (int, error)
- func (n *Node) WriteJSON(w io.Writer) (int, error)
- type ParseError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotArrayOrObject = errors.New("not array or object")
ErrNotArrayOrObject is a common error that multiple methods of Node or KeyNode return. This signals that the Node type is a standalone value.
Functions ¶
Types ¶
type JSONType ¶
type JSONType uint8
JSONType is an enum for any JSON-types
JSONTypes to caompare nodes of an ast with. The zero value signals invalid.
type KeyNode ¶ added in v0.9.11
func StandaloneNode ¶
StandaloneNode generates a single json value of str. It panics if str is a compund json expression.
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node is one node of a tree building a JSON-AST. Depending on its internal type it holds a different value:
JSONType ValueType Error nil Null nil Bool bool Number float64 String string Array []*Node Object []KeyNode
func NewJSONGo ¶
NewJSONGo reads in a Go-value and generates a json ast that can be manipulated easily.
func NewJSONReader ¶ added in v0.9.13
NewJSONReader reads from r and generates an AST
func NewJSONString ¶ added in v0.9.13
NewJSONString reads from s and generates an AST
func (*Node) AddChildren ¶
AddChildren appends nn nodes to the Array or Object n. It panics if n is not of the two mentioned types or if appended values in an object don't have keys.
func (*Node) GetChild ¶
GetChild returns the node specifiend by name. The key "" always returns the node itself.
func (*Node) GetChildrenKeys ¶
GetChildrenKeys returns a slice of all keys an Object or array holds. It is nil if n is not array or object and is not nil but is non-nil with a lengh of 0 if n is an empty array or object.
func (*Node) JSON2Go ¶
JSON2Go reads contents from n and writes them into val. val has to be a pointer value and may panic if types don't match.
func (*Node) MarshalJSON ¶
MarshalJSON implements the json.Mashaler interface for Node
Example ¶
package main import ( "fmt" airp "github.com/d1ced/jsonparser_airp" ) func main() { n := airp.StandaloneNode("", "{}") m := airp.StandaloneNode("Num", "3.125") o := airp.StandaloneNode("Str", `"Hello, World!"`) n.AddChildren(m, o) data, _ := n.MarshalJSON() fmt.Printf("%s", data) }
Output: {"Num": 3.125, "Str": "Hello, World!"}
func (*Node) RemoveChild ¶
RemoveChild removes key from the ast corrctly reducing arrays
func (*Node) SetChild ¶
SetChild adds or replaces the child key of n with val. SetChild does not add multiple level of objects. SetChild panics a to extended object is not array or object.
func (*Node) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmashaler interface for Node
Example ¶
package main import ( "fmt" airp "github.com/d1ced/jsonparser_airp" ) func main() { data := []byte(`{"a": 20, "b": [true, null]}`) root := airp.Node{} _ = root.UnmarshalJSON(data) // root now holds the top of the JSON ast. fmt.Println(root.String()) }
Output: {"a":20,"b":[true,null]}
func (*Node) Value ¶
Value creates the Go representation of a JSON-Node. Like encoding/json the possible underlying types of the first return parameter are:
Object map[string]interface{} Array []interface{} String string Number float64 Bool bool Null nil (with the error being nil too)
Example ¶
package main import ( "fmt" airp "github.com/d1ced/jsonparser_airp" ) func main() { data := []byte(`[{"a": null}, true]`) root := airp.Node{} _ = root.UnmarshalJSON(data) v, _ := root.Value() fmt.Println(v) }
Output: [map[a:<nil>] true]
func (*Node) WriteIndent ¶
WriteIndent writes the AST hold by n to w with the given indent (preferably spaces or a tab).
type ParseError ¶
type ParseError struct {
// contains filtered or unexported fields
}
ParseError captures information on errors when parsing.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
func (*ParseError) Where ¶
func (e *ParseError) Where() (row, col int)
Where returns the row and column where the syntax error in json occured.