jcs

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package jcs implements RFC 8785 JSON Canonicalization Scheme serialization.

Given a parsed Value tree (from jcstoken), this package produces the exact canonical byte sequence specified by RFC 8785. It depends on jcsfloat for ECMA-262-compliant number serialization and uses UTF-16 code-unit ordering for object property name sorting as required by RFC 8785 §3.2.3.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Canonicalize added in v0.3.0

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

Canonicalize parses JSON input and produces the RFC 8785 JCS canonical byte sequence. It is equivalent to calling jcstoken.Parse followed by Serialize.

API-CANON-001: Output is identical to Parse followed by Serialize.

Example
package main

import (
	"fmt"
	"log"

	"github.com/lattice-substrate/json-canon/jcs"
)

func main() {
	input := []byte(`{ "b" : 2, "a" : 1 }`)
	canonical, err := jcs.Canonicalize(input)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(canonical))
}
Output:

{"a":1,"b":2}

func CanonicalizeWithOptions added in v0.3.0

func CanonicalizeWithOptions(input []byte, opts *jcstoken.Options) ([]byte, error)

CanonicalizeWithOptions is like Canonicalize but accepts parser options.

API-CANON-002: Options are passed through to ParseWithOptions.

func Serialize

func Serialize(v *jcstoken.Value) ([]byte, error)

Serialize produces the RFC 8785 JCS canonical byte sequence for a parsed JSON value.

CANON-ENC-001: Output is UTF-8. CANON-WS-001: No insignificant whitespace.

Example
package main

import (
	"fmt"
	"log"

	"github.com/lattice-substrate/json-canon/jcs"
	"github.com/lattice-substrate/json-canon/jcstoken"
)

func main() {
	input := []byte(`{ "z" : true, "a" : [3, 1] }`)
	v, err := jcstoken.Parse(input)
	if err != nil {
		log.Fatal(err)
	}
	canonical, err := jcs.Serialize(v)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(canonical))
}
Output:

{"a":[3,1],"z":true}

func SerializeWithOptions added in v0.3.0

func SerializeWithOptions(v *jcstoken.Value, opts *jcstoken.Options) ([]byte, error)

SerializeWithOptions is like Serialize but validates the value tree against caller-supplied bounds before canonical emission.

Types

This section is empty.

Jump to

Keyboard shortcuts

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