jsonx

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func IsSyntaxError(err error) bool

IsSyntaxError reports whether err is a JSON syntax error.

func IsTypeError

func IsTypeError(err error) bool

IsTypeError reports whether err is a JSON value/type mismatch.

func SDKDecoder

func SDKDecoder(r io.Reader) *jsonv1.Decoder

SDKDecoder returns a v1 decoder for AWS SDK helpers that still take *encoding/json.Decoder (e.g. restjson.GetErrorInfo).

func SyntaxError

func SyntaxError(err error) (int64, bool)

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

func TypeError(err error) (string, bool)

TypeError reports whether err is a JSON value/type mismatch and the dotted path of the offending member (the v1 UnmarshalTypeError.Field form).

func UseNumber

func UseNumber() json.Options

UseNumber makes every untyped (any) JSON number decode as Number instead of float64, replacing v1 Decoder.UseNumber.

func V1

func V1() json.Options

V1 is the full option set that reproduces encoding/json v1 behavior.

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) Float64

func (n Number) Float64() (float64, error)

Float64 returns the number as a float64.

func (Number) Int

func (n Number) Int() (int, error)

Int returns the number as an int, failing when it is out of int range.

func (Number) Int64

func (n Number) Int64() (int64, error)

Int64 returns the number as an int64.

func (Number) MarshalJSON

func (n Number) MarshalJSON() ([]byte, error)

MarshalJSON emits the number literal; an empty Number is 0 like v1.

func (Number) String

func (n Number) String() string

String returns the literal text of the number.

func (*Number) UnmarshalJSON

func (n *Number) UnmarshalJSON(b []byte) error

UnmarshalJSON accepts a JSON number, or a string holding a valid number as v1 did.

Jump to

Keyboard shortcuts

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