canonical_json 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

Canonical JSON Encoder/Decoder #

This package provides an encoder and decoder for encoding/decoding canonical JSON. The canonical JSON format does not support floating-point numbers and the decoder validates that the value is indeed a canoncially encoded JSON value.

Disclaimer: This is not an officially supported Google product.

Example #

import 'package:canonical_json/canonical_json.dart';
void main() {
  // Encode a message
  final bytes = canonicalJson.encode({
    'from': 'alice',
    'message': 'Hi Bob',
  final hash = sha256(bytes); // using a sha256 from some library...
  // Decode message
  try {
    final msg = canonicalJson.decode(bytes);
    if (!fixedTimeEqual(sha256(canonicalJson.encode(msg)), bytes)) {
      print('Expected a different hash!');
  } on InvalidCanonicalJsonException {
    print('Message was not canonical JSON!');

Canonical JSON Format #

  • Integers are encoded without leading zeros.
  • Floating point numbers are not permitted.
  • Map keys appear in sorted by byte values.
  • Whitespace is not permitted (outside of strings).
  • Only allow escape sequences in strings are \\ and \".
  • Strings must be valid UTF-8 in Unicode Normalization Form C.

This follows the rules outlined in canoncial JSON, with the deviation that this package requires strings to be encoded as valid UTF-8 in Unicode Normalization Form C rather than arbitrary byte values. This is only recommended by canoncial JSON, but this library takes the oppinion that binary values should be encoded.

See Also #

v1.0.0 #

  • Initial release.


// Copyright 2019 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:canonical_json/canonical_json.dart';

void main() {
  // Encode a message
  final bytes = canonicalJson.encode({
    'from': 'alice',
    'message': 'Hi Bob',

  // Decode message
  try {
    final msg = canonicalJson.decode(bytes) as Map<String, Object>;
    print('message is: ${msg['message']}');
  } on InvalidCanonicalJsonException {
    print('Message was not canonical JSON!');

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  canonical_json: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:canonical_json/canonical_json.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Oct 11, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:canonical_json/canonical_json.dart.

Health suggestions

Format lib/src/encoder.dart.

Run dartfmt to format lib/src/encoder.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
typed_data ^1.1.6 1.1.6
unorm_dart ^0.1.1 0.1.1
Dev dependencies
pedantic ^1.4.0
test ^1.5.1