shamblett / cbor

A CBOR implementation for Dart
MIT License
36 stars 15 forks source link

Uint8List in an array doesn't generate the CBOR byte string type #18

Closed jeremyherbert closed 3 years ago

jeremyherbert commented 3 years ago

Using this code:

import 'dart:typed_data';
import 'package:typed_data/typed_buffers.dart';
import 'package:cbor/cbor.dart' as cbor;

void main() {
  Map command = {};

  Uint8List data = Uint8List.fromList([196, 79, 51]);

  final inst = cbor.Cbor();
  final encoder = inst.encoder;

  command['data'] = [data];

  encoder.writeMap(command);

  print(inst.rawOutput.getData());
}

I get the following output:

[161, 100, 100, 97, 116, 97, 129, 131, 24, 196, 24, 79, 24, 51]

If I transfer this into http://cbor.me/ it shows the following decomposed data:

A1             # map(1)
   64          # text(4)
      64617461 # "data"
   81          # array(1)
      83       # array(3)
         18 C4 # unsigned(196)
         18 4F # unsigned(79)
         18 33 # unsigned(51)

and the decoded data:

{"data": [[196, 79, 51]]}

This means that the Uint8List inside an array is not being generated correctly. Indeed, it looks like in the code the fix that was applied to the map was not applied to the array encoder: https://github.com/shamblett/cbor/commit/a5a0efd16e9b16accc264020cb55ea907702e72b

shamblett commented 3 years ago

Yes your correct about the omission, thanks for the update, it will be in the next release.

shamblett commented 3 years ago

Package re released at version 4.0.0 with this update.