3

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?

Liminal
  • 1,709
  • 2
  • 12
  • 15
  • It all depends on what you need. The serialization process does not necessarily involve converting to JSON format. Converting to JSON format does not mean serialization. Thus, if you need serialization, then use programs for serialization. If you need to work with data in JSON format, then use the JSON software. – mezoni Jun 26 '19 at 17:33
  • One of the possible options, if you only need to encode / decode the data in JSON format, can be found here: https://pub.dev/packages/yaml2podo. – mezoni Jun 26 '19 at 17:37
  • @mezoni: I'm explicitly looking for a way of combining built_value objects and json_serialization objects in the same object hierarchy and being able to convert to and from JSON. How does yaml2podo solve that problem? – Liminal Jun 27 '19 at 09:42
  • If they are not compatible with each other and you know about it what, strictly speaking, would you like to hear in response? Confirmation that it is obvious that this is impossible? – mezoni Jun 27 '19 at 14:14
  • @mezoni I don't know for 100% sure that it is impossible so it's not obvious to me. And yes, if it is impossible I want to know that. In that case I will use either of those two solutions that have by themselves separately proven to work for me. Since then I will only have to rewrite some of the classes and not all of them like I would have to do if I switch to a third different library. – Liminal Jun 28 '19 at 09:46
  • Thanks for clarifying. It remains only to wait for an answer to such an not obvious situation (how to combine the incompatible). – mezoni Jun 28 '19 at 09:52

1 Answers1

0

built_value will produce a Map<String, dynamic> if you use StandardJsonPlugin.

You might be able to use this to plug into dart:convert's toEncodable:

https://api.dartlang.org/stable/2.7.0/dart-convert/jsonEncode.html

David Morgan
  • 466
  • 3
  • 6