canonical_json 1.0.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new50

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!');
    }
    print(msg['message']);
  } 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.

example/main.dart

// 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
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 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:


dependencies:
  canonical_json: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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';
  
Version Uploaded Documentation Archive
1.0.0 Mar 26, 2019 Go to the documentation of canonical_json 1.0.0 Download canonical_json 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

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

  • Dart: 2.2.0
  • pana: 0.12.14

Platforms

Detected platforms: Flutter, web, other

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

Dependencies

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