Documentation
¶
Overview ¶
Package strictjson reads a JSON text under the parsing rules J1 to J6 of SPEC.md section 10.1 (Parsing), steps READ (J1) and PARSE (J2 to J6) of section 10.2, each rule chosen so that no two verifiers read one file differently. It is the reader every later verification step reads the document through, and a keyring (section 10.2, step 0) is read through it too.
It is named after the Truestamp application's Truestamp.StrictJSON.
Read and ReadFile make J1, reading at most MAX_READ_OCTETS + 1 octets, which decides it; CheckSize makes J1 on bytes already in memory. Parse makes J2 to J6 with its own tokenizer, which builds the value directly: nothing is decoded by encoding/json, whose v1 API keeps the last of two members with one name and replaces invalid UTF-8 and lone surrogates with U+FFFD, and whose v2 API makes J2 to J4 but not J1, J5 or J6. Decode makes J1 and then J2 to J6, in step order. A file the rules refuse is a *Rejection, the bare rejection input_too_large or malformed_json with its sub_reason (SPEC.md sections 10.1 and 12.1).
Values ¶
A parsed value is within the value profile of SPEC.md section 10.1, and has one Go form per JSON value, so two files that differ only in formatting parse to equal values:
- an object is a map[string]any from its member names to their values, and an array is a []any;
- a string is a Go string of Unicode scalar values, with every escape resolved;
- true and false are bools, and null is nil;
- a number is a Number, its nearest binary64 value (J5): 1, 1.0 and 1e0 are one Number, and -0, -0.0 and -1e-400 read as 0.
Constants ¶
MAX_READ_OCTETS, MAX_DEPTH and MAX_INTEGER come from internal/specdata, as do the reason codes and sub_reasons. The truestamp binary links internal/specdata through internal/jcs and this package, so the checks specdata makes when it is initialized run at the binary's start-up, as they do in every program that links this package: a file under specdata's embedded verification/spec/constructions/ that is not named by its digest, such as a stray Finder .DS_Store, stops such a program at start-up with a panic. specdata's tests and the sync's refusal of dotfiles catch it before a build does.
Index ¶
Constants ¶
const ( // MaxReadOctets is MAX_READ_OCTETS, the J1 limit. MaxReadOctets = specdata.MaxReadOctets // MaxDepth is MAX_DEPTH, the J6 limit, counting the outermost value as 1. MaxDepth = specdata.MaxDepth // MaxInteger is MAX_INTEGER, 2^53 - 1: J5 lets no integral value through // whose magnitude is above it and below 10^21. MaxInteger = specdata.MaxInteger )
The limits of SPEC.md section 9 that the rules use.
Variables ¶
var ErrUnreadable = errors.New("strictjson: the input cannot be read")
ErrUnreadable marks an input that could not be read at all, such as a missing file. It is not a verification result (SPEC.md section 12.2, exit code 1), so it is never a *Rejection.
Functions ¶
func CheckSize ¶
CheckSize is J1 on bytes in memory: nil when data is at most MAX_READ_OCTETS octets, and otherwise the rejection input_too_large.
func InIntegerGap ¶
InIntegerGap reports whether f is in J5's integer gap: integral, of magnitude above MAX_INTEGER and below 10^21. J5 refuses every such value, and no other finite value.
func Parse ¶
Parse is step PARSE, J2 to J6: the value data holds, or a *Rejection whose code is malformed_json. It does not make J1; Decode makes both.
Types ¶
type Number ¶
type Number float64
Number is a JSON number as J5 reads it: its nearest binary64 value, never -0, never infinite, and never an integer of magnitude above MAX_INTEGER and below 10^21. A Number the parser gives is always such a value; JCS (internal/jcs) refuses one that is not.
type Rejection ¶
type Rejection struct {
// Code is specdata.ReasonInputTooLarge or specdata.ReasonMalformedJSON.
Code specdata.Reason
// SubReason is empty for input_too_large, and for malformed_json one of
// syntax, invalid_utf8, lone_surrogate, duplicate_name, number and depth.
SubReason specdata.SubReason
// Offset is the octet at which the reader stopped. It is informational
// and never compared (SPEC.md section 13.1).
Offset int
}
Rejection is a bare rejection of step READ or PARSE (SPEC.md section 10.2): input_too_large with no sub_reason, or malformed_json with the sub_reason naming a rule the text breaks. For a text that breaks more than one rule, the sub_reason is that of the first this reader meets, which SPEC.md section 10.1 leaves open.