binary_codec 2.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

binary_codec #

Dart library for converting standard data types to a binary format and back

Supported datatypes: Null, bool, double, int, Uint8List, String, Map, List. Maps and Lists may contain any of the supported datatypes as keys and values.

On dart2js ints are always encoded as doubles. And if you try to decode ints on dart2js out of the safe range you will probably not get the exact value. But as long as you stay on one platform (not switching between dartvm and dart2js) your on the safe side and don't need to think about it.

1.0.X #

Not safe, don't use them.

2.0.1 #

Forgot the CHANGELOG.md :) Now I've rewritten all the code, so it makes more sense, and does more of the things we expect it to do. Changed the license to MIT since there are some hours invested in this code know.

2.0.2 #

run formatter

example/main.dart

import 'dart:math';
import 'dart:typed_data';

import '../lib/binary_codec.dart';

/// Example text
const loremIpsum = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, '
    'sed diam nonumy eirmod tempor invidunt ut labore et dolore '
    'magna aliquyam erat, sed diam voluptua. At vero eos et accusam '
    'et justo duo dolores et ea rebum. Stet clita kasd gubergren, no '
    'sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem '
    'ipsum dolor sit amet, consetetur sadipscing elitr, sed diam '
    'nonumy eirmod tempor invidunt ut labore et dolore magna '
    'aliquyam erat, sed diam voluptua. At vero eos et accusam '
    'et justo duo dolores et ea rebum. Stet clita kasd gubergren, '
    'no sea takimata sanctus est Lorem ipsum dolor sit amet.';

void main() {
  var exampleObject = generateSomeData();

  /// Encode the generated data
  Uint8List encoded = binaryCodec.encode(exampleObject);

  /// Decode the encoded data
  var decoded = binaryCodec.decode(encoded);

  /// Check if they are the same
  if (exampleObject.toString() == decoded.toString()) {
    /// They should be the same
    print('Success');
  } else {
    /// Else theres a bug in the library
    /// Please send the [exampleObject] that leads here and on which 
    /// platform you are to the author of this library (stefan.zemljic@gmx.ch)
    print('Wait... What???');
  }
}

dynamic generateSomeData() {
  var random = Random();
  var shortBinaryLists = List.generate(16, (length) {
    return Uint8List.fromList(
      List.generate(length, (_) => random.nextInt(256)),
    );
  });
  var someDoubles = [
    2930543.324960,
    -134943.2345,
    0.0,
    1.0,
    -1.0,
    double.infinity,
    double.nan,
    double.maxFinite,
    double.negativeInfinity,
    double.minPositive,
  ];
  var someInts = [321, 43956, -23459, 0, 1, -1, -239458];
  return {
    'small positive ints': List.generate(128, (x) => x),
    'small negative ints': List.generate(64, (x) => -x),
    'short binary lists': shortBinaryLists,
    'short texts': ['hello world', 'lorem ipsum', 'asdf qwer jklö'],
    'ints': someInts,
    'null': null,
    'booleans': [true, false],
    'doubles': someDoubles,
    'bigger binary lists':
        Uint8List.fromList(List.generate(1266, (x) => x % 256)),
    'bigger texts': loremIpsum.substring(0, 430),
    'lists': ['1', 2, 3.0, List.generate(4, (_) => 4), 'Five'],
    'maps': {'key1': 'value1', true: false, false: true, 4.0: 4, 5: 5.0},
  };
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  binary_codec: ^2.0.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:binary_codec/binary_codec.dart';
  
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 Nov 4, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

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

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0