jsontk

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 10 Imported by: 1

README

jsontk

image

from Top Gear S29E2

又不是不能用,你就说比标准库快不快吧

features

{
	"with" :  	// comments!
     true,
 "allows": 1, // a bit of error
}

compatibility

tested with https://github.com/nst/JSONTestSuite using this code:

func walk(tks *jsontk.JSON) {
    var err error
    switch tks.Type() {
    case jsontk.BEGIN_OBJECT:
        for _, k := range tks.Keys() {
            walk(tks.Get(k))
        }
    case jsontk.BEGIN_ARRAY:
        for i := tks.Len(); i > 0; i-- {
            walk(tks.Index(i - 1))
        }
    case jsontk.NUMBER:
        _, err = tks.Float64()
    case jsontk.STRING:
        _, err = tks.String()
    }
    if err != nil {
        os.Exit(1)
    }
}

tks, err := jsontk.Tokenize(b)
//fmt.Println(f)
if err != nil {
    os.Exit(1)
}
walk(tks)

image

the results only shows the difference between standard library and jsontk, the succeeded cases are not shown.

Warning / Disclaimer

EXPERIMENTAL

This library is solely designed to extract payload fields in an insecure manner. You might not want to use this library unless you understand what you're doing

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPanic              = errors.New("panic occurred")
	ErrUnexpectedSep      = errors.New("invalid separator")
	ErrEarlyEOF           = errors.New("early EOF")
	ErrUnexpectedToken    = errors.New("invalid TokenType")
	ErrInvalidParentheses = errors.New("invalid parentheses")
	ErrStandardViolation  = errors.New("json not compliant to RFC8259") // for some simple validations
)

Functions

func Iterate added in v0.4.0

func Iterate(s []byte, cb func(typ TokenType, data []byte)) error

Types

type JSON

type JSON struct {
	// contains filtered or unexported fields
}

func Tokenize

func Tokenize(s []byte) (result *JSON, err error)

func (*JSON) BatchGet added in v0.1.2

func (j *JSON) BatchGet(into map[string]*JSON) int

func (*JSON) Bool

func (j *JSON) Bool() (bool, error)

func (*JSON) Close added in v0.2.0

func (c *JSON) Close()

func (*JSON) Float64

func (j *JSON) Float64() (float64, error)

func (*JSON) Get

func (j *JSON) Get(key string) *JSON

func (*JSON) Index

func (j *JSON) Index(i int) *JSON

func (*JSON) Int64

func (j *JSON) Int64() (int64, error)

func (*JSON) IsNull

func (j *JSON) IsNull() bool

func (*JSON) Keys added in v0.2.0

func (j *JSON) Keys() []string

func (*JSON) Len added in v0.2.0

func (j *JSON) Len() int

func (*JSON) Number

func (j *JSON) Number() (json.Number, error)

func (*JSON) String

func (j *JSON) String() (string, error)

func (*JSON) Tokenize added in v0.2.0

func (c *JSON) Tokenize(s []byte) error

Tokenize reads binary data into an array of JSON tokens. The data passed in tokenize should never be changed even after calling Tokenize or unexpected data could be read from the result.

func (*JSON) Type added in v0.1.2

func (j *JSON) Type() TokenType

func (*JSON) Validate added in v0.3.0

func (j *JSON) Validate() error

type Token

type Token struct {
	Type  TokenType
	Value []byte
}

type TokenType

type TokenType int
const (
	INVALID TokenType = iota
	BEGIN_OBJECT
	END_OBJECT
	BEGIN_ARRAY
	END_ARRAY
	KEY
	STRING
	NUMBER
	BOOLEAN
	NULL
)

func (TokenType) String

func (t TokenType) String() string

Jump to

Keyboard shortcuts

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