canonicalJsonEncode function
Encodes the restricted Dune Canonical JSON v1 profile.
This encoder is intentionally smaller than general JSON:
- supported values are null, bool, String, finite num, List,
Map<String, ...>, and
Serializablemodels whosetoJson()returns supported values; - object keys are sorted lexicographically;
- arrays preserve order;
- no whitespace is emitted;
- non-finite numbers, non-string map keys, and custom objects are rejected.
The profile is used for signature- and hash-covered protocol payloads where every implementation must produce identical bytes for the same logical data.
Implementation
String canonicalJsonEncode(Object? value) {
final buffer = StringBuffer();
_writeCanonicalJson(buffer, value);
return buffer.toString();
}