jsontoken

package
v0.296.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

README

draft

how would you call when a json scanner that finds a valid raw encoded entity part within a json? In this example, I just called that as json field

For example, in this sample:

[{"key1":"value1","ary":[1, 2, 3]}]

the scanner would find:

  • path: ["array", "array-value", "object", "object-key"] raw: []byte("key1") kind: "string"
  • path: ["array", "array-value", "object", "object-value"] raw: []byte("value1") kind: "string" key: "key1"
  • path: ["array", "array-value", "object", "object-key"] raw: []byte("ary") kind: "string"
  • path: ["array", "array-value", "object", "object-value"] raw: []byte("[1, 2, 3]") kind: "array" key: "ary"
  • path: ["array", "array-value", "object", "object-value", "array", "array-value"] raw: []byte("1") kind: "number" index: 0
  • path: ["array", "array-value", "object", "object-value", "array", "array-value"] raw: []byte("2") kind: "number" index: 1
  • path: ["array", "array-value", "object", "object-value", "array", "array-value"] raw: []byte("3") kind: "number" index: 2
  • path: ["array", "array-value"] kind: "object" raw: []byte({"key1":"value1","ary":[1, 2, 3]})
  • path: [] kind: "array" raw: []byte([{"key1":"value1","ary":[1, 2, 3]}])

Documentation

Index

Examples

Constants

View Source
const (
	KindArray      strKind = "array"
	KindArrayValue strKind = "array-value"

	KindObject    strKind = "object"
	KindObjectKey strKind = "object-key"

	KindString  strKind = "string"
	KindNumber  strKind = "number"
	KindBoolean strKind = "boolean"
	KindNull    strKind = "null"
)
View Source
const ErrMalformed errorkit.Error = "malformed json error"

Variables

This section is empty.

Functions

func ErrMalformedF

func ErrMalformedF(format string, a ...any) error

func IterateArray

func IterateArray(ctx context.Context, r io.Reader) iter.Seq2[json.RawMessage, error]

func Query

func Query(ctx context.Context, r io.Reader, path ...Kind) iter.Seq2[json.RawMessage, error]

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 (
	"context"
	"fmt"
	"io"

	"go.llib.dev/frameless/pkg/jsonkit/jsontoken"
)

func main() {
	var ctx context.Context
	var body io.Reader

	result := jsontoken.Query(ctx, body, jsontoken.KindArray, jsontoken.KindArrayValue)
	for rawJSON, err := range result {
		if err != nil {
			fmt.Println(err.Error())
			continue
		}
		fmt.Println(string(rawJSON))
	}
}
Output:

func Scan

func Scan(in Input) (json.RawMessage, error)

func ScanFrom

func ScanFrom[T string | []byte | *bufio.Reader](v T) (json.RawMessage, error)

ScanFrom is a syntax sugar to use Scan with string and byte slices

func TrimSpace

func TrimSpace(data []byte) []byte

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 Input

type Input interface {
	iokit.RuneReader
	iokit.RuneUnreader
	iokit.ByteReader
}

type Kind

type Kind interface {
	Equal(Kind) bool
	String() string
}

type KindObjectValue

type KindObjectValue struct {
	// Key is the raw json data that represents the Key value
	Key json.RawMessage
}

func (KindObjectValue) Equal

func (k KindObjectValue) Equal(oth Kind) bool

func (KindObjectValue) String

func (k KindObjectValue) String() string

type Output

type Output interface {
	io.Writer
	WriteTo(w io.Writer) (n int64, err error)
	WriteRune(r rune) (n int, err error)
	WriteByte(c byte) error
	Bytes() []byte
}

type Path

type Path []Kind

func (Path) Equal

func (p Path) Equal(oth Path) bool

func (Path) Match

func (p Path) Match(oth Path) bool

Match check if the other path matches the

func (Path) With

func (p Path) With(k Kind) Path

type Scanner

type Scanner struct {
	Path Path
	Func func(json.RawMessage) error
}

func (*Scanner) Scan

func (s *Scanner) Scan(in Input) (json.RawMessage, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL