jsoncanonicalizer

package
v1.5.7 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

TODO: This package is written by owner of RFC 8785, consider to replace with json/v2 when go 1.25 is released

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NumberToJSON

func NumberToJSON(ieeeF64 float64) (res string, err error)

NumberToJSON converts an IEEE-754 double precision floating-point number to its JSON string representation according to ECMAScript 6 specification.

This function is used for JSON canonicalization, ensuring consistent number formatting regardless of platform or implementation differences. The output format follows the ES6-JSON/JCS (JSON Canonicalization Scheme) specification.

Special handling:

  • NaN and Infinity are invalid in JSON and return an error
  • Negative zero (-0) is normalized to positive zero (0)
  • Numbers use 'f' format for values between 1e-6 and 1e+21, 'e' format otherwise
  • Exponent notation is normalized (e.g., "1e+09" becomes "1e+9")

The ieeeF64 parameter is the IEEE-754 double precision number to convert.

Returns the JSON-compliant string representation of the number, or an error if the number is invalid (NaN or Infinity).

func Transform

func Transform(jsonData []byte) (result []byte, e error)

Transform canonicalizes JSON data according to RFC 8785 (JSON Canonicalization Scheme).

Canonicalization ensures that semantically equivalent JSON documents produce identical byte sequences, enabling consistent hashing and digital signatures. This is critical for DID Document hashing, where the same document must always produce the same hash regardless of formatting differences.

The transformation process:

  • Removes all whitespace
  • Sorts object members lexicographically by their UTF-16 code units
  • Normalizes number formatting (ES6 format)
  • Normalizes string escaping (UTF-16 surrogate pairs)
  • Ensures consistent representation of JSON literals

The jsonData parameter must be valid UTF-8 encoded JSON. The input is parsed and transformed into canonical form.

Returns the canonicalized JSON as a byte slice, or an error if:

  • The input is not valid JSON
  • The input contains non-ASCII characters (must be UTF-8)
  • The JSON structure is malformed

Example use case: Hashing DID Documents for blockchain transactions.

Types

This section is empty.

Jump to

Keyboard shortcuts

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