jsonkit

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 6 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 jsonkit is the JSON house style of the repository: the few named option sets that every call of encoding/json/v2 takes, and the Number type that keeps a number as it was written.

encoding/json/v2 differs from the old encoding/json (v1) by default, and what a client, an SDK, another replica or a stored file can see must not change with the move. The sets are built from the options of the v2 and jsontext packages only, one reason each, and a call site uses a set instead of its own list:

  • Wire: everything that leaves the process (HTTP bodies, exporter and notifier payloads, files, ids and ETags that are hashes of an encoding).
  • Lenient: input from clients, SDKs, flag files and the admin API, as lenient as v1 and GO Feature Flag are.
  • Strict: configuration input that rejects what it does not know, where the code was strict before.

The standard library is used as it is: calls read as jsonv2.Marshal(value, jsonkit.Wire()). A test of internal/archtest fails for a call of Marshal or Unmarshal (and their Read, Write, Encode and Decode forms) with no option.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotNumber = errors.New("not a JSON number")

ErrNotNumber is the error of a Number that is read from, or written as, something that is not a JSON number.

Functions

func Indented

func Indented(indent string) interface{ jsonv2.Options }

Indented is Wire with the lines indented by indent, for documents that people read or review (the OpenAPI document, the schemas, the output of the command line). It has no prefix.

func Lenient

func Lenient() interface{ jsonv2.Options }

Lenient is the option set of input that GO Feature Flag accepts through encoding/json (v1), and so must be accepted here with the same meaning: SDK requests, flag set and flag files, admin requests, documents of the control plane.

  • MatchCaseInsensitiveNames: "Key" and "KEY" fill the field "key" (v2 matches the exact case only).
  • AllowDuplicateNames: a repeated member is read, the last value wins (v2 fails).
  • AllowInvalidUTF8: invalid UTF-8 in a string becomes U+FFFD (v2 fails).
  • a Number is read as the literal of a JSON number.

A repeated member inside an object that is read into an any or a typed value is merged by v2 where v1 replaced it, and no option changes that.

func Marshal

func Marshal(value any) ([]byte, error)

Marshal encodes value as Wire does.

func MarshalLine

func MarshalLine(value any, opts jsonv2.Options) ([]byte, error)

MarshalLine is jsonv2.Marshal with a newline after the value, the form that the Encode of encoding/json (v1) wrote and that clients of the relay were served: one document, one line, in a single piece (a value that cannot be encoded leaves nothing behind).

func MarshalWireLine

func MarshalWireLine(value any) ([]byte, error)

MarshalWireLine is MarshalLine with the options of Wire, passed as arguments like those of Marshal.

func MarshalWrite

func MarshalWrite(out io.Writer, value any) error

MarshalWrite encodes value as Wire does, into out.

func Readable

func Readable() interface{ jsonv2.Options }

Readable is Wire without the escaping of <, > and &, for text that people read (a change log, a redaction comparison). U+2028 and U+2029 stay escaped, as v1 did when HTML escaping was off.

func Strict

func Strict() interface{ jsonv2.Options }

Strict is Lenient that also rejects a member that no field names (RejectUnknownMembers): the configuration input that was strict before (the DisallowUnknownFields of v1).

func Unmarshal

func Unmarshal(data []byte, out any) error

Unmarshal decodes data as Lenient does.

func Valid

func Valid(data []byte) bool

Valid reports whether data is one JSON value, as the Valid of encoding/json (v1) does: the syntax is checked, a repeated member name and invalid UTF-8 are not refused.

func Wire

func Wire() interface{ jsonv2.Options }

Wire is the option set of every encoding that a client, another replica, a file or a hash can see. It gives the bytes that encoding/json (v1) gave, so that a body, an ETag or a state id does not change:

  • Deterministic: map members are sorted by key (v1 sorted them; v2 does not by default), so that the same value always has the same bytes, which ETags and signed webhook bodies rely on.
  • FormatNilSliceAsNull and FormatNilMapAsNull: a nil slice or map is null, not [] or {} (v2 default).
  • EscapeForHTML and EscapeForJS: <, > and & are written as <, > and &, and U+2028 and U+2029 as and 
, as v1 did.
  • AllowInvalidUTF8: a string with invalid UTF-8 is written with U+FFFD instead of failing (v1 did; v2 fails).
  • AllowDuplicateNames: the output of a custom marshaler is not refused for a repeated name (v1 did not check).
  • PreserveRawStrings: a raw value (jsontext.Value, a stored encoding) keeps its escapes (v1 did).
  • a Number is written as the literal that it holds.

Types

type Number

type Number string

Number is a JSON number kept as it was written: "1.0" stays "1.0", "1e3" stays "1e3", and a number beyond int64 or float64 keeps every digit. Wire writes and Lenient reads that literal unchanged. The empty Number is written as 0.

func (Number) Float64

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

Float64 is the number as a float64; it fails for a value out of range.

func (Number) Int64

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

Int64 is the number as an int64; it fails for a fraction, an exponent or a value out of range.

func (Number) MarshalJSON

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

MarshalJSON writes the literal. The empty Number is written as 0. It is a method of the type, so that no option has to carry it: an option that carries a function makes v2 build its table of marshalers for each call that builds the option anew.

func (Number) String

func (n Number) String() string

String is the literal.

func (*Number) UnmarshalJSON

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

UnmarshalJSON reads the literal of a JSON number. A null leaves the number as it is, as it did for a json.Number of v1.

Jump to

Keyboard shortcuts

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