All files / json / unstable_canonicalize.ts

100.00% Branches 33/33
100.00% Functions 6/6
100.00% Lines 49/49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
 
 
 
 
 
 
 
 
 
 
x5
 
x147
 
 
x147
x4
x4
 
x4
 
x147
 
x94
x147
 
 
 
 
 
x5
x19
 
x18
x19
x46
x46
x17
x19
 
 
 
 
 
 
x5
x5
x5
 
 
x147
 
x147
x147
x186
x186
x182
x182
 
x182
 
x140
x147
 
 
 
 
 
 
 
x5
x319
x319
 
 
x315
x4
x4
x166
 
x319
x319
x319
 
x319
x319
x319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x5
x98
x98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x5
x6
x6
































































































































































































// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.

import type { JsonPrimitive, JsonValue } from "./types.ts";
import type { Uint8Array_ } from "./_types.ts";

/**
 * Serializes a primitive JSON value (null, boolean, number, string) to its
 * canonical string representation per RFC 8785.
 */
function serializePrimitive(value: JsonPrimitive): string {
  // JSON.stringify handles null, boolean, and string correctly per RFC 8785
  if (typeof value !== "number") return JSON.stringify(value);

  // RFC 8785 Section 3.2.2.3: Numbers must conform to I-JSON (RFC 7493)
  if (!Number.isFinite(value)) {
    throw new TypeError(
      `Cannot canonicalize non-finite number: ${value} is not allowed in I-JSON`,
    );
  }
  // Handle -0 as "0" (RFC 8785 Section 3.2.2.3)
  if (Object.is(value, -0)) return "0";
  // ECMAScript Number-to-String for all other numbers
  return value.toString();
}

/**
 * Serializes an array to its canonical string representation.
 * Undefined elements become null (standard JSON behavior).
 */
function serializeArray(value: JsonValue[], ancestors: object[]): string {
  if (value.length === 0) return "[]";

  const parts: string[] = [];
  for (const elem of value) {
    parts.push(elem === undefined ? "null" : serializeValue(elem, ancestors));
  }
  return "[" + parts.join(",") + "]";
}

/**
 * Serializes an object to its canonical string representation.
 * Keys are sorted by UTF-16 code units (RFC 8785 Section 3.2.3).
 * Undefined values are skipped (standard JSON behavior, RFC 8785 Section 3.1).
 */
function serializeObject(
  value: { [key: string]: JsonValue | undefined },
  ancestors: object[],
): string {
  // Default sort uses UTF-16 code unit comparison per RFC 8785
  const keys = Object.keys(value).sort();

  const parts: string[] = [];
  for (const key of keys) {
    const propValue = value[key];
    if (propValue === undefined) continue;
    parts.push(
      JSON.stringify(key) + ":" + serializeValue(propValue, ancestors),
    );
  }

  return "{" + parts.join(",") + "}";
}

/**
 * Recursively serializes a JSON value to its canonical string representation.
 *
 * @param value The JSON value to serialize
 * @param ancestors Stack of ancestor objects for cycle detection
 */
function serializeValue(value: JsonValue, ancestors: object[] = []): string {
  if (value === null) return "null";
  if (typeof value !== "object") return serializePrimitive(value);

  // Circular reference detection: check if this object is an ancestor
  if (ancestors.includes(value)) {
    throw new TypeError("Converting circular structure to JSON");
  }
  ancestors.push(value);

  const result = Array.isArray(value)
    ? serializeArray(value, ancestors)
    : serializeObject(value, ancestors);

  ancestors.pop();
  return result;
}

/**
 * Serializes a JSON value to a canonical string per
 * {@link https://www.rfc-editor.org/rfc/rfc8785 | RFC 8785} JSON
 * Canonicalization Scheme (JCS).
 *
 * This produces a deterministic JSON string suitable for hashing or signing,
 * with object keys sorted lexicographically by UTF-16 code units and no
 * whitespace between tokens.
 *
 * Note: The input must be JSON-compatible data. Objects with `toJSON()` methods
 * (like `Date`) should be converted to their JSON representation first.
 *
 * @experimental **UNSTABLE**: New API, yet to be vetted.
 *
 * @param value The JSON value to canonicalize.
 * @returns The canonical JSON string.
 *
 * @example Basic usage (RFC 8785 Appendix E inspired)
 * ```ts
 * import { canonicalize } from "@std/json/unstable-canonicalize";
 * import { assertEquals } from "@std/assert";
 *
 * // Keys are sorted lexicographically, no whitespace between tokens
 * const data = {
 *   time: "2019-01-28T07:45:10Z",
 *   big: "055",
 *   val: 3.5,
 * };
 * assertEquals(canonicalize(data), '{"big":"055","time":"2019-01-28T07:45:10Z","val":3.5}');
 * ```
 *
 * @example Number serialization (RFC 8785 Section 3.2.2.3)
 * ```ts
 * import { canonicalize } from "@std/json/unstable-canonicalize";
 * import { assertEquals } from "@std/assert";
 *
 * // Numbers follow ECMAScript serialization rules
 * assertEquals(canonicalize(10.0), "10");           // No unnecessary decimals
 * assertEquals(canonicalize(1e21), "1e+21");        // Scientific notation for large
 * assertEquals(canonicalize(0.0000001), "1e-7");    // Scientific notation for small
 * assertEquals(canonicalize(-0), "0");              // Negative zero becomes "0"
 * ```
 *
 * @example Key sorting by UTF-16 code units (RFC 8785 Section 3.2.3)
 * ```ts
 * import { canonicalize } from "@std/json/unstable-canonicalize";
 * import { assertEquals } from "@std/assert";
 *
 * // Keys sorted by UTF-16 code units: digits < uppercase < lowercase
 * const data = { a: 1, A: 2, "1": 3 };
 * assertEquals(canonicalize(data), '{"1":3,"A":2,"a":1}');
 * ```
 *
 * @throws {TypeError} If the value contains non-finite numbers (Infinity, -Infinity, NaN).
 * @throws {TypeError} If the value contains circular references.
 *
 * @see {@link https://www.rfc-editor.org/rfc/rfc8785 | RFC 8785}
 */
export function canonicalize(value: JsonValue): string {
  return serializeValue(value);
}

/**
 * Serializes a JSON value to canonical UTF-8 bytes per
 * {@link https://www.rfc-editor.org/rfc/rfc8785 | RFC 8785} JSON
 * Canonicalization Scheme (JCS).
 *
 * This is equivalent to `new TextEncoder().encode(canonicalize(value))` and
 * is provided as a convenience for cryptographic operations that require
 * byte input.
 *
 * @experimental **UNSTABLE**: New API, yet to be vetted.
 *
 * @param value The JSON value to canonicalize.
 * @returns The canonical JSON as UTF-8 bytes.
 *
 * @example Creating a verifiable hash
 * ```ts
 * import { canonicalizeToBytes } from "@std/json/unstable-canonicalize";
 * import { encodeHex } from "@std/encoding/hex";
 * import { assertEquals } from "@std/assert";
 *
 * async function sha256Hex(data: Uint8Array): Promise<string> {
 *   const hash = await crypto.subtle.digest("SHA-256", data.buffer as ArrayBuffer);
 *   return encodeHex(new Uint8Array(hash));
 * }
 *
 * // Create a deterministic hash of JSON data for verification
 * const payload = { action: "transfer", amount: 100, to: "alice" };
 * const hash = await sha256Hex(canonicalizeToBytes(payload));
 *
 * // Same hash regardless of original key order
 * const reordered = { to: "alice", action: "transfer", amount: 100 };
 * const reorderedHash = await sha256Hex(canonicalizeToBytes(reordered));
 *
 * assertEquals(hash, reorderedHash);
 * ```
 *
 * @throws {TypeError} If the value contains non-finite numbers (Infinity, -Infinity, NaN).
 * @throws {TypeError} If the value contains circular references.
 *
 * @see {@link https://www.rfc-editor.org/rfc/rfc8785 | RFC 8785}
 */
export function canonicalizeToBytes(value: JsonValue): Uint8Array_ {
  return new TextEncoder().encode(canonicalize(value));
}