How to use the @MirrorUsed annotation on Serialization lib (dart2js)?

Issue

I using the serialization library (https://pub.dartlang.org/packages/serialization) to persiste and recover complex objects.

An excellent library to convert complex objects (also converts to JSON!). Congratulations to Google people!

However, necessary to transform dart in js (dart2js) and I’m not succeeding in using MirrorUsed. I would like to reduce the code size!

When I use @MirrorUsed like following, serialization does not work more.

main_app.dart

@MirrorsUsed(targets: 'serialization.serialization_mirrors.Serialization', override: '*')
import 'dart:mirrors';    
import 'package:serialization/serialization_mirrors.dart';
// [..other imports..]

@PolymerRegister('main-app')
class MainApp extends PolymerElement {
  var serializationProdutos;
  @property
  List produtos = new List();

  void ready() {

    Produto p = new Produto()..descricao = "SUPER TESTE"..unidade = "Unitário";
    Produto p2 = new Produto()..descricao = "SUPER TESTE 2"
    ..ehComposto = true


    Map m = new Map()
    ..putIfAbsent("expandir", () => true)..putIfAbsent(
      "produto", () => p);
    Map mm = new Map()
    ..putIfAbsent("expandir", () => true)..putIfAbsent(
      "produto", () => p2);

    (mm['produto'] as Produto).componentes.add(new ProdutoComponente()..produtoComponente = p);
    add('produtos', m);
    add('produtos', mm);
  }

  @reflectable
  salvarDados(Event e, [_]) {
    serializationProdutos = new Serialization();
    serializationProdutos.addRuleFor(Produto);
    serializationProdutos.addRuleFor(ProdutoComponente);
    var jsonProdutos = JSON.encode(serializationProdutos.write(produtos));  
    window.localStorage['precoMi_produtos'] = jsonProdutos;
  }

  @reflectable
  carregarDados(Event e, [_]) {
     var jsonProdutos = window.localStorage['precoMi_produtos'];
    if (jsonProdutos != null) {
      try {
           serializationProdutos = new Serialization();
           serializationProdutos.addRuleFor(Produto);
           serializationProdutos.addRuleFor(ProdutoComponente);
           List pro = serializationProdutos.read(JSON.decode(jsonProdutos));
           addAll('produtos', pro);
      } catch (e) {
        window.console.log(
            'Error local storage. ${e}');
      }
  }

Am I using @MirrorUsed correctly?

Solution

If you’re using Polymer, then it’s using the Reflectable package, which is much easier to optimize than MirrorsUsed, and I’m not even sure that the two will work nicely together. Really, the Serialization package needs to be updated to enable this.

As a short-term workaround, what I’d suggest is using non-mirrored serialization rules. You can write these by hand, which is more work for you in terms of maintenance, but if the number of classes is not too large, it’s probably acceptable, and it should produce quite small code without any extra effort on your part. Or, if your classes are relatively simple (no complex constructors, ordering dependencies) you can use the serialization transformer to generate them. Unfortunately there’s a bug where dartdocs is not showing the library comments, so you’d have to look at the comments on e.g. https://github.com/google/serialization.dart/blob/master/lib/transformer.dart and https://github.com/google/serialization.dart/blob/master/lib/serialization.dart

Answered By – Alan Knight

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *