Skip to content

Commit 083ff9e

Browse files
committed
Migrate canonical_json to null-safety
1 parent ca7d7b4 commit 083ff9e

8 files changed

+28
-24
lines changed

canonical_json/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v1.1.0
2+
* Migrated to null-safety.
3+
14
## v1.0.0
25
* Initial release.
36

canonical_json/lib/canonical_json.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,4 @@ export 'src/exceptions.dart' show InvalidCanonicalJsonException;
8383
/// }
8484
/// }
8585
/// ```
86-
const Codec<Object, List<int>> canonicalJson = CanonicalJsonCodec();
86+
const Codec<Object?, List<int>> canonicalJson = CanonicalJsonCodec();

canonical_json/lib/src/codec.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'dart:convert' show Codec;
1616
import 'decoder.dart' show CanonicalJsonDecoder;
1717
import 'encoder.dart' show CanonicalJsonEncoder;
1818

19-
class CanonicalJsonCodec extends Codec<Object, List<int>> {
19+
class CanonicalJsonCodec extends Codec<Object?, List<int>> {
2020
const CanonicalJsonCodec();
2121

2222
@override

canonical_json/lib/src/decoder.dart

+10-10
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import 'utils.dart' show char, RawMapEntry, ensureUint8List;
1818
import 'exceptions.dart' show InvalidCanonicalJsonException;
1919
import 'fast_unorm.dart' show fastNfc;
2020

21-
class CanonicalJsonDecoder extends Converter<List<int>, Object> {
21+
class CanonicalJsonDecoder extends Converter<List<int>, Object?> {
2222
@override
23-
Object convert(List<int> input) {
23+
Object? convert(List<int> input) {
2424
ArgumentError.checkNotNull(input, 'input');
2525
final data = ensureUint8List(input);
2626
return Decoder(data).decode();
@@ -42,7 +42,7 @@ bool _isAscii(String s) => s.split('').every((c) => char(c) < 128);
4242

4343
class _RawMapEntry extends RawMapEntry {
4444
final int offset;
45-
_RawMapEntry(Uint8List key, Object value, this.offset) : super(key, value);
45+
_RawMapEntry(Uint8List key, Object? value, this.offset) : super(key, value);
4646
}
4747

4848
class Decoder {
@@ -70,21 +70,21 @@ class Decoder {
7070
}
7171
}
7272

73-
InvalidCanonicalJsonException _fail(String message, [int offset]) =>
73+
InvalidCanonicalJsonException _fail(String message, [int? offset]) =>
7474
_InvalidCanonicalJsonException(_data, offset ?? _offset, message);
7575

7676
int get _value => _offset < _data.length ? _data[_offset] : -1;
7777
int get _peak => _offset + 1 < _data.length ? _data[_offset + 1] : -1;
7878

79-
Object decode() {
79+
Object? decode() {
8080
final result = _readValue();
8181
if (_data.length != _offset) {
8282
throw _fail('expected end of input');
8383
}
8484
return result;
8585
}
8686

87-
Object _readValue() {
87+
Object? _readValue() {
8888
// 1-9, leading zeros are not allowed
8989
if (char('1') <= _value && _value <= char('9')) {
9090
return _readInt();
@@ -168,8 +168,8 @@ class Decoder {
168168
}
169169

170170
/// Read a list, assumes _value is `[`.
171-
List<Object> _readList() {
172-
final result = <Object>[];
171+
List<Object?> _readList() {
172+
final result = <Object?>[];
173173
assert(_value == char('['));
174174
_offset++;
175175
if (_try(']')) {
@@ -183,7 +183,7 @@ class Decoder {
183183
}
184184

185185
/// Read a map, assumes value is `{`.
186-
Map<String, Object> _readMap() {
186+
Map<String, Object?> _readMap() {
187187
assert(_value == char('{'));
188188
_offset++;
189189
if (_try('}')) {
@@ -207,7 +207,7 @@ class Decoder {
207207
}
208208
}
209209
// Create object from entries and validate utf-8 encoding of keys.
210-
final result = <String, Object>{};
210+
final result = <String, Object?>{};
211211
for (final entry in entries) {
212212
result[_decodeString(entry.key, entry.offset)] = entry.value;
213213
}

canonical_json/lib/src/encoder.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ import 'package:typed_data/typed_data.dart' show Uint8Buffer;
1818
import 'utils.dart' show char, RawMapEntry;
1919
import 'fast_unorm.dart' show fastNfc;
2020

21-
class CanonicalJsonEncoder extends Converter<Object, Uint8List> {
21+
class CanonicalJsonEncoder extends Converter<Object?, Uint8List> {
2222
@override
23-
Uint8List convert(Object input) {
23+
Uint8List convert(Object? input) {
2424
final result = Uint8Buffer();
2525
_encode(result, input, input);
2626
return Uint8List.view(result.buffer, 0, result.length);
2727
}
2828
}
2929

30-
void _encode(Uint8Buffer output, Object input, Object original) {
30+
void _encode(Uint8Buffer output, Object? input, Object? original) {
3131
// Handle null, true, false
3232
if (input == null) {
3333
output.addAll(ascii.encode('null'));
@@ -70,7 +70,7 @@ void _encode(Uint8Buffer output, Object input, Object original) {
7070
}
7171

7272
// Handle lists by calling recursively.
73-
if (input is List<Object>) {
73+
if (input is List<Object?>) {
7474
output.add(char('['));
7575
for (var i = 0; i < input.length; i++) {
7676
if (i > 0) {
@@ -83,7 +83,7 @@ void _encode(Uint8Buffer output, Object input, Object original) {
8383
}
8484

8585
// Handle maps from string to anything.
86-
if (input is Map<String, Object>) {
86+
if (input is Map<String, Object?>) {
8787
final entries = RawMapEntry.fromMap(input).toList();
8888
entries.sort(RawMapEntry.compare);
8989
output.add(char('{'));

canonical_json/lib/src/utils.dart

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ int char(String character) {
2828

2929
class RawMapEntry {
3030
final Uint8List key;
31-
final Object value;
31+
final Object? value;
3232
RawMapEntry(this.key, this.value) {
3333
assert(key != null);
3434
}
3535

36-
static Iterable<RawMapEntry> fromMap(Map<String, Object> map) sync* {
36+
static Iterable<RawMapEntry> fromMap(Map<String, Object?> map) sync* {
3737
for (final e in map.entries) {
3838
final s = fastNfc(e.key).replaceAll(r'\', r'\\').replaceAll('"', r'\"');
3939
final b = utf8.encode(s);
@@ -46,7 +46,6 @@ class RawMapEntry {
4646
}
4747

4848
static int compare(RawMapEntry a, RawMapEntry b) {
49-
assert(a != null && b != null, 'a and b should never be null');
5049
final N = math.min(a.key.length, b.key.length);
5150
for (var i = 0; i < N; i++) {
5251
final r = a.key[i].compareTo(b.key[i]);

canonical_json/pubspec.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: canonical_json
2-
version: 1.0.0
2+
version: 1.1.0
33
description: |
44
Encoder and decoder for a canonical JSON format, useful when cryptographically
55
hashing or signing JSON objects.
@@ -8,9 +8,9 @@ repository: https://github.com/google/dart-neats.git
88
issue_tracker: https://github.com/google/dart-neats/labels/pkg:canonical_json
99
dependencies:
1010
typed_data: ^1.1.6
11-
unorm_dart: ^0.1.1
11+
unorm_dart: ^0.2.0
1212
dev_dependencies:
1313
test: ^1.5.1
1414
pedantic: ^1.4.0
1515
environment:
16-
sdk: '>=2.0.0 <3.0.0'
16+
sdk: '>=2.12.0 <3.0.0'

canonical_json/test/canonical_json_test.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ void main() {
7575
expect(decoded, equals(value));
7676
final encodedAgain = canonicalJson.encode(decoded);
7777
expect(encodedAgain, encoded);
78-
final decodedJson = json.fuse(utf8).decode(encoded);
78+
// TODO: Use json.fuse(utf8) when: http://dartbug.com/46205 is fixed!
79+
// final decodedJson = json.fuse(utf8).decode(encoded);
80+
final decodedJson = json.decode(utf8.decode(encoded));
7981
expect(decodedJson, equals(value));
8082
}));
8183

0 commit comments

Comments
 (0)