JSON.stringify does not promise that: property order, whitespace, and number formatting can all differ between producers. RFC 8785 (the JSON Canonicalization Scheme, JCS) fixes one serialization, and @goloco/jcs is Goloco’s implementation of it.
It is a vendored copy of the reference JavaScript implementation the RFC itself lists in Appendix G — the canonicalize npm package by two of the RFC’s authors — pinned to version 4.0.0 and shipped with zero runtime dependencies. The package’s VENDORED.md records the exact source, version, and integrity hash.
Install
@goloco/jcs is not published to npm yet. Inside a Goloco source checkout it is a workspace package, so you can import it directly:
Use
canonicalize(value: unknown): string | undefined returns undefined only where JSON.stringify would (a top-level undefined, function, or symbol).
What the RFC guarantees
- No whitespace between tokens.
- Property names sorted by UTF-16 code unit, recursively, including objects nested in arrays. Array element order is never changed.
- Numbers in ECMAScript
Number.prototype.toStringform:1e+30,0.000001,9007199254740992.-0serializes as0. - Strings escaped one way:
\b \t \n \f \rfor those five control characters, lowercase\u00hhfor the other control characters,\"and\\, and every other code point literal (no\/, no\uXXXXfor non-ASCII). - Errors, not output, for NaN, Infinity, lone surrogates, and circular structures. A compliant canonicalizer must refuse them, and this one throws.
Conformance
The package’s test suite runs the RFC author’s published test vectors against the vendored copy, byte for byte, plus the RFC’s Appendix B number table and its sorting sample. Every expectation comes from the RFC or its vectors, never from the serializer under test.Where it is used
@goloco/jcs is the only canonical-JSON entry point for new Goloco code. The terms-hashing pipeline (the hash a quote’s termsHash refers to, see SDK usage) migrates onto it in a later change; until then, quote and funding flows keep their current serializer and this package is additive.