dogs_darwin 1.0.1-alpha.15 copy "dogs_darwin: ^1.0.1-alpha.15" to clipboard
dogs_darwin: ^1.0.1-alpha.15 copied to clipboard

Darwin marshal linker for the dogs serialization library.

example/dogs_darwin_example.dart

import 'package:darwin_marshal/darwin_marshal.dart';
import 'package:dogs_core/dogs_core.dart';
import 'package:dogs_darwin/dogs_darwin.dart';
import 'package:lyell/lyell.dart';

void main() {
  var dogs = DogEngine();
  dogs.registerConverter(CatConverter());
  var marshal = DarwinMarshal();
  DarwinMarshalSimple.register(marshal);
  DogsMarshal.link(marshal, dogs);
  print(marshal.associatedMappers);

  var serializationContext = SerializationContext(
      MarshalTarget(TypeToken<List<Cat>>()), "application/json", {}, marshal);
  var serializer = marshal.findSerializer(serializationContext)!;
  var serialized = serializer
      .serialize([Cat("CAT 1", 5), Cat("CAT 2", 5)], serializationContext);
  print(serialized);

  var deserializationContext = DeserializationContext(
      "application/json", MarshalTarget(TypeToken<Set<Cat>>()), {}, marshal);
  var deserializer = marshal.findDeserializer(deserializationContext)!;
  var deserialized =
      deserializer.deserialize(serialized, deserializationContext);
  print(deserialized);
}

class Cat {
  String name;
  int age;

  Cat(this.name, this.age);

  @override
  String toString() {
    return 'Cat{name: $name, age: $age}';
  }
}

class CatConverter extends DogConverter<Cat> with StructureEmitter<Cat> {
  @override
  Cat convertFromGraph(DogGraphValue value, DogEngine engine) {
    var map = value.asMap!.value;
    return Cat(map[DogString("name")]!.coerceNative(),
        map[DogString("age")]!.coerceNative());
  }

  @override
  DogGraphValue convertToGraph(Cat value, DogEngine engine) {
    return DogMap({
      DogString("name"): DogGraphValue.fromNative(value.name),
      DogString("age"): DogGraphValue.fromNative(value.age)
    });
  }

  @override
  DogStructure get structure => DogStructure<Cat>.synthetic("Cat");
}
0
likes
110
points
22
downloads

Publisher

verified publisherexperiments.helight.dev

Weekly Downloads

Darwin marshal linker for the dogs serialization library.

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

conduit_open_api, darwin_http, darwin_injector, darwin_marshal, darwin_sdk, dogs_core, lyell

More

Packages that depend on dogs_darwin