Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidBytes ¶ added in v1.0.1
ValidBytes returns true if s is valid JSON, otherwise returns false.
Types ¶
type Error ¶
func Get ¶
Get calls fn for the value at the given path. The value path is defined by keys separated by a dot and index access operators for arrays. If no value is found for the given path then fn isn't called and no error is returned. If escapePath then all dots and square brackets are expected to be escaped.
WARNING: Fields exported by *Iterator provided in fn must not be mutated! Do not use or alias *Iterator after fn returns!
Example ¶
package main import ( "fmt" "github.com/romshark/jscan" ) func main() { j := `[false,[[2, {"[escaped]":[{"test-key":"string value"}]}]]]` if err := jscan.Get( j, `[1][0][1].\[escaped\][0].test-key`, true, func(i *jscan.Iterator) { fmt.Println(i.Value()) }, ); err.IsErr() { fmt.Printf("ERR: %s\n", err) return } }
Output: string value
func Scan ¶
Scan calls fn for every scanned value including objects and arrays. Scan returns true if there was an error or if fn returned true, otherwise it returns false. If cachePath == true then paths are generated and cached on the fly reducing their performance penalty.
WARNING: Fields exported by *Iterator provided in fn must not be mutated! Do not use or alias *Iterator after fn returns!
Example ¶
package main import ( "fmt" "github.com/romshark/jscan" ) func main() { j := `{ "s": "value", "t": true, "f": false, "0": null, "n": -9.123e3, "o0": {}, "a0": [], "o": { "k": "\"v\"", "a": [ true, null, "item", -67.02e9, ["foo"] ] }, "a3": [ 0, { "a3.a3":8 } ] }` err := jscan.Scan(jscan.Options{ CachePath: true, EscapePath: true, }, j, func(i *jscan.Iterator) (err bool) { fmt.Printf("| value:\n") fmt.Printf("| level: %d\n", i.Level) if k := i.Key(); k != "" { fmt.Printf("| key: %q\n", i.Key()) } fmt.Printf("| valueType: %s\n", i.ValueType) if v := i.Value(); v != "" { fmt.Printf("| value: %q\n", i.Value()) } fmt.Printf("| arrayIndex: %d\n", i.ArrayIndex) fmt.Printf("| path: '%s'\n", i.Path()) return false // No Error, resume scanning }) if err.IsErr() { fmt.Printf("ERR: %s\n", err) return } }
Output: | value: | level: 0 | valueType: object | arrayIndex: -1 | path: '' | value: | level: 1 | key: "s" | valueType: string | value: "value" | arrayIndex: -1 | path: 's' | value: | level: 1 | key: "t" | valueType: true | value: "true" | arrayIndex: -1 | path: 't' | value: | level: 1 | key: "f" | valueType: false | value: "false" | arrayIndex: -1 | path: 'f' | value: | level: 1 | key: "0" | valueType: null | value: "null" | arrayIndex: -1 | path: '0' | value: | level: 1 | key: "n" | valueType: number | value: "-9.123e3" | arrayIndex: -1 | path: 'n' | value: | level: 1 | key: "o0" | valueType: object | arrayIndex: -1 | path: 'o0' | value: | level: 1 | key: "a0" | valueType: array | arrayIndex: -1 | path: 'a0' | value: | level: 1 | key: "o" | valueType: object | arrayIndex: -1 | path: 'o' | value: | level: 2 | key: "k" | valueType: string | value: "\\\"v\\\"" | arrayIndex: -1 | path: 'o.k' | value: | level: 2 | key: "a" | valueType: array | arrayIndex: -1 | path: 'o.a' | value: | level: 3 | valueType: true | value: "true" | arrayIndex: 0 | path: 'o.a[0]' | value: | level: 3 | valueType: null | value: "null" | arrayIndex: 1 | path: 'o.a[1]' | value: | level: 3 | valueType: string | value: "item" | arrayIndex: 2 | path: 'o.a[2]' | value: | level: 3 | valueType: number | value: "-67.02e9" | arrayIndex: 3 | path: 'o.a[3]' | value: | level: 3 | valueType: array | arrayIndex: 4 | path: 'o.a[4]' | value: | level: 4 | valueType: string | value: "foo" | arrayIndex: 0 | path: 'o.a[4][0]' | value: | level: 1 | key: "a3" | valueType: array | arrayIndex: -1 | path: 'a3' | value: | level: 2 | valueType: number | value: "0" | arrayIndex: 0 | path: 'a3[0]' | value: | level: 2 | valueType: object | arrayIndex: 1 | path: 'a3[1]' | value: | level: 3 | key: "a3.a3" | valueType: number | value: "8" | arrayIndex: -1 | path: 'a3[1].a3\.a3'
Example (Error_handling) ¶
package main import ( "fmt" "github.com/romshark/jscan" ) func main() { j := `"something...` err := jscan.Scan(jscan.Options{}, j, func(i *jscan.Iterator) (err bool) { fmt.Println("This shall never be executed") return false // No Error, resume scanning }) if err.IsErr() { fmt.Printf("ERR: %s\n", err) return } }
Output: ERR: error at index 13: unexpected EOF
type ErrorBytes ¶ added in v1.0.1
func GetBytes ¶ added in v1.0.1
func GetBytes( s, path []byte, escapePath bool, fn func(*IteratorBytes), ) ErrorBytes
Get calls fn for the value at the given path. The value path is defined by keys separated by a dot and index access operators for arrays. If no value is found for the given path then fn isn't called and no error is returned. If escapePath then all dots and square brackets are expected to be escaped.
WARNING: Fields exported by *IteratorBytes in fn must not be mutated! Do not use or alias *IteratorBytes after fn returns!
func ScanBytes ¶ added in v1.0.1
func ScanBytes( o Options, s []byte, fn func(*IteratorBytes) (err bool), ) ErrorBytes
ScanBytes calls fn for every scanned value including objects and arrays. Scan returns true if there was an error or if fn returned true, otherwise it returns false. If cachePath == true then paths are generated and cached on the fly reducing their performance penalty.
WARNING: Fields exported by *IteratorBytes in fn must not be mutated! Do not use or alias *IteratorBytes after fn returns!
func ValidateBytes ¶ added in v1.0.1
func ValidateBytes(s []byte) ErrorBytes
ValidateBytes returns an error if s is invalid JSON.
func (ErrorBytes) Error ¶ added in v1.0.1
func (e ErrorBytes) Error() string
func (ErrorBytes) IsErr ¶ added in v1.0.1
func (e ErrorBytes) IsErr() bool
IsErr returns true if there is an error, otherwise returns false.
type ErrorCode ¶
type ErrorCode int8
ErrorCode defines the error type.
const ( ErrorCodeInvalidEscapeSeq ErrorCode ErrorCodeIllegalControlChar ErrorCodeUnexpectedEOF ErrorCodeUnexpectedToken ErrorCodeMalformedNumber ErrorCodeCallback )
All error codes
type Iterator ¶
type Iterator struct { ValueType ValueType Level int KeyStart, KeyEnd, KeyLenEscaped int ValueStart, ValueEnd int ArrayIndex int // contains filtered or unexported fields }
Iterator provides access to the recently scanned value.
type IteratorBytes ¶ added in v1.0.1
type IteratorBytes struct { ValueType ValueType Level int KeyStart, KeyEnd, KeyLenEscaped int ValueStart, ValueEnd int ArrayIndex int // contains filtered or unexported fields }
IteratorBytes provides access to the recently scanned value.
func (*IteratorBytes) Key ¶ added in v1.0.1
func (i *IteratorBytes) Key() []byte
Key returns the field key if any.
func (*IteratorBytes) Path ¶ added in v1.0.1
func (i *IteratorBytes) Path() (s []byte)
Path returns the stringified path.
func (*IteratorBytes) ScanPath ¶ added in v1.0.1
func (i *IteratorBytes) ScanPath(fn func(keyStart, keyEnd, arrIndex int))
ScanPath calls fn for every element in the path of the value. If keyStart is != -1 then the element is a field value, otherwise arrIndex indicates the index of the item in the underlying array.
func (*IteratorBytes) Value ¶ added in v1.0.1
func (i *IteratorBytes) Value() []byte
Value returns the value if any.
func (*IteratorBytes) ViewPath ¶ added in v1.0.1
func (i *IteratorBytes) ViewPath(fn func(p []byte))
ViewPath calls fn and provides the stringified path.
WARNING: do not use or alias p after fn returns! Only viewing or copying are considered safe! Use (*IteratorBytes).Path instead for a safer and more convenient API.