Documentation
¶
Overview ¶
Package lenient provides scalar types that are forgiving about how a remote system encodes them. They exist for the receiving half of Postel's law: be conservative in what you send, be liberal in what you accept.
A plain `int64` field fails an entire document the first time a peer sends "480" instead of 480. Substituting lenient.Int64 absorbs that, and every other numeric spelling seen in the wild, without weakening the strict validation you apply to documents you author. lenient.String does the same for text fields that peers sometimes send as bare numbers or booleans.
The numeric type is Int64, not Int, on purpose: a sender's JSON has no idea what GOARCH you built for, so the accepted range must not narrow on a 32-bit target. Precision beyond int64 is not a goal.
Tolerance stops at structure. Both types reject input that is not a JSON scalar at all, because coercing an object or array would invent a value the sender never wrote. Everything short of that quietly degrades to the zero value rather than erroring, so one sloppy field never costs you the document.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Int64 ¶
type Int64 int64
Int64 is a tolerant integer that accepts whatever numeric encoding a remote system actually sends. Parsing semantics are convert.Int64, applied per Postel's law: JSON numbers and quoted integer strings parse, floats truncate toward zero, out-of-range values clamp to the int64 bounds, and null, empty, or unparseable values quietly become zero ("not provided"). Output is always a plain JSON number.
Integers are exact across the whole int64 range: a top-level JSON integer is parsed from its source text, so values above 2^53 survive intact rather than degrading through a float64. The underlying type is int64 rather than int so that range does not depend on the platform — a sender's JSON has no idea what GOARCH you built for. Precision beyond int64 is not a goal: larger values clamp, and an integer nested inside an array (`[9007199254740993]`) unwraps through the tolerant path and rounds.
func (Int64) MarshalJSON ¶
MarshalJSON encodes the value as a plain JSON number.
func (*Int64) UnmarshalJSON ¶
UnmarshalJSON decodes any JSON value into the integer, tolerantly.
func (*Int64) UnmarshalXML ¶
UnmarshalXML decodes the element's character data with the same tolerant numeric parsing as UnmarshalJSON.
type String ¶
type String string
String is a tolerant string that accepts whatever scalar encoding a remote system actually sends. Applied per Postel's law: quoted strings pass through, JSON numbers keep their exact source text (oEmbed providers send `"version": 1.0`, and "1.0" must not become "1"), booleans become "true"/"false", and null becomes the empty string. Objects and arrays are not scalars and are rejected. Output is always a plain JSON string.
func (*String) UnmarshalJSON ¶
UnmarshalJSON decodes any scalar JSON value into the string, tolerantly.