fling_pickle 0.4.3 fling_pickle: ^0.4.3 copied to clipboard
Lightweight serialization and deserialization framework for Dart. Unintrusive and avoids complex initialization, annotations, and heavy reflection.
import 'dart:io';
import 'package:fling_pickle/fling_pickle.dart';
// For full integration, implement the Pickleable interface and support a
// static method to create an instance from a Pickle (constructors can work, but
// you'll miss out on a few syntactic sugar features since Dart does not allow
// function pointers to constructors).
class Color implements Pickleable {
final int r;
final int g;
final int b;
const Color(this.r, this.g, this.b);
@override
String toString() {
return '($r, $g, $b)';
}
//----------start Added for SerDes----------//
static Color fromPickle(final Pickle pickle) => Color(
pickle.readInt('r'),
pickle.readInt('g'),
pickle.readInt('b'),
);
@override
Pickle asPickle() {
return PickleBuilder()
.withInt('r', r)
.withInt('g', g)
.withInt('b', b)
.build();
}
//----------end Added for SerDes----------//
}
// If you prefer to keep your class clean of serialization code, you can
// implement the converters separately (as long as you have access to the
// information you will need to recreate the instance). You can also convert
// recursively.
class Banana {
final String name;
final double age;
final Color favoriteColor;
final List<Color> potentialColors;
const Banana(
this.name,
this.age,
this.favoriteColor,
this.potentialColors,
);
@override
String toString() {
return '$name is $age years old and likes the color $favoriteColor out of $potentialColors';
}
}
//----------start Added for SerDes----------//
Pickle pickleBanana(final Banana banana) => PickleBuilder()
.withString('name', banana.name)
.withDouble('age', banana.age)
.withPickleable('favoriteColor', banana.favoriteColor)
.withPickleables('potentialColors', banana.potentialColors)
.build();
Banana regurgitateBanana(final Pickle pickle) => Banana(
pickle.readString('name'),
pickle.readDouble('age'),
pickle.readPickleable('favoriteColor', Color.fromPickle),
pickle.readPickleables('potentialColors', Color.fromPickle));
//----------end Added for SerDes----------//
// Now we can serialize and deserialize an object, even one with nested
// Pickleables.
void main() async {
final fruit = Banana(
'Yellow',
1.5,
Color(255, 255, 0),
[Color(1, 2, 3), Color(4, 5, 6)],
);
print('I am a Banana: $fruit');
// To serialize the object, we need a Pickler and the object's Pickle.
final pickler = BinaryPickler();
final pickle = pickleBanana(fruit);
// We can serialize asynchronously or synchronously depending on our needs.
final serializedPickle = pickler.writeSync(pickle);
// We can do whatever we like with the resulting serialized pickle.
final file = File('banana');
file.writeAsBytesSync(serializedPickle);
// We can deserialize asynchronously or synchronously as well.
final deserializedPickle = pickler.readSync(file.readAsBytesSync());
// After depickling, we are back to the original object.
final regurgitatedFruit = regurgitateBanana(deserializedPickle);
print('I am still a Banana: $regurgitatedFruit');
file.deleteSync();
}