jcs

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 3 Imported by: 0

README

jcs

import "github.com/kitsunium/sdk/pkg/v1/data/codec/jcs"

Package jcs writes the JSON Canonicalization Scheme of RFC 8785: one byte form for a JSON document — members sorted, no whitespace, numbers and strings spelled one way — so it can be signed or hashed, and checked by anyone who canonicalizes the same document.

canonical, err := jcs.Canonicalize(document)
digest := sha256.Sum256(canonical)

Marshal canonicalizes a Go value's encoding/json/v2 encoding. The document must be one I-JSON value (RFC 7493) — valid UTF-8, no duplicated member name — or Canonicalize returns NotJSON, naming the offset and never a byte of the document; a number past the largest finite double is NumberInvalid. It is not a codec Format: a canonical form is a policy over JSON, as strictjson is a policy over decoding.

Index

Constants

const CodeMarshalFailed errs.Code = 222723

CodeMarshalFailed is the code of MarshalFailed (0.3.102.3).

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jcs.CodeJCSMarshalFailed.

Code 0.3.102.3

const CodeNotJSON errs.Code = 222721

CodeNotJSON is the code of NotJSON (0.3.102.1), for errs.HasCode.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jcs.CodeJCSNotJSON.

Code 0.3.102.1

const CodeNumberInvalid errs.Code = 222722

CodeNumberInvalid is the code of NumberInvalid (0.3.102.2).

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jcs.CodeJCSNumberInvalid.

Code 0.3.102.2

Variables

var MarshalFailed *errs.Error

MarshalFailed wraps encoding/json/v2's refusal of a value.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jcs.MarshalFailed.

Code 0.3.102.3 · reason MARSHAL_FAILED · public "The value has no JSON encoding"

var NotJSON *errs.Error

NotJSON refuses a document that is not exactly one I-JSON value; its field names the offset, never its content.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jcs.NotJSON.

Code 0.3.102.1 · reason NOT_JSON · public "The document is not a JSON value"

var NumberInvalid *errs.Error

NumberInvalid refuses a number past the largest finite double.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jcs.NumberInvalid.

Code 0.3.102.2 · reason NUMBER_INVALID · public "A number of the document has no canonical form"

func Canonicalize

func Canonicalize(doc []byte) ([]byte, error)

Canonicalize returns the RFC 8785 canonical form of the JSON document doc, or NotJSON, or NumberInvalid.

func Marshal

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

Marshal returns the RFC 8785 canonical form of v's encoding/json/v2 encoding, or MarshalFailed.

Generated from docs/api by tools/genindex.

Documentation

Overview

Package jcs writes the JSON Canonicalization Scheme of RFC 8785: one byte form for a JSON document — members sorted, no whitespace, numbers and strings spelled one way — so it can be signed or hashed, and checked by anyone who canonicalizes the same document.

canonical, err := jcs.Canonicalize(document)
digest := sha256.Sum256(canonical)

Marshal canonicalizes a Go value's encoding/json/v2 encoding. The document must be one I-JSON value (RFC 7493) — valid UTF-8, no duplicated member name — or Canonicalize returns NotJSON, naming the offset and never a byte of the document; a number past the largest finite double is NumberInvalid. It is not a codec Format: a canonical form is a policy over JSON, as strictjson is a policy over decoding.

Index

Constants

View Source
const (
	// CodeNotJSON is the code of [NotJSON] (0.3.102.1), for errs.HasCode.
	CodeNotJSON errs.Code = corejcs.CodeJCSNotJSON
	// CodeNumberInvalid is the code of [NumberInvalid] (0.3.102.2).
	CodeNumberInvalid errs.Code = corejcs.CodeJCSNumberInvalid
	// CodeMarshalFailed is the code of [MarshalFailed] (0.3.102.3).
	CodeMarshalFailed errs.Code = corejcs.CodeJCSMarshalFailed
)

The error codes, range 0.3.102.*, declared in the core (ADR 0160).

Variables

View Source
var (
	// NotJSON refuses a document that is not exactly one I-JSON value; its field
	// names the offset, never its content.
	NotJSON = corejcs.NotJSON
	// NumberInvalid refuses a number past the largest finite double.
	NumberInvalid = corejcs.NumberInvalid
	// MarshalFailed wraps encoding/json/v2's refusal of a value.
	MarshalFailed = corejcs.MarshalFailed
)

The sentinels, for errors.Is: each carries the code of the same name.

Functions

func Canonicalize

func Canonicalize(doc []byte) ([]byte, error)

Canonicalize returns the RFC 8785 canonical form of the JSON document doc, or NotJSON, or NumberInvalid.

func Marshal

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

Marshal returns the RFC 8785 canonical form of v's encoding/json/v2 encoding, or MarshalFailed.

Types

This section is empty.

Jump to

Keyboard shortcuts

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