strictjson

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 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 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

View Source
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

View Source
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

func CheckSize(data []byte) error

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 Decode

func Decode(data []byte) (any, error)

Decode is steps READ and PARSE in order: J1, then J2 to J6.

func InIntegerGap

func InIntegerGap(f float64) bool

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

func Parse(data []byte) (any, error)

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.

func Read

func Read(r io.Reader) ([]byte, error)

Read is step READ over r: it reads at most MAX_READ_OCTETS + 1 octets, which decides J1, and returns them when there are at most MAX_READ_OCTETS. More is the rejection input_too_large; a failed read wraps ErrUnreadable.

func ReadFile

func ReadFile(name string) ([]byte, error)

ReadFile is Read over the file at name, for the proof READ reads and for every keyring a run loads. A file that cannot be opened or read wraps ErrUnreadable.

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.

func (Number) Float64

func (n Number) Float64() float64

Float64 returns n as a float64.

func (Number) Int64

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

Int64 returns n as an int64 and true when n is an integer of magnitude at most MAX_INTEGER (SPEC.md section 9), the bound of the pattern class INT (section 2.4); otherwise 0 and false.

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.

func (*Rejection) Error

func (r *Rejection) Error() string

Error names the code, the sub_reason and the offset, and quotes nothing of the text.

Jump to

Keyboard shortcuts

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