Documentation
¶
Index ¶
- Constants
- func IterateArray(ctx context.Context, r io.Reader) iter.Seq2[json.RawMessage, error]
- func Query(r io.Reader, path ...Kind) iter.Seq2[json.RawMessage, error]
- func QueryMany(r io.Reader, selectors ...Selector) error
- func TrimSpace(data []byte) []byte
- type ArrayIterator
- type Input
- type Kind
- type KindElement
- type KindValue
- type LexingError
- type Output
- type Path
- type Scanner
- type Selector
Examples ¶
Constants ¶
View Source
const ( KindString strKind = "string" KindNumber strKind = "number" KindBoolean strKind = "boolean" KindNull strKind = "null" )
View Source
const ( KindObject strKind = "object" KindName strKind = "name" )
View Source
const ErrMalformed errorkitlite.Error = "[ErrMalformed] malformed JSON"
View Source
const KindArray strKind = "array"
Variables ¶
This section is empty.
Functions ¶
func IterateArray ¶
func Query ¶
Query will turn the input reader into a json visitor that yields results when a path is matching. Think about it something similar as jq. It will not keep the visited json i n memory, to avoid problems with infinite streams.
Example ¶
package main
import (
"fmt"
"io"
"go.llib.dev/frameless/pkg/jsonkit/jsontoken"
)
func main() {
var body io.Reader
result := jsontoken.Query(body, jsontoken.KindArray, jsontoken.KindElement{})
for rawJSON, err := range result {
if err != nil {
fmt.Println(err.Error())
continue
}
fmt.Println(string(rawJSON))
}
}
func QueryMany ¶ added in v0.304.0
Example (StructuredLexing) ¶
package main
import (
"io"
"go.llib.dev/frameless/pkg/jsonkit/jsontoken"
"go.llib.dev/frameless/pkg/pointer"
)
func main() {
var jsonData io.Reader
jsontoken.QueryMany(jsonData, jsontoken.Selector{
Path: jsontoken.Path{
jsontoken.KindObject,
jsontoken.KindValue{Name: pointer.Of("values")},
jsontoken.KindArray,
jsontoken.KindElement{},
},
On: func(valuesElement io.Reader) error {
return jsontoken.QueryMany(valuesElement,
jsontoken.Selector{
Path: jsontoken.Path{
jsontoken.KindObject,
jsontoken.KindValue{Name: pointer.Of("foos")},
jsontoken.KindArray,
jsontoken.KindElement{},
},
On: func(fooElement io.Reader) error {
return nil
},
},
jsontoken.Selector{
Path: jsontoken.Path{
jsontoken.KindObject,
jsontoken.KindValue{Name: pointer.Of("bars")},
jsontoken.KindArray,
jsontoken.KindElement{},
},
On: func(barElement io.Reader) error {
return nil
},
},
)
},
})
}
Types ¶
type ArrayIterator ¶
type ArrayIterator struct {
Context context.Context
Input io.Reader
// contains filtered or unexported fields
}
func (*ArrayIterator) Close ¶
func (c *ArrayIterator) Close() error
func (*ArrayIterator) Decode ¶
func (c *ArrayIterator) Decode(ptr any) error
func (*ArrayIterator) Err ¶
func (c *ArrayIterator) Err() error
func (*ArrayIterator) Next ¶
func (c *ArrayIterator) Next() bool
func (*ArrayIterator) Value ¶
func (c *ArrayIterator) Value() json.RawMessage
type KindElement ¶ added in v0.304.0
type KindElement struct{ Index *int }
func (KindElement) Match ¶ added in v0.308.0
func (e KindElement) Match(oth Kind) bool
func (KindElement) String ¶ added in v0.304.1
func (e KindElement) String() string
type LexingError ¶ added in v0.304.0
func (LexingError) Error ¶ added in v0.304.0
func (err LexingError) Error() string
type Path ¶
type Path []Kind
type Scanner ¶
type Scanner struct {
// Selectors allows granual control on what should be kept during scanning of a JSON input stream.
// When no Selectors is set, the default is to keep everything.
Selectors []Selector
// BufferSize is the size of the reading buffer for each group of Selectors who match the same Selector#Path.
//
// default: 16 Mb
BufferSize iokit.ByteSize
}
Scanner is a streaming lexer, that allows
Click to show internal directories.
Click to hide internal directories.