├── test
├── data
│ ├── integer.cbor
│ ├── map.cbor
│ ├── floats.cbor
│ ├── tagged_date.cbor
│ ├── indef_string.cbor
│ └── nested_array.cbor
├── .gitignore
├── issues
│ ├── issue62
│ │ ├── raw.txt
│ │ ├── issue62.dart
│ │ ├── decoded.txt
│ │ └── jsonconverted.txt
│ ├── issue41_test.dart
│ ├── issue18_test.dart
│ ├── issue75
│ │ ├── issue75.dart
│ │ └── long-decode-file.txt
│ ├── issue9_test.dart
│ └── issue51_test.dart
├── datetime_test.dart
├── int_test.dart
├── rational_number_test.dart
├── json_encoder_test.dart
├── filebased_test.dart
├── supplementary_test.dart
└── simple_encoder_rfc_test.dart
├── example
├── example.md
├── encode.dart
└── stream_decode.dart
├── sbom.yaml
├── AUTHORS
├── .gitignore
├── lib
├── simple.dart
├── src
│ ├── error.dart
│ ├── codec.dart
│ ├── constants.dart
│ ├── decoder
│ │ ├── stage2.dart
│ │ ├── stage0.dart
│ │ ├── decoder.dart
│ │ ├── pretty_print.dart
│ │ └── stage1.dart
│ ├── encoder
│ │ ├── encoder.dart
│ │ └── sink.dart
│ ├── json.dart
│ ├── utils
│ │ ├── arg.dart
│ │ └── utils.dart
│ ├── value
│ │ ├── internal.dart
│ │ ├── simple_value.dart
│ │ ├── int.dart
│ │ ├── map.dart
│ │ ├── double.dart
│ │ ├── bytes.dart
│ │ ├── value.dart
│ │ ├── list.dart
│ │ └── string.dart
│ └── simple.dart
└── cbor.dart
├── pubspec.yaml
├── analysis_options.yaml
├── .github
└── workflows
│ ├── ci.yml
│ └── pana.yml
├── LICENSE
├── CHANGELOG.md
├── README.md
└── sbom.spdx
/test/data/integer.cbor:
--------------------------------------------------------------------------------
1 | *
--------------------------------------------------------------------------------
/test/.gitignore:
--------------------------------------------------------------------------------
1 | data/*.out
2 |
--------------------------------------------------------------------------------
/test/data/map.cbor:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shamblett/cbor/HEAD/test/data/map.cbor
--------------------------------------------------------------------------------
/test/data/floats.cbor:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shamblett/cbor/HEAD/test/data/floats.cbor
--------------------------------------------------------------------------------
/test/data/tagged_date.cbor:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shamblett/cbor/HEAD/test/data/tagged_date.cbor
--------------------------------------------------------------------------------
/test/data/indef_string.cbor:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shamblett/cbor/HEAD/test/data/indef_string.cbor
--------------------------------------------------------------------------------
/test/data/nested_array.cbor:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shamblett/cbor/HEAD/test/data/nested_array.cbor
--------------------------------------------------------------------------------
/test/issues/issue62/raw.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shamblett/cbor/HEAD/test/issues/issue62/raw.txt
--------------------------------------------------------------------------------
/example/example.md:
--------------------------------------------------------------------------------
1 | ## Please refer to the example Dart files in this directory and the README for usage examples.
--------------------------------------------------------------------------------
/sbom.yaml:
--------------------------------------------------------------------------------
1 | type: spdx
2 |
3 | spdx:
4 | SPDXFormat: 'tagvalue'
5 | documentCreation:
6 | CreatorComment: 'Please refer to the AUTHORS file for contributor/creation details'
--------------------------------------------------------------------------------
/AUTHORS:
--------------------------------------------------------------------------------
1 | # A non-exhaustive list of contributors to the Dart CBOR package.
2 | # Names to be added alphabetically.
3 | #
4 | # For a complete list, see the git revision history.
5 |
6 | N. Nattis
7 | S. Hamblett
8 |
--------------------------------------------------------------------------------
/test/issues/issue41_test.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 02/04/2020
5 | * Copyright : S.Hamblett
6 | */
7 | import 'package:cbor/cbor.dart';
8 | import 'package:test/test.dart';
9 |
10 | void main() {
11 | test('63254', () {
12 | final decoded = cbor.decode([25, 247, 22]);
13 |
14 | expect(decoded, CborSmallInt(63254));
15 | });
16 | }
17 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Files and directories created by pub
2 | .packages
3 | .pub/
4 | build/
5 |
6 | # Remove the following pattern if you wish to check in your lock file
7 | pubspec.lock
8 |
9 | # Files created by dart2js
10 | *.part.js
11 | *.js.deps
12 | *.info.json
13 |
14 | # Directory created by dartdoc
15 | doc/api/
16 |
17 | # JetBrains IDEs
18 | .idea/
19 | *.ipr
20 | *.iws
21 |
22 | # Dart 2 additions
23 | .dart_tool/
24 |
--------------------------------------------------------------------------------
/lib/simple.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | /// This API exposes [CborSimpleCodec], which allows converting from CBOR
9 | /// to Dart objects without `CborValue`.
10 | ///
11 | /// Everything this API exposes, except the [cbor] constant, is exposed
12 | /// from the advanced API too.
13 | library;
14 |
15 | import 'src/simple.dart';
16 |
17 | export 'src/simple.dart';
18 |
19 | /// A constant instance of [CborSimpleCodec].
20 | const CborSimpleCodec cbor = CborSimpleCodec();
21 |
--------------------------------------------------------------------------------
/test/issues/issue18_test.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 02/04/2020
5 | * Copyright : S.Hamblett
6 | */
7 | import 'package:cbor/cbor.dart';
8 | import 'package:test/test.dart';
9 |
10 | void main() {
11 | test('Uint8List in array', () {
12 | final encoded = cbor.encode(
13 | CborMap({
14 | CborString('data'): CborList([
15 | CborBytes([196, 79, 51]),
16 | ]),
17 | }),
18 | );
19 | expect(encoded, [161, 100, 100, 97, 116, 97, 129, 67, 196, 79, 51]);
20 | });
21 | }
22 |
--------------------------------------------------------------------------------
/pubspec.yaml:
--------------------------------------------------------------------------------
1 | name: cbor
2 | description: A CBOR library for Dart. An RFC8949 compliant encoding/decoding CBOR implementation.
3 | version: 6.3.7
4 | repository: https://github.com/shamblett/cbor
5 | homepage: https://github.com/shamblett/cbor
6 |
7 | funding:
8 | - https://www.darticulate.com/#funding
9 |
10 | environment:
11 | sdk: '>=3.7.0 <4.0.0'
12 |
13 | dependencies:
14 | collection: '^1.17.2'
15 | typed_data: '^1.3.2'
16 | hex: '^0.2.0'
17 | convert: '^3.1.2'
18 | ieee754: '^1.0.3'
19 | meta: '^1.9.1'
20 | characters: '^1.4.0'
21 |
22 | dev_dependencies:
23 | test: '^1.25.14'
24 | lints: '^5.1.1'
25 | dart_code_metrics_presets: '^2.21.0'
26 |
--------------------------------------------------------------------------------
/test/issues/issue62/issue62.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 02/04/2020
5 | * Copyright : S.Hamblett
6 | */
7 | import 'dart:io';
8 |
9 | import 'package:cbor/cbor.dart';
10 | import 'package:test/test.dart';
11 |
12 | void main() {
13 | test('Weird JSON conversion', () async {
14 | final currDir = Directory.current.path;
15 | final f = File('$currDir/test/issue62/raw.txt');
16 | final decoded = await f.openRead().transform(cbor.decoder).single;
17 | expect(decoded.toString().isNotEmpty, isTrue);
18 | final jsonEncoder = CborJsonEncoder();
19 | final json = jsonEncoder.convert(decoded);
20 | print(json);
21 | });
22 | }
23 |
--------------------------------------------------------------------------------
/analysis_options.yaml:
--------------------------------------------------------------------------------
1 | # This file allows you to configure the Dart analyzer.
2 | #
3 | # The commented part below is just for inspiration. Read the guide here:
4 | # https://www.dartlang.org/guides/language/analysis-options
5 | include: package:lints/recommended.yaml
6 |
7 | analyzer:
8 | exclude:
9 |
10 | dart_code_metrics:
11 | exclude:
12 | metrics:
13 | - test/**
14 | - example/**
15 | rules:
16 | - test/**
17 | - example/**
18 | extends:
19 | - package:dart_code_metrics_presets/dart_all.yaml
20 | rules:
21 | - avoid-dynamic : false
22 | - avoid-non-null-assertion : false
23 | - newline-before-return : false
24 | - avoid-late-keyword : false
25 | - prefer-match-file-name : false
26 |
--------------------------------------------------------------------------------
/lib/src/error.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | /// An exception raised when decoding.
9 | class CborMalformedException extends FormatException {
10 | const CborMalformedException(super.message, [int? super.offset]);
11 |
12 | @override
13 | String toString() {
14 | var string = 'Malformed CBOR';
15 | if (offset != null) {
16 | string += ' at $offset';
17 | }
18 | string += ': $message';
19 |
20 | return string;
21 | }
22 | }
23 |
24 | /// Raised when object could not be codified due to cyclic references.
25 | class CborCyclicError extends Error {
26 | final Object cause;
27 |
28 | CborCyclicError(this.cause);
29 |
30 | @override
31 | String toString() {
32 | return 'Cbor cyclic error';
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: Build Status
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | pull_request:
7 | branches: [ master ]
8 |
9 | jobs:
10 | build:
11 | runs-on: ubuntu-latest
12 |
13 | steps:
14 | - uses: actions/checkout@v2
15 | - uses: dart-lang/setup-dart@v1
16 |
17 | - name: Install dependencies
18 | run: dart pub get
19 |
20 | - name: Verify formatting
21 | run: dart format --output=none --set-exit-if-changed .
22 |
23 | - name: Analyze project source
24 | run: dart analyze
25 |
26 | - name: Run tests with coverage
27 | run: dart run coverage:test_with_coverage
28 |
29 | - name: Upload to codecov.io
30 | uses: codecov/codecov-action@v2
31 | with:
32 | token: ${{ secrets.CODECOV_TOKEN }}
33 | file: coverage/lcov.info
34 | name: Upload to codecov.io
35 | verbose: true
36 |
--------------------------------------------------------------------------------
/example/encode.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | import 'package:cbor/cbor.dart';
9 |
10 | /// A simple encoding sequence followed by a self decode and a pretty print.
11 | int main() {
12 | final value = CborValue([
13 | [1, 2, 3], // Writes an array
14 | CborBytes([0x00]), // Writes a byte string
15 | 67.89,
16 | 10,
17 | // You can encode maps with any value encodable as CBOR as key.
18 | {1: 'one', 2: 'two'},
19 | 'hello',
20 |
21 | // Indefinite length string
22 | CborEncodeIndefiniteLengthString(['hello', ' ', 'world']),
23 | ]);
24 |
25 | final bytes = cbor.encode(value);
26 |
27 | final _ = cbor.decode(bytes);
28 |
29 | // Pretty print
30 | print(cborPrettyPrint(bytes));
31 |
32 | // Print json
33 | print(const CborJsonEncoder().convert(value));
34 |
35 | return 0;
36 | }
37 |
--------------------------------------------------------------------------------
/test/issues/issue75/issue75.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 03/05/2025
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | import 'dart:convert';
9 | import 'dart:io';
10 |
11 | import 'package:cbor/cbor.dart';
12 | import 'package:test/test.dart';
13 |
14 | void main() {
15 | test('Decode time', () {
16 | final currDir = Directory.current.path;
17 | final testFile = File('$currDir/test/issues/issue75/long-decode-file.txt');
18 | final contents = testFile.readAsStringSync();
19 | final toDecode = base64Decode(contents);
20 |
21 | final start = DateTime.now();
22 | final decoded = cbor.decode(toDecode);
23 | final end = DateTime.now();
24 |
25 | final timing = end.difference(start);
26 | print('Time taken : ${timing.inMilliseconds} ms');
27 | final jsonEncoder = CborJsonEncoder();
28 | final json = jsonEncoder.convert(decoded);
29 | print(json);
30 | });
31 | }
32 |
--------------------------------------------------------------------------------
/lib/src/codec.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | import 'dart:convert';
9 |
10 | import 'package:cbor/cbor.dart';
11 |
12 | /// A constant instance of a CBOR codec, using default parameters.
13 | const CborCodec cbor = CborCodec();
14 |
15 | /// A CBOR encoder and decoder.
16 | ///
17 | /// For exceptions [decode] may throw, see [CborDecoder].
18 | class CborCodec extends Codec> {
19 | @override
20 | final CborDecoder decoder = const CborDecoder();
21 | @override
22 | final CborEncoder encoder = const CborEncoder();
23 |
24 | /// Create a CBOR codec.
25 | const CborCodec();
26 | }
27 |
28 | /// Alias for `cbor.encode`.
29 | List cborEncode(CborValue value) {
30 | return cbor.encode(value);
31 | }
32 |
33 | /// Alias for `cbor.decode`.
34 | CborValue cborDecode(List value) {
35 | return cbor.decode(value);
36 | }
37 |
--------------------------------------------------------------------------------
/.github/workflows/pana.yml:
--------------------------------------------------------------------------------
1 | name: Pana Analysis
2 | on:
3 | push:
4 | branches:
5 | - master
6 | - development
7 |
8 | jobs:
9 |
10 | package-analysis:
11 |
12 | runs-on: ubuntu-latest
13 |
14 | steps:
15 | - uses: actions/checkout@v2
16 |
17 | - uses: axel-op/dart-package-analyzer@v3
18 | id: analysis # set an id for the current step
19 | with:
20 | githubToken: ${{ secrets.GITHUB_TOKEN }}
21 |
22 | # You can then use this id to retrieve the outputs in the next steps.
23 | # The following step shows how to exit the workflow with an error if a score is below 100:
24 | - name: Check scores
25 | env:
26 | # NB: "analysis" is the id set above. Replace it with the one you used if different.
27 | TOTAL: ${{ steps.analysis.outputs.total }}
28 | TOTAL_MAX: ${{ steps.analysis.outputs.total_max }}
29 | run: |
30 | PERCENTAGE=$(( $TOTAL * 100 / $TOTAL_MAX ))
31 | if (( $PERCENTAGE < 50 ))
32 | then
33 | echo Score too low!
34 | exit 1
35 | fi
--------------------------------------------------------------------------------
/lib/src/constants.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 27/03/2025
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | // dart format width=123
9 |
10 | /// Constants
11 | class CborConstants {
12 | static const prettyPrintIndent = 2;
13 | static const hexRadix = 16;
14 | static const binRadix = 2;
15 | static const bytesPerWord = 4;
16 | static const bitsPerWord = 32;
17 | static const bitsPerDoubleWord = 64;
18 | static const additionalInfoByteRange = 5;
19 | static const additionalInfoBitMask = 0x1f;
20 | static const byteLength = 8;
21 | static const bigIntSlice = 33;
22 | static const maxYear = 9999;
23 | static const seconds = 60;
24 | static const milliseconds = 1000;
25 | static const jsonBitLength = 53;
26 |
27 | // Bitfields, padding, indents and ranges
28 | static const one = 1;
29 | static const two = 2;
30 | static const three = 3;
31 | static const four = 4;
32 | static const five = 5;
33 | static const six = 6;
34 | static const seven = 7;
35 | static const eight = 8;
36 | static const nine = 9;
37 | static const sixteen = 16;
38 | }
39 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Steve Hamblett
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/example/stream_decode.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | import 'package:cbor/cbor.dart';
9 |
10 | /// A stream based decode.
11 | Future main() async {
12 | // Assume we have received a CBOR encoded byte buffer from the network.
13 | // The byte sequence below gives :-
14 | // {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E'}
15 | final payload = Stream.fromIterable(
16 | [
17 | 0xa5,
18 | 0x61,
19 | 0x61,
20 | 0x61,
21 | 0x41,
22 | 0x61,
23 | 0x62,
24 | 0x61,
25 | 0x42,
26 | 0x61,
27 | 0x63,
28 | 0x61,
29 | 0x43,
30 | 0x61,
31 | 0x64,
32 | 0x61,
33 | 0x44,
34 | 0x61,
35 | 0x65,
36 | 0x61,
37 | 0x45,
38 | ].map((x) => [x]),
39 | );
40 |
41 | // Decode using stream transforming
42 | // We use single because this is a single item
43 | final decoded = await payload.transform(cbor.decoder).single;
44 |
45 | // Print preview of decoded item
46 | // Should print {a: A, b: B, c: C, d: D, e: E}
47 | print(decoded);
48 |
49 | return 0;
50 | }
51 |
--------------------------------------------------------------------------------
/test/datetime_test.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | import 'package:cbor/cbor.dart';
9 | import 'package:test/test.dart';
10 |
11 | void main() {
12 | group('DateTime conversion tests', () {
13 | test('With 0 ms', () {
14 | expect(
15 | CborDateTimeString(DateTime.utc(2013, 3, 21, 20, 4, 0)).toString(),
16 | '2013-03-21T20:04:00Z',
17 | );
18 | });
19 |
20 | test('With 200 ms', () {
21 | expect(
22 | CborDateTimeString(DateTime.utc(2013, 3, 21, 20, 4, 0, 200)).toString(),
23 | '2013-03-21T20:04:00.2Z',
24 | );
25 | });
26 |
27 | test('With 200.5 ms', () {
28 | expect(
29 | CborDateTimeString(
30 | DateTime.utc(2013, 3, 21, 20, 4, 0, 200, 500),
31 | ).toString(),
32 | '2013-03-21T20:04:00.2005Z',
33 | );
34 | });
35 |
36 | test('With timezone', () {
37 | expect(
38 | CborDateTimeString(
39 | DateTime(2013, 3, 21, 20, 4, 0),
40 | timeZoneOffset: Duration(hours: 2, minutes: 30),
41 | ).toString(),
42 | '2013-03-21T20:04:00+02:30',
43 | );
44 | });
45 | });
46 | }
47 |
--------------------------------------------------------------------------------
/lib/src/decoder/stage2.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | import 'package:cbor/cbor.dart';
9 |
10 | import 'stage1.dart';
11 |
12 | class RawValueTagged {
13 | final Header header;
14 | final List data;
15 | final int offset;
16 | final List tags;
17 |
18 | RawValueTagged(
19 | this.header, {
20 | this.data = const [],
21 | required this.offset,
22 | this.tags = const [],
23 | });
24 | }
25 |
26 | class RawSinkTagged implements Sink {
27 | final Sink sink;
28 | List _tags = [];
29 |
30 | RawSinkTagged(this.sink);
31 |
32 | @override
33 | void add(RawValue data) {
34 | if (data.header.majorType == CborMajorType.tag) {
35 | if (data.header.arg.isIndefiniteLength) {
36 | throw CborMalformedException('Tag can not have additional info 31');
37 | }
38 |
39 | _tags.add(data.header.arg.toInt());
40 | } else {
41 | sink.add(
42 | RawValueTagged(
43 | data.header,
44 | offset: data.start,
45 | data: data.data,
46 | tags: _clearTags(),
47 | ),
48 | );
49 | }
50 | }
51 |
52 | @override
53 | void close() {
54 | sink.close();
55 | }
56 |
57 | List _clearTags() {
58 | final tags = _tags;
59 | _tags = [];
60 | return tags;
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/lib/src/encoder/encoder.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | import 'dart:convert';
9 |
10 | import 'package:cbor/cbor.dart';
11 | import 'package:typed_data/typed_buffers.dart';
12 |
13 | import 'sink.dart';
14 |
15 | /// A CBOR encoder.
16 | ///
17 | /// If cyclic references are found while encoding, a [CborCyclicError] will
18 | /// be thrown.
19 | ///
20 | /// Other than that, usage of the encoder is not expected to produce exceptions.
21 | ///
22 | /// This encoder supports [startChunkedConversion] and can therefore be
23 | /// used as a stream transformer.
24 | class CborEncoder extends Converter> {
25 | const CborEncoder();
26 |
27 | @override
28 | List convert(CborValue input) {
29 | final b = Uint8Buffer();
30 | input.encode(EncodeSink.withBuffer(b));
31 | return b;
32 | }
33 |
34 | @override
35 | Sink startChunkedConversion(Sink> sink) {
36 | return _ChunkedConversion(EncodeSink.withSink(sink));
37 | }
38 | }
39 |
40 | class _ChunkedConversion implements Sink {
41 | final EncodeSink sink;
42 |
43 | _ChunkedConversion(this.sink);
44 |
45 | @override
46 | void add(CborValue data) {
47 | data.encode(sink);
48 | }
49 |
50 | @override
51 | void close() {
52 | sink.close();
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/lib/src/decoder/stage0.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | import 'dart:math';
9 | import 'dart:typed_data';
10 |
11 | import 'package:typed_data/typed_buffers.dart' as typed;
12 |
13 | import '../constants.dart';
14 |
15 | class Reader {
16 | final typed.Uint8Buffer _bytes = typed.Uint8Buffer();
17 | int _read = 0;
18 | int _offset = 0;
19 |
20 | int get offset => _offset;
21 | int get length => _bytes.length - _read;
22 |
23 | void add(List input, [int start = 0, int? end]) =>
24 | _bytes.addAll(input, start, end);
25 |
26 | int? peekUint8() {
27 | if (_read == _bytes.length) {
28 | return null;
29 | }
30 |
31 | return _bytes[_read];
32 | }
33 |
34 | int? readUint8() {
35 | return readExactBytes(1)?.first;
36 | }
37 |
38 | Uint8List? readExactBytes(int c) {
39 | if (_read + c <= _bytes.length) {
40 | final bytes = _bytes.buffer.asUint8List().sublist(_read, c + _read);
41 | _read += c;
42 | _offset += c;
43 |
44 | if (length < _bytes.length ~/ CborConstants.bytesPerWord) {
45 | _bytes.removeRange(0, _read);
46 | _read = 0;
47 | }
48 |
49 | return bytes;
50 | }
51 | return null;
52 | }
53 |
54 | Uint8List readBytes(int maximum) {
55 | return readExactBytes(min(maximum, length))!;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/test/int_test.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : Alex Dochioiu
4 | * Date : 19/06/2024
5 | * Copyright : Alex Dochioiu
6 | */
7 |
8 | import 'package:cbor/cbor.dart';
9 | import 'package:test/test.dart';
10 |
11 | void main() {
12 | group("Int Numbers Test", () {
13 | final numbersToTest = [
14 | BigInt.parse("0"),
15 | BigInt.parse("1"),
16 | BigInt.parse("2656159029"),
17 | BigInt.parse("4294967295"),
18 | BigInt.parse("4294967296"),
19 | BigInt.parse("4294967299"),
20 | BigInt.parse("5294967590"),
21 | BigInt.parse("91234567890"),
22 | BigInt.parse("912345678901234567890"),
23 | ];
24 |
25 | group("positive numbers", () {
26 | for (final number in numbersToTest) {
27 | test(number.toString(), () {
28 | final cborInt = CborInt(number);
29 | final serialized = cborEncode(cborInt);
30 | final deserialized = cborDecode(serialized);
31 | expect(cborInt.toBigInt(), number);
32 | expect(deserialized, isA());
33 | expect((deserialized as CborInt).toBigInt(), number);
34 | });
35 | }
36 | });
37 |
38 | group("negative numbers", () {
39 | for (final positiveNumber in numbersToTest) {
40 | final number = BigInt.zero - positiveNumber;
41 | test(number.toString(), () {
42 | final cborInt = CborInt(number);
43 | final serialized = cborEncode(cborInt);
44 | final deserialized = cborDecode(serialized);
45 | expect(cborInt.toBigInt(), number);
46 | expect(deserialized, isA());
47 | expect((deserialized as CborInt).toBigInt(), number);
48 | });
49 | }
50 | });
51 | });
52 | }
53 |
--------------------------------------------------------------------------------
/lib/src/json.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | import 'dart:convert';
9 |
10 | import 'package:cbor/cbor.dart';
11 |
12 | import 'utils/utils.dart';
13 |
14 | /// Encodes CBOR into JSON.
15 | ///
16 | /// If the keys for a map are not strings, they are encoded recursively
17 | /// as JSON, and the string is used.
18 | ///
19 | /// This encoder supports [startChunkedConversion] and can therefore be
20 | /// used as a stream transformer.
21 | class CborJsonEncoder extends Converter {
22 | final Object? _substituteValue;
23 | final bool _allowMalformedUtf8;
24 |
25 | /// Create a new CBOR JSON encoder.
26 | ///
27 | /// [substituteValue] will be used for values that cannot be encoded, such
28 | /// as [double.infinity], [double.nan], [CborUndefined]. By default this
29 | /// is `null`.
30 | ///
31 | /// If [allowMalformedUtf8] is `false`, the decoder will
32 | /// throw [FormatException] when invalid UTF-8 is found. Otherwise,
33 | /// it will use replacement characters.
34 | const CborJsonEncoder({
35 | Object? substituteValue,
36 | bool allowMalformedUtf8 = false,
37 | }) : _allowMalformedUtf8 = allowMalformedUtf8,
38 | _substituteValue = substituteValue;
39 |
40 | @override
41 | String convert(CborValue input) {
42 | return json.encode(
43 | input.toJson(
44 | substituteValue: _substituteValue,
45 | allowMalformedUtf8: _allowMalformedUtf8,
46 | ),
47 | );
48 | }
49 |
50 | @override
51 | Sink startChunkedConversion(Sink sink) {
52 | return const JsonEncoder()
53 | .startChunkedConversion(sink)
54 | .map(
55 | (x) => x.toJson(
56 | substituteValue: _substituteValue,
57 | allowMalformedUtf8: _allowMalformedUtf8,
58 | ),
59 | );
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/lib/src/utils/arg.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | import '../constants.dart';
9 |
10 | /// The information encoded by additional Arg and the following bytes.
11 | abstract class Arg {
12 | static const Arg indefiniteLength = _ArgIndefiniteLength();
13 |
14 | bool get isIndefiniteLength;
15 |
16 | bool get isValidInt;
17 |
18 | const factory Arg.int(int arg) = _ArgInt;
19 |
20 | factory Arg.bigInt(BigInt arg) = _ArgBigInt;
21 |
22 | Arg operator ~();
23 |
24 | int toInt();
25 | BigInt toBigInt();
26 | }
27 |
28 | class _ArgIndefiniteLength implements Arg {
29 | @override
30 | final bool isIndefiniteLength = true;
31 |
32 | @override
33 | final bool isValidInt = false;
34 |
35 | const _ArgIndefiniteLength();
36 |
37 | @override
38 | _ArgIndefiniteLength operator ~() => this;
39 |
40 | @override
41 | int toInt() => 0;
42 | @override
43 | BigInt toBigInt() => BigInt.zero;
44 | }
45 |
46 | class _ArgInt implements Arg {
47 | final int value;
48 |
49 | @override
50 | final bool isIndefiniteLength = false;
51 |
52 | @override
53 | final bool isValidInt = true;
54 |
55 | const _ArgInt(this.value);
56 |
57 | @override
58 | _ArgInt operator ~() => _ArgInt(
59 | (~BigInt.from(value)).toSigned(CborConstants.bigIntSlice).toInt(),
60 | );
61 |
62 | @override
63 | int toInt() => value;
64 | @override
65 | BigInt toBigInt() => BigInt.from(value);
66 | }
67 |
68 | class _ArgBigInt implements Arg {
69 | final BigInt value;
70 |
71 | @override
72 | final bool isIndefiniteLength = false;
73 |
74 | @override
75 | final bool isValidInt = false;
76 |
77 | _ArgBigInt(this.value);
78 |
79 | @override
80 | _ArgBigInt operator ~() => _ArgBigInt(~value);
81 |
82 | @override
83 | int toInt() => value.toInt();
84 | @override
85 | BigInt toBigInt() => value;
86 | }
87 |
--------------------------------------------------------------------------------
/lib/cbor.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | /// Advanced API for CBOR.
9 | ///
10 | /// # Quick reference
11 | ///
12 | /// ## Decoders and encoders
13 | ///
14 | /// Between bytes and [CborValue]:
15 | ///
16 | /// * [CborCodec]
17 | /// * [CborDecoder]
18 | /// * [CborEncoder]
19 | /// * [cbor]
20 | /// * [cborDecode]
21 | /// * [cborEncode]
22 | ///
23 | /// Between bytes and objects:
24 | ///
25 | /// * [CborSimpleCodec]
26 | /// * [CborSimpleDecoder]
27 | /// * [CborSimpleEncoder]
28 | ///
29 | /// To JSON from [CborValue]:
30 | ///
31 | /// * [CborJsonEncoder]
32 | ///
33 | /// ## Pretty printing
34 | ///
35 | /// * [cborPrettyPrint]
36 | ///
37 | /// ## Values
38 | ///
39 | /// Super type:
40 | ///
41 | /// * [CborValue]
42 | ///
43 | /// Interfaces:
44 | ///
45 | /// * [CborDateTime]
46 | /// * Implementers:
47 | /// * [CborDateTimeString]
48 | /// * [CborDateTimeInt]
49 | /// * [CborDateTimeFloat]
50 | /// * [CborInt]
51 | /// * Implementers:
52 | /// * [CborSmallInt]
53 | /// * [CborBigInt]
54 | /// * [CborDateTimeInt]
55 | ///
56 | /// Major types:
57 | ///
58 | /// * [CborBytes]
59 | /// * [CborString]
60 | /// * [CborFloat]
61 | /// * [CborMap]
62 | /// * [CborList]
63 | /// * [CborInt]
64 | /// * [CborSmallInt]
65 | /// * [CborSimpleValue]
66 | /// * [CborNull]
67 | /// * [CborBool]
68 | /// * [CborUndefined]
69 | ///
70 | /// Tag based:
71 | ///
72 | /// * [CborDateTimeInt]
73 | /// * [CborBigInt]
74 | /// * [CborDateTimeString]
75 | /// * [CborBase64]
76 | /// * [CborBase64Url]
77 | /// * [CborMime]
78 | /// * [CborRegex]
79 | /// * [CborDateTimeFloat]
80 | /// * [CborDecimalFraction]
81 | /// * [CborRationalNumber]
82 | /// * [CborBigFloat]
83 | library;
84 |
85 | export 'src/codec.dart';
86 | export 'src/decoder/decoder.dart';
87 | export 'src/decoder/pretty_print.dart';
88 | export 'src/encoder/encoder.dart';
89 | export 'src/error.dart';
90 | export 'src/json.dart';
91 | export 'src/simple.dart';
92 | export 'src/value/value.dart';
93 |
--------------------------------------------------------------------------------
/lib/src/decoder/decoder.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | // The decoder works with 3 stages
9 | //
10 | // Stage 0: not really decoder stage, utils used for handling bytes.
11 | //
12 | // Stage 1: sort of a lexer, transforms the input into values with
13 | // header and data. a list will be a single item followed by its items.
14 | //
15 | // Stage 2: associate tags with values.
16 | //
17 | // Stage 3: produce final values, using builders that take values until
18 | // it is done. a list builder will be created when a list header is found
19 | // and then will keep taking values until the list is full.
20 |
21 | import 'dart:convert';
22 |
23 | import 'package:cbor/cbor.dart';
24 |
25 | import 'stage1.dart';
26 | import 'stage2.dart';
27 | import 'stage3.dart';
28 |
29 | /// A CBOR decoder.
30 | ///
31 | /// If the input is malformed, the decoder will throw [CborMalformedException].
32 | ///
33 | /// The decoder will not throw if the input is invalid but well-formed.
34 | /// However, operations on [CborValue] may throw if invalid data is used.
35 | ///
36 | /// This decoder supports [startChunkedConversion] and can therefore be
37 | /// used as a stream transformer. The decoder operates over a CBOR sequence
38 | /// in this mode.
39 | class CborDecoder extends Converter, CborValue> {
40 | /// Create a CBOR decoder.
41 | const CborDecoder();
42 |
43 | /// Converts [input] and returns the result of the conversion.
44 | ///
45 | /// If the input does not contain a single CBOR item, [FormatException] will
46 | /// be thrown.
47 | @override
48 | CborValue convert(List input) {
49 | CborValue? value;
50 |
51 | startChunkedConversion(
52 | ChunkedConversionSink.withCallback((values) {
53 | if (values.isEmpty) {
54 | throw FormatException('Expected at least one CBOR value.');
55 | }
56 | if (values.length > 1) {
57 | throw FormatException('Expected at most one CBOR value.');
58 | }
59 |
60 | value = values.single;
61 | }),
62 | )
63 | ..add(input)
64 | ..close();
65 |
66 | return value!;
67 | }
68 |
69 | @override
70 | ByteConversionSink startChunkedConversion(Sink sink) =>
71 | RawSink(RawSinkTagged(CborSink(sink)));
72 | }
73 |
--------------------------------------------------------------------------------
/test/issues/issue9_test.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 02/04/2020
5 | * Copyright : S.Hamblett
6 | */
7 | import 'package:convert/convert.dart';
8 | import 'package:cbor/cbor.dart';
9 | import 'package:test/test.dart';
10 |
11 | void main() {
12 | test('9-1', () {
13 | // 81 # array(1)
14 | // A3 # map(3)
15 | // 01 # unsigned(1)
16 | // A1 # map(1)
17 | // 01 # unsigned(1)
18 | // 01 # unsigned(1)
19 | // 03 # unsigned(3)
20 | // A1 # map(1)
21 | // 01 # unsigned(1)
22 | // 01 # unsigned(1)
23 | // 0C # unsigned(12)
24 | // A1 # map(1)
25 | // 01 # unsigned(1)
26 | // 01 # unsigned(1)
27 |
28 | const hexString = '81a301a1010103a101010ca10101';
29 | final bytes = hex.decode(hexString);
30 | final decoded = cbor.decode(bytes);
31 | expect(
32 | decoded,
33 | CborList([
34 | CborMap({
35 | CborSmallInt(1): CborMap({CborSmallInt(1): CborSmallInt(1)}),
36 | CborSmallInt(3): CborMap({CborSmallInt(1): CborSmallInt(1)}),
37 | CborSmallInt(12): CborMap({CborSmallInt(1): CborSmallInt(1)}),
38 | }),
39 | ]),
40 | );
41 | });
42 |
43 | test('9-2', () {
44 | // 81 # array(1)
45 | // A3 # map(3)
46 | // 01 # unsigned(1)
47 | // A1 # map(1)
48 | // 01 # unsigned(1)
49 | // 02 # unsigned(2)
50 | // 03 # unsigned(3)
51 | // A1 # map(1)
52 | // 01 # unsigned(1)
53 | // 81 # array(1)
54 | // 01 # unsigned(1)
55 | // 0C # unsigned(12)
56 | // A1 # map(1)
57 | // 01 # unsigned(1)
58 | // 02 # unsigned(2)
59 |
60 | const hexString = '81A301A1010203A10181010CA10102';
61 | final bytes = hex.decode(hexString);
62 | final decoded = cbor.decode(bytes);
63 | expect(
64 | decoded,
65 | CborList([
66 | CborMap({
67 | CborSmallInt(1): CborMap({CborSmallInt(1): CborSmallInt(2)}),
68 | CborSmallInt(3): CborMap({
69 | CborSmallInt(1): CborList([CborSmallInt(1)]),
70 | }),
71 | CborSmallInt(12): CborMap({CborSmallInt(1): CborSmallInt(2)}),
72 | }),
73 | ]),
74 | );
75 | });
76 | }
77 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ### 6.3.7
2 | - [Issue 75 - poc test only](https://github.com/shamblett/cbor/issues/75)
3 | - [PR 74](https://github.com/shamblett/cbor/pull/74)
4 |
5 | ### 6.3.6
6 | - [Issue 72](https://github.com/shamblett/cbor/issues/72)
7 |
8 | ### 6.3.5
9 | - [Issue 68](https://github.com/shamblett/cbor/issues/68)
10 |
11 | ### 6.3.4
12 | - [Issue 66](https://github.com/shamblett/cbor/issues/66)
13 |
14 | ### 6.3.3
15 | - [PR 65](https://github.com/shamblett/cbor/pull/65)
16 |
17 | ### 6.3.2
18 | - [PR 64](https://github.com/shamblett/cbor/pull/64)
19 |
20 | ### 6.3.1
21 | - [Issue 62](https://github.com/shamblett/cbor/issues/62)
22 |
23 | ### 6.3.0
24 | - [PR 60](https://github.com/shamblett/cbor/pull/60)
25 |
26 | ### 6.2.0
27 | - [PR 58](https://github.com/shamblett/cbor/pull/58)
28 |
29 | ### 6.1.1
30 | - [Issue 56](https://github.com/shamblett/cbor/issues/56)
31 | - [PR 57](https://github.com/shamblett/cbor/pull/57)
32 |
33 | ### 6.1.0
34 | - [Issue 51](https://github.com/shamblett/cbor/issues/51)
35 | - [PR 52](https://github.com/shamblett/cbor/pull/52)
36 |
37 | ### 6.0.0
38 | - [Issue 54](https://github.com/shamblett/cbor/issues/54) - Dart 3
39 |
40 | ### 5.1.2
41 | - [Issue 48](https://github.com/shamblett/cbor/issues/48)
42 |
43 | ### 5.1.1
44 | - [Issue 46](https://github.com/shamblett/cbor/issues/46)
45 |
46 | ### 5.1.0
47 | - [Issue 44](https://github.com/shamblett/cbor/issues/44)
48 |
49 | ### 5.0.1
50 | - [Issue 41](https://github.com/shamblett/cbor/issues/41)
51 | - [PR 42](https://github.com/shamblett/cbor/pull/42)
52 |
53 | ### 5.0.0
54 |
55 | ***Warning - major breaking API change in this version***
56 |
57 | - [Issue 28](https://github.com/shamblett/cbor/issues/28) - API rewrite
58 |
59 | ### 4.1.0
60 | - [Issue 2](https://github.com/shamblett/cbor/issues/2) - Byte string keys added
61 |
62 | ### 4.0.2
63 | - [Issue 5](https://github.com/shamblett/cbor/issues/5)
64 |
65 | ### 4.0.1
66 | - [Issue 21](https://github.com/shamblett/cbor/issues/21)
67 |
68 | ### 4.0.0
69 | - [Issues 18 and 14 (NNBD)](https://github.com/shamblett/cbor/issues/18)
70 | - [Issues 14 (NNBD)](https://github.com/shamblett/cbor/issues/14)
71 |
72 | ### 3.2.0
73 | - [Issue 13](https://github.com/shamblett/cbor/issues/13)
74 |
75 | ### 3.1.0
76 | - [Issue 5](https://github.com/shamblett/cbor/issues/5)
77 |
78 | ### 3.0.0
79 | - [Issue 10](https://github.com/shamblett/cbor/issues/10)
80 |
81 | ### 2.1.0
82 | - [Issue 9](https://github.com/shamblett/cbor/issues/9)
83 |
84 | ### 2.0.5
85 | - Linter rules update
86 |
87 | ### 2.0.4
88 | - Linter rules update
89 |
90 | ### 2.0.3
91 | - [Issue 6](https://github.com/shamblett/cbor/issues/6)
92 | - Linter rules update
93 |
94 | ### 2.0.2
95 | - [Issue 4](https://github.com/shamblett/cbor/issues/4), update to Dart 2
96 |
97 | ### 1.0.4
98 | - Updates for pub. Remove json_mapper
99 |
100 | ### 1.0.3
101 | - Strong mode updates for pub.
102 |
103 | ### 1.0.2
104 | - SDK version constraint update
105 |
106 | ### 1.0.1
107 | - Minor update to the README,
108 | [issue 1](https://github.com/shamblett/cbor/issues/1).
109 |
110 | ## 1.0.0
111 | - Initial version
112 |
--------------------------------------------------------------------------------
/test/rational_number_test.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : Alex Dochioiu
4 | * Date : 19/06/2024
5 | * Copyright : Alex Dochioiu
6 | */
7 |
8 | import 'package:cbor/cbor.dart';
9 | import 'package:test/test.dart';
10 |
11 | void main() {
12 | group(
13 | "RationalNumber",
14 | () => {
15 | group('encoding tests', () {
16 | test('encode auto length', () {
17 | expect(
18 | cbor.encode(
19 | CborRationalNumber(
20 | numerator: CborInt(BigInt.from(1)),
21 | denominator: CborSmallInt(2),
22 | type: CborLengthType.auto,
23 | ),
24 | ),
25 | [0xd8, 0x1e, 0x82, 0x1, 0x2],
26 | );
27 | });
28 |
29 | test('encode definite length', () {
30 | expect(
31 | cbor.encode(
32 | CborRationalNumber(
33 | numerator: CborInt(BigInt.from(1)),
34 | denominator: CborSmallInt(2),
35 | type: CborLengthType.definite,
36 | ),
37 | ),
38 | [0xd8, 0x1e, 0x82, 0x1, 0x2],
39 | );
40 | });
41 |
42 | test('encode indefinite length', () {
43 | expect(
44 | cbor.encode(
45 | CborRationalNumber(
46 | numerator: CborInt(BigInt.from(1)),
47 | denominator: CborSmallInt(2),
48 | type: CborLengthType.indefinite,
49 | ),
50 | ),
51 | [0xd8, 0x1e, 0x9f, 0x1, 0x2, 0xff],
52 | );
53 | });
54 | }),
55 | group('decoding tests', () {
56 | test('auto length', () {
57 | expect(
58 | cbor.decode([0xd8, 0x1e, 0x82, 0x1, 0x2]),
59 | CborRationalNumber(
60 | numerator: CborInt(BigInt.from(1)),
61 | denominator: CborSmallInt(2),
62 | type: CborLengthType.auto,
63 | ),
64 | );
65 | });
66 |
67 | test('definite length', () {
68 | expect(
69 | cbor.decode([0xd8, 0x1e, 0x82, 0x1, 0x2]),
70 | CborRationalNumber(
71 | numerator: CborInt(BigInt.from(1)),
72 | denominator: CborSmallInt(2),
73 | type: CborLengthType.definite,
74 | ),
75 | );
76 | });
77 |
78 | test('indefinite length', () {
79 | expect(
80 | cbor.decode([0xd8, 0x1e, 0x9f, 0x1, 0x2, 0xff]),
81 | CborRationalNumber(
82 | numerator: CborInt(BigInt.from(1)),
83 | denominator: CborSmallInt(2),
84 | type: CborLengthType.indefinite,
85 | ),
86 | );
87 | });
88 |
89 | test('not CborRationalNumber when denominator is 0', () {
90 | expect(
91 | cbor.decode([0xd8, 0x1e, 0x9f, 0x1, 0x0, 0xff])
92 | is CborRationalNumber,
93 | false,
94 | );
95 |
96 | expect(
97 | cbor.decode([0xd8, 0x1e, 0x9f, 0x1, 0x0, 0xff]) is CborList,
98 | true,
99 | );
100 | });
101 | }),
102 | },
103 | );
104 | }
105 |
--------------------------------------------------------------------------------
/lib/src/value/internal.dart:
--------------------------------------------------------------------------------
1 | /*
2 | * Package : Cbor
3 | * Author : S. Hamblett
4 | * Date : 04/01/2022
5 | * Copyright : S.Hamblett
6 | */
7 |
8 | // This is for internal usage and should not be returned to the user
9 |
10 | import 'package:cbor/cbor.dart';
11 | import 'package:meta/meta.dart';
12 |
13 | import '../encoder/sink.dart';
14 | import '../utils/arg.dart';
15 |
16 | class ToObjectOptions {
17 | final bool parseDateTime;
18 | final bool parseUri;
19 | final bool decodeBase64;
20 | final bool allowMalformedUtf8;
21 |
22 | ToObjectOptions({
23 | required this.parseDateTime,
24 | required this.parseUri,
25 | required this.decodeBase64,
26 | required this.allowMalformedUtf8,
27 | });
28 | }
29 |
30 | class ToJsonOptions {
31 | final JsonBytesEncoding encoding;
32 | final bool allowMalformedUtf8;
33 | final Object? substituteValue;
34 |
35 | ToJsonOptions({
36 | required this.encoding,
37 | this.substituteValue,
38 | required this.allowMalformedUtf8,
39 | });
40 |
41 | ToJsonOptions copyWith({JsonBytesEncoding? encoding}) => ToJsonOptions(
42 | encoding: encoding ?? this.encoding,
43 | substituteValue: substituteValue,
44 | allowMalformedUtf8: allowMalformedUtf8,
45 | );
46 | }
47 |
48 | enum JsonBytesEncoding { base64Url, base64, base16 }
49 |
50 | @internal
51 | class Break with CborValueMixin implements CborValue {
52 | @override
53 | final List tags = const [];
54 |
55 | const Break();
56 |
57 | @override
58 | Object? toObjectInternal(Set