Documentation
¶
Overview ¶
Package jsonstream provides a JSON tokenizer that reports line and column information for tokens. It optionally supports /* */ and // comment syntax as an extension to standard JSON.
Index ¶
- func IsError(k Kind) bool
- func IsNonIntegerDecodeError(e error) bool
- func IsOutOfRangeDecodeError(e error) bool
- func PathEquals(path Path, elems []any) bool
- func PathToSlice(p Path) []any
- func WithPaths(tokens iter.Seq[Token]) iter.Seq[TokenWithPath]
- type Kind
- type Parser
- type Path
- type Token
- func (t *Token) AsBool() bool
- func (t Token) AsError() error
- func (t *Token) AsFloat32() float32
- func (t *Token) AsFloat64() float64
- func (t *Token) AsInt() int
- func (t *Token) AsInt32() int32
- func (t *Token) AsInt64() int64
- func (t *Token) AsString() string
- func (t Token) Error() string
- func (t *Token) KeyAsString() string
- func (t Token) String() string
- type TokenWithPath
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsNonIntegerDecodeError ¶
IsNonIntegerDecodeError Returns true iff a decode error results from an attempt to parse a non-integer numeric value as an integer.
func IsOutOfRangeDecodeError ¶
IsOutOfRangeDecodeError returns true iff a decode error results from an attempt to parse a numeric value that is out of range.
func PathEquals ¶ added in v0.2.0
PathEquals returns true iff the given path is equivalent to the given sequence of int and string values.
func PathToSlice ¶ added in v0.2.0
PathToSlice converts a Path to a slice of int and string values.
Types ¶
type Kind ¶
type Kind int
Kind represents the kind of a JSON token.
const ( // A '{' token ObjectStart Kind = iota // A '}' token ObjectEnd // A '[' token] ArrayStart // A ']' token ArrayEnd // A string String Kind = iota // A number Number Kind = iota // A true boolean value True Kind = iota // A false boolean value False Kind = iota // A null value Null Kind = iota // A // or /* */ comment. If you need to distinguish between the two, you can // look at the second byte of the token's Value field. Comment Kind = iota // A parse error ErrorTrailingInput Kind = iota | isError // An unexpected EOF was encountered ErrorUnexpectedEOF // An unexpected token was encountered ErrorUnexpectedToken // There is a trailing comma in an object or array (not permitted by the JSON // standard). ErrorTrailingComma // There is a comma in an unexpected position (either immediately following '[' // or '{' or immediately following another comma). ErrorUnexpectedComma // An unexpected character was encountered while tokenizing the input. ErrorUnexpectedCharacter // A numeric literal has leading zeros (not permitted by the JSON standard). // Tokens with this kind can also be treated as tokens of kind Number, if you // wish to be liberal in what you accept. ErrorLeadingZerosNotPermitted // A decimal point was not followed by a digit. ErrorExpectedDigitAfterDecimalPoint // The 'e' (or 'E') in a number was not followed by a digit. ErrorExpectedDigitFollowingEInNumber // A bad "\uXXXX" escape sequence was encountered in a string. ErrorBadUnicodeEscape // A control character not permitted by the JSON standard was found inside a // string. ErrorIllegalControlCharInsideString // UTF-8 decoding failing inside a string. ErrorUTF8DecodingErrorInsideString )
type Parser ¶
type Parser struct { AllowComments bool // Set to true to allow /* */ and // comments in the input AllowTrailingCommas bool // Set to true to allow trailing commas in arrays and objects (does not allow initial commas or multiple commas) // contains filtered or unexported fields }
Parser is a streaming JSON parser. It is valid when default initialized.
func (*Parser) DecodeError ¶
DecodeError returns the first decode error if any, or nil otherwise. A decode error is an error caused by invalid input to AsInt, AsInt32, AsInt64, AsFloat32, or AsFloat64.
func (*Parser) DecodeErrors ¶
DecodeErrors returns a slice containing all decode errors in the order they occurred. A decode error is an error occurring in AsInt, AsInt32, AsInt64, AsFloat32, or AsFloat64.
func (*Parser) LastDecodeError ¶
LastDecodeError returns the last decode error if any, or nil otherwise. A decode error is an error caused by invalid input to AsInt, AsInt32, AsInt64, AsFloat32, or AsFloat64.
func (*Parser) PopDecodeErrorIf ¶
Removes the last decode error if it satisfies the predicate. This is useful with the supplied predicates IsNonIntegerDecodeError and IsOutOfRangeDecodeError. For example, if p.PopDecodeErrorIf(IsOutOfRangeDecodeError) is called immediately after AsInt(), then errors caused by out of range integers will be ignored.
type Path ¶ added in v0.2.0
type Path struct {
// contains filtered or unexported fields
}
Path represents a sequence of strings and integers >= 0 that gives the path to a value inside a JSON document. For example, the sequence {1, "foo", 0} is the path to document[1]["foo"][0].
func SliceToPath ¶ added in v0.2.0
SliceToPath converts a slice of int and string values to a Path.
type Token ¶
type Token struct { Line int // the line number of the first character of the token Col int // the column of the first character of the token Start int // the start position of the token in the input (byte index) End int // the end position of the token in the input (byte index) Key []byte // the key of the token, or nil if none (may be a sub-slice of the input) Kind Kind // the kind of token Value []byte // the value of the token (may be a sub-slice of the input). ErrorMsg string // error message set if IsError(token.Kind) == true // contains filtered or unexported fields }
Token represents a JSON token.
func (*Token) AsBool ¶
AsBool returns the token's value as a bool. Its return value is defined only for tokens where Kind == True or Kind == False.
func (Token) AsError ¶ added in v0.1.2
AsError returns an error value if the token is an error token or nil otherwise.
func (*Token) AsFloat32 ¶
AsFloat32 returns the token's value as a float32. Its return value is defined only for tokens where Kind == Number. The input is parsed using strconv.ParseFloat. If ParseFloat signals an error, a decode error is added to the associated Parser.
func (*Token) AsFloat64 ¶
AsFloat64 returns the token's value as a float64. Its return value is defined only for tokens where Kind == Number. The input is parsed using strconv.ParseFloat. If ParseFloat signals an error, a decode error is added to the associated Parser.
func (*Token) AsInt ¶
AsInt returns the token's value as an int. Its return value is defined only for tokens where Kind == Number. If the value is not an integer or does not fit in an int, then a decode error is added to the associated Parser. A decode error is not added for in-range integer values specified using floating point syntax (e.g. '1.5e1', which evaluates to 15). If the decode error satisfies IsNotAnIntegerError(err) or IsOutOfRangeError(err) then the returned value approximates the value of the float as closely as possible. The function may therefore be used to parse floating point values as the nearest int value.
For more on decode errors see the following methods of Parser: DecodeError(), LastDecodeError(), DecodeErrors(), PopDecodeErrorIf().
func (*Token) AsString ¶
AsString returns the token's value as a string. Its return value is defined only for tokens where Kind == String.
func (*Token) KeyAsString ¶
KeyAsString returns the token's associated object Key as a string.
type TokenWithPath ¶ added in v0.2.0
func (TokenWithPath) String ¶ added in v0.2.0
func (twp TokenWithPath) String() string