-
Notifications
You must be signed in to change notification settings - Fork 15
/
encode.dart
43 lines (35 loc) · 835 Bytes
/
encode.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Package : Cbor
* Author : S. Hamblett <steve.hamblett@linux.com>
* Date : 04/01/2022
* Copyright : S.Hamblett
*/
import 'package:cbor/cbor.dart';
/// A simple encoding sequence followed by a self decode and a pretty print.
int main() {
final value = CborValue([
[1, 2, 3], // Writes an array
CborBytes([0x00]), // Writes a byte string
67.89,
10,
// You can encode maps with any value encodable as CBOR as key.
{
1: 'one',
2: 'two',
},
'hello',
// Indefinite length string
CborEncodeIndefiniteLengthString([
'hello',
' ',
'world',
]),
]);
final bytes = cbor.encode(value);
final _ = cbor.decode(bytes);
// Pretty print
print(cborPrettyPrint(bytes));
// Print json
print(const CborJsonEncoder().convert(value));
return 0;
}