Jeff
Jeff

Reputation: 5966

How to properly deserialize a top level generic with built_value

I have a built_value object that's declared like so:

abstract class ParentItem<T> implements Built<ParentItem<T>, ParentItemBuilder<T>> {
  static Serializer<ParentItem> get serializer => _$parentItemSerializer;

  int get skip;
  int get limit;
  int get total;
  BuiltList<T> get items;

  ParentItem._();
  factory ParentItem([updates(ParentItemBuilder<T> b)]) = _$ParentItem<T>;
}

What is the proper way to deserialize this in dart / built_value? None of the following work:

// Fails with no builder for BuiltList<dynamic><Object>.
serializers.deserializeWith(ParentItem.serializer, json);
// Fails with _$ParentItemSerializer is not a subtype of Serializer<ParentItem<ConcreteType>>
serializers.deserializeWith<ParentItem<ConcreteType>>(ParentItem.serialize, json);
// Fails with no builder for ParentItem<dynamic><ConcreteType>
serializers.deserialize(json, new FullType(ParentItem, [new FullType(ConcreteType)]);

Upvotes: 3

Views: 1056

Answers (2)

David Morgan
David Morgan

Reputation: 486

This is correct. Note that there are some cases where built_value could add the builder factories for you, but doesn't yet. There's an open issue for those cases

https://github.com/google/built_value.dart/issues/124

but for top level generics there is no way for it to know--you'll have to add them yourself.

Upvotes: 1

Jeff
Jeff

Reputation: 5966

It appears that the built_value generator creates specific versions of internally used generics for all possible concrete types, like so:

..addBuilderFactory(
  const FullType(ParentType, const [const FullType(ConcreteType)]),
  () => new ParentTypeBuilder<ConcreteType>())

But it does not do this (because how could it) for all combinations of a top level opject... so you have to add them yourself.

Upvotes: 2

Related Questions