Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "github.com/alxarch/njson" ) func main() { d := njson.Document{} root, _, _ := d.Parse(`{"answer":42, "foo": {"bar": "baz"}}`) answer, _ := root.Get("answer").ToInt() fmt.Println(answer) n := root.Lookup("foo", "bar") bar := n.Unescaped() fmt.Println(bar) n.SetString("Hello, 世界") data := make([]byte, 64) data, _ = root.AppendJSON(data[:0]) fmt.Println(string(data)) }
Output: 42 baz {"answer":42,"foo":{"bar":"Hello, 世界"}}
Index ¶
- func PrintJSON(w io.Writer, a Appender) (n int, err error)
- type Appender
- type Document
- func (d *Document) AppendJSON(dst []byte) ([]byte, error)
- func (d *Document) Array() Node
- func (d *Document) Close()
- func (d *Document) False() Node
- func (d *Document) Node(id uint) Node
- func (d *Document) Null() Node
- func (d *Document) Number(f float64) Node
- func (d *Document) Object() Node
- func (d *Document) Parse(s string) (Node, string, error)
- func (d *Document) Reset()
- func (d *Document) Root() Node
- func (d *Document) Text(s string) Node
- func (d *Document) TextHTML(s string) Node
- func (d *Document) TextRaw(s string) Node
- func (d *Document) True() Node
- type IterV
- type Node
- func (n Node) Append(values ...Node)
- func (n Node) AppendJSON(dst []byte) ([]byte, error)
- func (n Node) Data() (string, Type)
- func (n Node) Del(key string)
- func (n Node) Document() *Document
- func (n Node) Get(key string) Node
- func (n Node) ID() uint
- func (n Node) Index(i int) Node
- func (n Node) Lookup(path ...string) Node
- func (n Node) PrintJSON(w io.Writer) (int, error)
- func (n Node) Raw() string
- func (n Node) Remove(i int)
- func (n Node) Replace(i int, value Node)
- func (n Node) Set(key string, value Node)
- func (n Node) SetFalse()
- func (n Node) SetFloat(f float64)
- func (n Node) SetInt(i int64)
- func (n Node) SetNull()
- func (n Node) SetString(s string)
- func (n Node) SetStringHTML(s string)
- func (n Node) SetStringRaw(s string)
- func (n Node) SetTrue()
- func (n Node) SetUint(u uint64)
- func (n Node) Slice(i, j int)
- func (n Node) Strip(key string)
- func (n Node) ToBool() (bool, bool)
- func (n Node) ToFloat() (float64, bool)
- func (n Node) ToInt() (int64, bool)
- func (n Node) ToInterface() (interface{}, bool)
- func (n Node) ToUint() (uint64, bool)
- func (n Node) Type() Type
- func (n Node) TypeError(want Type) error
- func (n Node) Unescaped() string
- func (n Node) Values() IterV
- func (n Node) With(id uint) Node
- func (n Node) WrapUnmarshalJSON(u json.Unmarshaler) (err error)
- func (n Node) WrapUnmarshalText(u encoding.TextUnmarshaler) (err error)
- type ParseError
- type Pool
- type Type
- type UnexpectedEOF
- type Unmarshaler
- type V
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document is a JSON document.
func Blank ¶
func Blank() *Document
Blank returns a blank document from the default pool. Use Document.Close() to reset and return the document to the pool.
func (*Document) AppendJSON ¶
AppendJSON appends the JSON data of the document root node to a byte slice.
func (*Document) Close ¶
func (d *Document) Close()
Close resets and returns the document to the default pool to be reused.
func (*Document) False ¶
False adds a new Boolean node with it's value set to false to the document.
func (*Document) Text ¶
Text adds a new String node to the document escaping JSON unsafe characters.
func (*Document) TextHTML ¶
TextHTML adds a new String node to the document escaping HTML and JSON unsafe characters.
type IterV ¶
type IterV struct { V // contains filtered or unexported fields }
IterV is an iterator over a node's values.
func (*IterV) Index ¶
Index returns the current iteration index. Before Next() is called for the first time it returns -1. After the iteration has finished it returns -2.
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node is a reference to a node in a JSON Document. It is a versioned reference to avoid document manipulation after reset.
func (Node) AppendJSON ¶
AppendJSON appends a node's JSON data to a byte slice.
func (Node) Del ¶
Del finds a key in an Object node's values and removes it. It does not keep the order of keys.
func (Node) Get ¶
Get gets a Node by key. If the key is not found the returned node's id will be MaxID and the Node will behave as empty.
func (Node) Index ¶
Index gets the Node at offset i of an Array. If the index is out of bounds it sets the id to MaxUint and the Node will behave as empty but still have a usable reference to Document.
func (Node) Raw ¶
Raw returns the JSON string of a Node's value. Object and Array nodes return an empty string.
func (Node) Set ¶
Set assigns a Node to the key of an Object Node. Since most keys need no escaping it doesn't escape the key. If the key needs escaping use strjson.Escaped.
func (Node) SetStringHTML ¶
SetStringHTML sets a Node's value to a string escaping invalid JSON and unsafe HTML characters.
func (Node) SetStringRaw ¶
SetStringRaw sets a Node's value to a string without escaping. Unless the provided string is guaranteed to not contain any JSON invalid characters, JSON output from this Node will be invalid.
func (Node) ToInterface ¶
ToInterface converts a Node to a generic interface{}.
func (Node) WrapUnmarshalJSON ¶
func (n Node) WrapUnmarshalJSON(u json.Unmarshaler) (err error)
WrapUnmarshalJSON wraps a call to the json.Unmarshaler interface
func (Node) WrapUnmarshalText ¶
func (n Node) WrapUnmarshalText(u encoding.TextUnmarshaler) (err error)
WrapUnmarshalText wraps a call to the encoding.TextUnmarshaler interface
type ParseError ¶ added in v0.0.9
type ParseError struct {
// contains filtered or unexported fields
}
ParseError signifies an invalid token in JSON data
func (*ParseError) Error ¶ added in v0.0.9
func (e *ParseError) Error() string
func (*ParseError) Pos ¶ added in v0.0.9
func (e *ParseError) Pos() int
Pos returns the offset at which the error ocurred
func (*ParseError) Type ¶ added in v0.0.9
func (e *ParseError) Type() Type
Type returns type of value that was being parsed when the error ocurred.
type Pool ¶ added in v0.0.5
type Pool struct {
// contains filtered or unexported fields
}
Pool is a pool of document objects
type Type ¶
type Type uint8
Type is the type of a node.
const ( TypeInvalid Type = iota TypeString Type = 1 << iota TypeObject TypeArray TypeNumber TypeBoolean TypeNull TypeAnyValue = TypeString | TypeNumber | TypeBoolean | TypeObject | TypeArray | TypeNull )
Token types and type masks
type UnexpectedEOF ¶ added in v0.0.9
type UnexpectedEOF Type
UnexpectedEOF signifies incomplete JSON data
func (UnexpectedEOF) Error ¶ added in v0.0.9
func (e UnexpectedEOF) Error() string
type Unmarshaler ¶
Unmarshaler is the interface implemented by types that can unmarshal from a Node.
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
internal
|
|
Package unjson uses reflection to marshal/unmarshal JSON from `njson.Node` input It is combatible with the default `encoding/json` package but the API is different so it's *not* a 'drop-in' replacement.
|
Package unjson uses reflection to marshal/unmarshal JSON from `njson.Node` input It is combatible with the default `encoding/json` package but the API is different so it's *not* a 'drop-in' replacement. |