Dart: how to use json_serializable library to create different subclasses from the json input

232 views Asked by At

UML

enter image description here

I have to serialize json in order to create specific subclass from the Enum which is an attribute of one class that contains the abstract class target

class Test {
  Outcome outcome;
  TestType type;
}

abstract class Outcome {
  String attribute;
}

class PackageOutcome extends Outcome {
  String packageAttribute;
}

class FlexibilityOutcome extends Outcome {
  String flexibilityAttribute;
}

Outcome doesn't have factory methods to serialize json, but PackageOutcome and FlexibilityOutcome have it.

in factory method Test.toJson() if I have a specific type of Enum TestType I crate PackageOutcome and if I've another type I create FlexibilityOutcome as Outcome of Test.

you can do this with json_serializable? There are some tutorials that explain how to do that?

0

There are 0 answers