Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with json_serializable #19

Open
realcr opened this issue Jan 16, 2020 · 2 comments
Open

Compatibility with json_serializable #19

realcr opened this issue Jan 16, 2020 · 2 comments
Labels

Comments

@realcr
Copy link

realcr commented Jan 16, 2020

Hi, thank you for your work on tuple!
Is there a recommended way to use tuple together with json_serializable?

For example, if I want to automatically serialize a class that looks like this:

@JsonSerializable(nullable: false)
class Person {
  final String firstName;
  final String lastName;
  final Tuple2<String,int> myTuple;

  Person({this.firstName, this.lastName, this.myTuple});

  factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
  Map<String, dynamic> toJson() => _$PersonToJson(this);
}
@rhalff
Copy link

rhalff commented Apr 23, 2020

You could use a converter to serialize tuples.

The below is a bit convoluted where I also extend Tuple, but similar should be possible by creating a TupleConverter to operate on a tuple directly.

import 'package:json_annotation/json_annotation.dart';
import 'package:tuple/tuple.dart';

class Entry extends Tuple2<String, double> {
  const Entry(item1, item2) : super(item1, item2);

  String get label => this.item1;
  double get amount => this.item2;
}

class EntryConverter implements JsonConverter<Entry, List<dynamic>> {
  static const instance = EntryConverter();

  const EntryConverter();

  @override
  Entry fromJson(dynamic json) {
    final label = json[0];
    final amount = json[1] == null ? 0.0 : double.parse(json[1]);

    return Entry(label, amount);
  }

  @override
  List<dynamic> toJson(Entry entry) {
    return entry == null ? null : [entry.label, entry.amount];
  }
}

@JsonSerializable()
@EntryConverter.instance
class Details {
  List<Entry> entries;
}

Access to .instance is just an optimization, I based this version on the TrivialNumberConverter in the tests.

@realcr
Copy link
Author

realcr commented May 2, 2020

@rhalff : Thanks for the detailed reply! I had a similar idea in mind back when I tried to come up with a solution, but it seemed to me this could end up with a lot of boilerplate code. I had hundreds of structures I needed to do this for, and I was sure that at some point I am going to make a mistake if I do everything manually.

I ended up using built_value and built_union for my serialization needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants