Documentation
¶
Overview ¶
Package jsonx holds the shared option sets for encoding/json/v2 and the few helpers that have no 1:1 v2 equivalent.
Every call site uses encoding/json/v2 and encoding/json/jsontext directly; V1 is passed so a mechanical swap from encoding/json keeps byte-identical wire output and error text (nil slices and maps as null, HTML escaping, case-insensitive member matching, tolerated duplicate names and invalid UTF-8, v1 error messages). Drop V1 per call only where the v2 default is wanted and verified.
This is the only package allowed to import the v1 encoding/json package: DefaultOptionsV1 and the legacy-semantics options are exported from there.
v1 to v2 mapping:
json.Marshal(v) json.Marshal(v, jsonx.V1())
json.Unmarshal(b, &v) json.Unmarshal(b, &v, jsonx.V1())
json.MarshalIndent(v, p, i) json.Marshal(v, jsonx.V1(), jsontext.Multiline(true),
jsontext.WithIndentPrefix(p), jsontext.WithIndent(i))
json.NewDecoder(r).Decode(&v) json.UnmarshalDecode(jsontext.NewDecoder(r), &v, jsonx.V1())
json.NewEncoder(w).Encode(v) json.MarshalEncode(jsontext.NewEncoder(w, jsonx.V1()), v)
enc.SetIndent / SetEscapeHTML jsontext.Multiline, WithIndent, EscapeForHTML on NewEncoder
dec.DisallowUnknownFields() json.RejectUnknownMembers(true)
dec.UseNumber(), json.Number jsonx.UseNumber(), jsonx.Number
json.RawMessage jsontext.Value
dec.Token() dec.ReadToken() (Kind: '{' '[' '"' '0' 't' 'f' 'n')
dec.More() inside a container dec.PeekKind() not '}' or ']'; at top level
compare InputOffset against the input (PeekKind()!=0 accepts garbage)
dec.Buffered() bytes.NewReader(dec.UnreadBuffer())
json.Delim jsontext.Kind
json.Valid(b) jsontext.Value(b).IsValid(jsonx.V1())
json.Compact / Indent jsontext.Value.Compact / Indent
json.Marshaler, Unmarshaler same names in v2 (MarshalJSON / UnmarshalJSON still honored)
*json.SyntaxError jsonx.SyntaxError / IsSyntaxError
*json.UnmarshalTypeError jsonx.TypeError / IsTypeError
Error types: with V1 the v2 engine returns the v1 types (*json.SyntaxError, *json.UnmarshalTypeError), not jsontext.SyntacticError or json.SemanticError, so errors.As on the v2 types silently misses and turns client errors into 500s. Always classify with SyntaxError, IsSyntaxError, TypeError and IsTypeError, which accept both families.
A jsontext.Token is invalidated by the next decoder call; copy String() before reading on. Whitespace options (Multiline, WithIndent) belong on jsontext.NewEncoder, not on MarshalEncode. SDKDecoder returns a v1 Decoder for SDK helpers that require one.
Use UnmarshalRead instead of UnmarshalDecode only when trailing data after the first value must be an error; v1 Decoder.Decode ignored it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsSyntaxError ¶
IsSyntaxError reports whether err is a JSON syntax error.
func IsTypeError ¶
IsTypeError reports whether err is a JSON value/type mismatch.
func SDKDecoder ¶
SDKDecoder returns a v1 decoder for AWS SDK helpers that still take *encoding/json.Decoder (e.g. restjson.GetErrorInfo).
func SyntaxError ¶
SyntaxError reports whether err is a JSON syntax error and its byte offset. With V1 options the v2 engine returns the v1 error types, so both are accepted.
func TypeError ¶
TypeError reports whether err is a JSON value/type mismatch and the dotted path of the offending member (the v1 UnmarshalTypeError.Field form).
Types ¶
type Number ¶
type Number string
Number is a JSON number kept in its textual form, the v2 stand-in for v1 json.Number. It is produced for untyped numbers when UseNumber is set.
func (Number) MarshalJSON ¶
MarshalJSON emits the number literal; an empty Number is 0 like v1.
func (*Number) UnmarshalJSON ¶
UnmarshalJSON accepts a JSON number, or a string holding a valid number as v1 did.