jsonwire

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package jsonwire implements stateless functionality for handling JSON text.

Index

Constants

This section is empty.

Variables

View Source
var (
	NewError       = errors.New
	ErrInvalidUTF8 = errors.New("invalid UTF-8 within string")
)

NewError and ErrInvalidUTF8 are injected by the "jsontext" package, so that these error types use the jsontext.SyntacticError type.

Functions

func AppendFloat

func AppendFloat(dst []byte, src float64, bits int) []byte

AppendFloat appends src to dst as a JSON number per RFC 7159, section 6. It formats numbers similar to the ES6 number-to-string conversion. See https://go.dev/issue/14135.

The output is identical to ECMA-262, 6th edition, section 7.1.12.1 and with RFC 8785, section 3.2.2.3 for 64-bit floating-point numbers except for -0, which is formatted as -0 instead of just 0.

For 32-bit floating-point numbers, the output is a 32-bit equivalent of the algorithm. Note that ECMA-262 specifies no algorithm for 32-bit numbers.

func AppendQuote

func AppendQuote[Bytes ~[]byte | ~string](dst []byte, src Bytes, flags *jsonflags.Flags) ([]byte, error)

AppendQuote appends src to dst as a JSON string per RFC 7159, section 7.

It takes in flags and respects the following:

  • EscapeForHTML escapes '<', '>', and '&'.
  • EscapeForJS escapes '\u2028' and '\u2029'.
  • AllowInvalidUTF8 avoids reporting an error for invalid UTF-8.

Regardless of whether AllowInvalidUTF8 is specified, invalid bytes are replaced with the Unicode replacement character ('\ufffd'). If no escape flags are set, then the shortest representable form is used, which is also the canonical form for strings (RFC 8785, section 3.2.2.2).

func AppendUnquote

func AppendUnquote[Bytes ~[]byte | ~string](dst []byte, src Bytes) (v []byte, err error)

AppendUnquote appends the unescaped form of a JSON string in src to dst. Any invalid UTF-8 within the string will be replaced with utf8.RuneError, but the error will be specified as having encountered such an error. The input must be an entire JSON string with no surrounding whitespace.

func CompareUTF16

func CompareUTF16[Bytes ~[]byte | ~string](x, y Bytes) int

CompareUTF16 lexicographically compares x to y according to the UTF-16 codepoints of the UTF-8 encoded input strings. This implements the ordering specified in RFC 8785, section 3.2.3.

func ConsumeFalse

func ConsumeFalse(b []byte) int

ConsumeFalse consumes the next JSON false literal per RFC 7159, section 3. It returns 0 if it is invalid, in which case consumeLiteral should be used.

func ConsumeLiteral

func ConsumeLiteral(b []byte, lit string) (n int, err error)

ConsumeLiteral consumes the next JSON literal per RFC 7159, section 3. If the input appears truncated, it returns io.ErrUnexpectedEOF.

func ConsumeNull

func ConsumeNull(b []byte) int

ConsumeNull consumes the next JSON null literal per RFC 7159, section 3. It returns 0 if it is invalid, in which case consumeLiteral should be used.

func ConsumeNumber

func ConsumeNumber(b []byte) (n int, err error)

ConsumeNumber consumes the next JSON number per RFC 7159, section 6. It reports the number of bytes consumed and whether an error was encountered. If the input appears truncated, it returns io.ErrUnexpectedEOF.

Note that JSON numbers are not self-terminating. If the entire input is consumed, then the caller needs to consider whether there may be subsequent unread data that may still be part of this number.

func ConsumeSimpleNumber

func ConsumeSimpleNumber(b []byte) (n int)

ConsumeSimpleNumber consumes the next JSON number per RFC 7159, section 6 but is limited to the grammar for a positive integer. It returns 0 if it is invalid or more complicated than a simple integer, in which case consumeNumber should be called.

func ConsumeSimpleString

func ConsumeSimpleString(b []byte) (n int)

ConsumeSimpleString consumes the next JSON string per RFC 7159, section 7 but is limited to the grammar for an ASCII string without escape sequences. It returns 0 if it is invalid or more complicated than a simple string, in which case consumeString should be called.

It rejects '<', '>', and '&' for compatibility reasons since these were always escaped in the v1 implementation. Thus, if this function reports non-zero then we know that the string would be encoded the same way under both v1 or v2 escape semantics.

func ConsumeString

func ConsumeString(flags *ValueFlags, b []byte, validateUTF8 bool) (n int, err error)

ConsumeString consumes the next JSON string per RFC 7159, section 7. If validateUTF8 is false, then this allows the presence of invalid UTF-8 characters within the string itself. It reports the number of bytes consumed and whether an error was encountered. If the input appears truncated, it returns io.ErrUnexpectedEOF.

func ConsumeStringResumable

func ConsumeStringResumable(flags *ValueFlags, b []byte, resumeOffset int, validateUTF8 bool) (n int, err error)

ConsumeStringResumable is identical to consumeString but supports resuming from a previous call that returned io.ErrUnexpectedEOF.

func ConsumeTrue

func ConsumeTrue(b []byte) int

ConsumeTrue consumes the next JSON true literal per RFC 7159, section 3. It returns 0 if it is invalid, in which case consumeLiteral should be used.

func ConsumeWhitespace

func ConsumeWhitespace(b []byte) (n int)

ConsumeWhitespace consumes leading JSON whitespace per RFC 7159, section 2.

func HasSuffixByte

func HasSuffixByte(b []byte, c byte) bool

HasSuffixByte reports whether b ends with c.

func NeedEscape

func NeedEscape[Bytes ~[]byte | ~string](src Bytes) bool

NeedEscape reports whether src needs escaping of any characters. It conservatively assumes EscapeForHTML and EscapeForJS. It reports true for inputs with invalid UTF-8.

func NewInvalidCharacterError

func NewInvalidCharacterError[Bytes ~[]byte | ~string](prefix Bytes, where string) error

func NewInvalidEscapeSequenceError

func NewInvalidEscapeSequenceError[Bytes ~[]byte | ~string](what Bytes) error

func ParseFloat

func ParseFloat(b []byte, bits int) (v float64, ok bool)

ParseFloat parses a floating point number according to the Go float grammar. Note that the JSON number grammar is a strict subset.

If the number overflows the finite representation of a float, then we return MaxFloat since any finite value will always be infinitely more accurate at representing another finite value than an infinite value.

func ParseUint

func ParseUint(b []byte) (v uint64, ok bool)

ParseUint parses b as a decimal unsigned integer according to a strict subset of the JSON number grammar, returning the value if valid. It returns (0, false) if there is a syntax error and returns (math.MaxUint64, false) if there is an overflow.

func QuoteRune

func QuoteRune[Bytes ~[]byte | ~string](b Bytes) string

QuoteRune quotes the first rune in the input.

func ReformatNumber

func ReformatNumber(dst, src []byte, canonicalize bool) ([]byte, int, error)

ReformatNumber consumes a JSON string from src and appends it to dst, canonicalizing it if specified. It returns the appended output and the number of consumed input bytes.

func ReformatString

func ReformatString(dst, src []byte, flags *jsonflags.Flags) ([]byte, int, error)

ReformatString consumes a JSON string from src and appends it to dst, reformatting it if necessary for the given escapeRune parameter. It returns the appended output and the number of consumed input bytes.

func TrimSuffixByte

func TrimSuffixByte(b []byte, c byte) []byte

TrimSuffixByte removes c from the end of b if it is present.

func TrimSuffixString

func TrimSuffixString(b []byte) []byte

TrimSuffixString trims a valid JSON string at the end of b. The behavior is undefined if there is not a valid JSON string present.

func TrimSuffixWhitespace

func TrimSuffixWhitespace(b []byte) []byte

TrimSuffixWhitespace trims JSON from the end of b.

func UnquoteMayCopy

func UnquoteMayCopy(b []byte, isVerbatim bool) []byte

UnquoteMayCopy returns the unescaped form of b. If there are no escaped characters, the output is simply a subslice of the input with the surrounding quotes removed. Otherwise, a new buffer is allocated for the output. It assumes the input is valid.

Types

type ConsumeNumberState

type ConsumeNumberState uint

func ConsumeNumberResumable

func ConsumeNumberResumable(b []byte, resumeOffset int, state ConsumeNumberState) (n int, _ ConsumeNumberState, err error)

ConsumeNumberResumable is identical to consumeNumber but supports resuming from a previous call that returned io.ErrUnexpectedEOF.

type ValueFlags

type ValueFlags uint

func (ValueFlags) IsCanonical

func (f ValueFlags) IsCanonical() bool

func (ValueFlags) IsVerbatim

func (f ValueFlags) IsVerbatim() bool

func (*ValueFlags) Join

func (f *ValueFlags) Join(f2 ValueFlags)

Jump to

Keyboard shortcuts

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