package jsonwire
Import Path
encoding/json/internal/jsonwire (on go.dev)
Dependency Relation
imports 11 packages, and imported by 3 packages
Involved Source Files
decode.go
encode.go
Package jsonwire implements stateless functionality for handling JSON text.
Package-Level Type Names (total 2)
func ConsumeNumberResumable(b []byte, resumeOffset int, state ConsumeNumberState) (n int, _ ConsumeNumberState, err error)
func ConsumeNumberResumable(b []byte, resumeOffset int, state ConsumeNumberState) (n int, _ ConsumeNumberState, err error)
( ValueFlags) IsCanonical() bool
( ValueFlags) IsVerbatim() bool
(*ValueFlags) Join(f2 ValueFlags)
func ConsumeString(flags *ValueFlags, b []byte, validateUTF8 bool) (n int, err error)
func ConsumeStringResumable(flags *ValueFlags, b []byte, resumeOffset int, validateUTF8 bool) (n int, err error)
func (*ValueFlags).Join(f2 ValueFlags)
Package-Level Functions (total 29)
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.
Type Parameters:
Bytes: ~[]byte | ~string
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).
Type Parameters:
Bytes: ~[]byte | ~string
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.
Type Parameters:
Bytes: ~[]byte | ~string
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.
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.
ConsumeLiteral consumes the next JSON literal per RFC 7159, section 3.
If the input appears truncated, it returns io.ErrUnexpectedEOF.
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.
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.
ConsumeNumberResumable is identical to consumeNumber but supports resuming
from a previous call that returned io.ErrUnexpectedEOF.
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.
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.
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.
ConsumeStringResumable is identical to consumeString but supports resuming
from a previous call that returned io.ErrUnexpectedEOF.
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.
ConsumeWhitespace consumes leading JSON whitespace per RFC 7159, section 2.
HasSuffixByte reports whether b ends with c.
Type Parameters:
Bytes: ~[]byte | ~string
NeedEscape reports whether src needs escaping of any characters.
It conservatively assumes EscapeForHTML and EscapeForJS.
It reports true for inputs with invalid UTF-8.
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.
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.
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.
ReformatString consumes a JSON string from src and appends it to dst,
reformatting it if necessary according to the specified flags.
It returns the appended output and the number of consumed input bytes.
TrimSuffixByte removes c from the end of b if it is present.
TrimSuffixString trims a valid JSON string at the end of b.
The behavior is undefined if there is not a valid JSON string present.
TrimSuffixWhitespace trims JSON from the end of b.
TruncatePointer optionally truncates the JSON pointer,
enforcing that the length roughly does not exceed n.
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.
Package-Level Variables (only one)
TODO(https://go.dev/issue/70547): Use utf8.ErrInvalid instead.
![]() |
The pages are generated with Golds v0.7.7-preview. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |