Documentation
¶
Index ¶
- Variables
- func Parse(in []byte) iter.Seq2[Item, error]
- func Reconstruct(in []byte) ([]byte, error)
- func Reformat(in []byte, prefix, indent string) ([]byte, error)
- func Scan(in []byte) iter.Seq2[RawToken, error]
- func ShouldAddComma(lastToken, nextToken TokenType) bool
- type Builder
- func (b *Builder) Add(key any, value any)
- func (b *Builder) AddRaw(key, token RawToken)
- func (b *Builder) Bytes() ([]byte, error)
- func (b *Builder) Len() int
- func (b *Builder) SetSkipEmptyStructures(skip bool)
- func (b *Builder) ShouldAddComma(next TokenType) bool
- func (b *Builder) Write(p []byte) (n int, err error)
- func (b *Builder) WriteComma(next TokenType)
- func (b *Builder) WriteIndent()
- func (b *Builder) WriteNewline(next TokenType)
- type Item
- func (x Item) Format(f fmt.State, c rune)
- func (x Item) GetAltPathString() string
- func (x Item) GetPath() Path
- func (x Item) GetPathString() string
- func (x Item) GetRawPath() RawPath
- func (x Item) GetTokenValue() (any, error)
- func (x Item) GetValue() (any, error)
- func (x Item) IsArrayValue() bool
- func (x Item) IsObjectValue() bool
- func (x Item) String() string
- type Path
- type PathItem
- func (p PathItem) ArrayIndex() (int, bool)
- func (p PathItem) Format(f fmt.State, c rune)
- func (p PathItem) IsArray() bool
- func (p PathItem) IsArrayIndex(idx int) bool
- func (p PathItem) IsObject() bool
- func (p PathItem) IsObjectKey(key string) bool
- func (p PathItem) IsObjectRawKey(rawKey string) bool
- func (p PathItem) Match(x any) bool
- func (p PathItem) ObjectKey() (string, bool)
- func (p PathItem) String() string
- func (p PathItem) Value() any
- type RawPath
- type RawToken
- func (r RawToken) Bytes() []byte
- func (r RawToken) Equal(other RawToken) bool
- func (r RawToken) GetBool() (bool, error)
- func (r RawToken) GetInt() (int, error)
- func (r RawToken) GetNumber() (float64, error)
- func (r RawToken) GetString() (string, error)
- func (r RawToken) GetValue() (any, error)
- func (r RawToken) IsClose() bool
- func (r RawToken) IsOpen() bool
- func (r RawToken) IsValue() bool
- func (r RawToken) IsZero() bool
- func (r RawToken) Raw() []byte
- func (r RawToken) String() string
- func (r RawToken) Type() TokenType
- type TokenType
Constants ¶
This section is empty.
Variables ¶
var ( ErrTokenInvalid = errors.New("invalid token") ErrTokenEmpty = errors.New("invalid empty token") ErrTokenString = errors.New("invalid string token") ErrTokenNumber = errors.New("invalid number token") ErrNumberNotInt = errors.New("number is not an integer") ErrTokenBool = errors.New("invalid boolean token") ErrTokenType = errors.New("invalid token type") )
Functions ¶
func Reconstruct ¶
Reconstruct is an example of how to reconstruct a JSON from Parse().
func Reformat ¶
Reformat is an example of how to reconstruct a JSON from Parse(), and format with indentation.
func ShouldAddComma ¶
ShouldAddComma returns true if a comma should be added before the next token.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
func NewBuilder ¶
NewBuilder creates a new Builder. It's optional to set the prefix and indent. A zero Builder is valid.
func (*Builder) SetSkipEmptyStructures ¶
SetSkipEmptyStructures makes the Builder ignores empty array `[]` and empty object `{}`. Default to false.
func (*Builder) ShouldAddComma ¶
ShouldAddComma returns true if a comma should be added before the next token.
func (*Builder) WriteComma ¶
WriteCommand writes a comma if needed.
func (*Builder) WriteIndent ¶
func (b *Builder) WriteIndent()
WriteIndent writes the indentation if needed.
func (*Builder) WriteNewline ¶
WriteNewline writes a newline if needed.
type Item ¶
type Item struct { Level int // level of indentation Index int // index in the parent array or object Key RawToken // optional object "key" Token RawToken // [ or { or } or ] or , or value // contains filtered or unexported fields }
func (Item) GetAltPathString ¶
GetAltPathString returns the path of the item as a string `[0].key[1]`.
func (Item) GetPath ¶
GetPath returns the path of the item as a slice of values. The values are the keys of objects (string) and the indexes of arrays (int).
func (Item) GetPathString ¶
GetPathString returns the path of the item as a string "0.key.1".
func (Item) GetRawPath ¶
GetRawPath returns the path of the item as a slice of PathItem. IMPORTANT: The result slice should not be modified.
func (Item) GetTokenValue ¶
func (Item) IsArrayValue ¶
func (Item) IsObjectValue ¶
type Path ¶
type Path []any
Path is a slice of values. The values are the keys of objects (string) and the indexes of arrays (int).
type PathItem ¶
type PathItem struct { Index int // array index or object index Key RawToken // object key Token RawToken // [ or { or } or ] }
func NewPathItem ¶
NewPathItem returns a new PathItem. Use int for array index and string for object key. Keys should be quoted for better performance (optional).
func (PathItem) ArrayIndex ¶
ArrayIndex returns the array index of the path item. It returns the index as an int if the item is inside an array.
func (PathItem) Format ¶
Format formats the path item as a string. Use "%+v" to format as "[0]" for array, ".key" for object.
func (PathItem) IsArrayIndex ¶
IsArrayIndex returns true if the path item is inside an array and the index matches the given index.
func (PathItem) IsObjectKey ¶
IsObjectKey returns true if the path item is inside an object and the key matches the given key.
func (PathItem) IsObjectRawKey ¶
IsObjectRawKey returns true if the path item is inside an object and the key matches the given unquote key.
func (PathItem) Match ¶
Match returns true if the path item matches the given value. The value must be an int for array index or a string|[]byte for object key.
func (PathItem) ObjectKey ¶
ObjectKey returns the object key of the path item. It returns the key as a string if the item is inside an object.
type RawPath ¶
type RawPath []PathItem
func NewRawPath ¶
NewRawPath returns a new RawPath. Use int for array index and string for object key. Keys should be quoted for better performance (optional).
func (RawPath) Contains ¶
ContainsRaw returns true if the path contains the given path, by raw values (int for array index, string for object key). Examples:
path.Contains([]any{1, "key", 2})
func (RawPath) Format ¶
Format formats the path as a string. Default to 0.key.1 or "%+v" to format as [0]."key"[1]
type RawToken ¶
type RawToken struct {
// contains filtered or unexported fields
}
RawToken represents a raw token from the scanner.
func MustRawToken ¶
MustRawToken returns a new raw token from the raw bytes. Panic if error.
func NewRawToken ¶
NewRawToken returns a new raw token from the raw bytes.
func NumberToken ¶
NumberToken returns a number token. For NaN and Inf, fallback to 0.
func (RawToken) GetString ¶
GetString returns the unquoted string value of the token. https://datatracker.ietf.org/doc/html/rfc8259#section-7
type TokenType ¶
type TokenType byte
TokenType represents the type of a JSON token.
const ( TokenNull TokenType = 'n' TokenTrue TokenType = 't' TokenFalse TokenType = 'f' TokenNumber TokenType = '0' TokenString TokenType = '"' TokenObjectOpen TokenType = '{' TokenObjectClose TokenType = '}' TokenArrayOpen TokenType = '[' TokenArrayClose TokenType = ']' TokenComma TokenType = ',' TokenColon TokenType = ':' )
func (TokenType) IsValue ¶
IsValue returns true if the token is a value: null, boolean, number, string.