JsonSerializable with subclasses in dart

Viewed 87

I have an object hierarchy that I want to serialize to json using dart classes. Here's a sudo-code example:

@JsonSerializable(nullable: true)
class Person {
  int age;
}

@JsonSerializable(nullable: true)
class Athlete extends Person {
  int speed;
}

@JsonSerializable(nullable: true)
class BaseballPlayer extends Athlete {
  int height;
}

@JsonSerializable(nullable: true)
class Building {
  List<Person> people = []; 

  Building(){
    people.add(Person());
    people.add(Athlete());
    people.add(BaseballPlayer());
  }
}

I can stream this out, but when I read it back, people has three "Person"s in it.. NOT a Person, Athlete and BaseballPlayer. it doesn't know how to load specific subclass types. How do I handle this?

0 Answers
Related