Skip to content
pubscout

canonical_json

1.1.3, released 9 months ago by google.dev

Encoder and decoder for canonical JSON format useful for cryptographic hashing and signing.

WASM-ready
$ flutter pub add canonical_json
Source repository

What it does

Provides tools to encode and decode canonical JSON format where maps are sorted by key, floats are not allowed, and formatting is strictly controlled. The decoder validates that the value is indeed canonically encoded.

AI-written summary of version 1.1.3, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to cryptographically hash or sign JSON objects consistently
  • You require deterministic JSON serialization with sorted keys and no whitespace

Skip it when

  • You need to encode or decode standard JSON containing floating-point numbers
  • You want a general-purpose JSON serializer that supports code generation

Strengths

  • Ensures deterministic JSON output for cryptographic use cases
  • Validates strict canonical JSON rules during decoding

Weaknesses

  • Does not support floating-point numbers
  • Requires strings to be in Unicode Normalization Form C

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
2
import 'package:canonical_json/canonical_json.dart';
void main() {
  final bytes = canonicalJson.encode({
    'from': 'alice',
    'message': 'Hi Bob',
  });
  final msg = canonicalJson.decode(bytes);
}

Alternatives

Packages that do the same job and could replace canonical_json.

  • webcrypto

    Cross-platform implementation of Web Cryptography APIs for Dart.

  • json_schema

    Validate JSON data against JSON Schema drafts in Dart.

  • cv

    Helpers to convert Dart objects to/from Map and JSON without code generation.

  • json_object_mapper

    Map plain Dart objects to and from JSON without annotations or code generation

Works with

Packages commonly used together with canonical_json: ones it builds on, ones built on it, and its own family.