Is there any reasonable way of mixing json_serializable and built_value models in a project?

Viewed 810

The problem is as follows, I have a flutter project with some classes using built_value and some classes using json_serializable. Both work fine separately but use very different ways of serializing/deserializing JSON.

built_value does its own thing with Serializers and json_serializer uses the dart:convert convention of fromJson / toJson methods

And I cannot find a simple way of combining these.

What I'm looking for is something like this:

Let's say I have a @JsonSerializable() class Person

@JsonSerializable()
class Person {
  final String name;
  final int age;

  Person(this.name, this.age);

  factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);

  Map<String, dynamic> toJson() => _$PersonToJson(this);

}

and a built_value class SomeAppState

abstract class SomeAppState implements Built<SomeAppState, SomeAppStateBuilder> {

  @nullable
  Person get currentPerson;

  SomeAppState._();
  factory SomeAppState([void Function(SomeAppStateBuilder) updates]) = _$SomeAppState;

  static Serializer<SomeAppState> get serializer => _$someAppStateSerializer;

}

There doesn't seem to be a reasonable way of serializing/deserializing an object of SomeAppState because built_value doesn't care about fromJson/toJson and there does not seem to be any way of doing it the other way around either because the built_value serializers don't produce Map<String, dynamic>

Am I forced to select either/or and just accept that you can't interop between the two or is there something clever that I am missing?

1 Answers
Related