Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Featurerequest: Typenamehandling #6

Closed
HerrNiklasRaab opened this issue Mar 1, 2019 · 2 comments
Closed

Featurerequest: Typenamehandling #6

HerrNiklasRaab opened this issue Mar 1, 2019 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@HerrNiklasRaab
Copy link
Contributor

HerrNiklasRaab commented Mar 1, 2019

Sorry for coming up here again :). But...

I am also in a situation where I need to handle something like this:

@jsonSerializable
class Business {
  String name;
}

@jsonSerializable
class Hotel extends Business {
  int stars;
  Hotel(this.stars);
}

@jsonSerializable
class Startup extends Business {
  int userCount;

  Startup(this.userCount);
}

@jsonSerializable
class Stakeholder {
  String fullName;
  List<Business> businesses;

  Stakeholder(this.fullName, this.businesses);
}

main() {
  var jack = Stakeholder("Jack", <Business>[Startup(10), Hotel(4)]);
  var json = JsonMapper.serialize(jack);
  var jack2 = JsonMapper.deserialize<Stakeholder>(json);
  jack2.businesses[0] is Startup; //Should be true
  jack2.businesses[1] is Hotel; //Should be true
}

The serializer should be able to, get the current type, save it in a JSON property, for example, "$type". And can create those types based on the "$type" property.

Newtonsoft.NET is handling this very pretty: https://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm

Because you are using a kind of "reflection", and not this complicated code generation JSON stuff, this should be possible right?

If you need any help, I am willing to do, but I've never contributed to an open source project, so you are dealing with a noob :).

Thanks and a happy day,

Niklas

@k-paxian k-paxian self-assigned this Mar 1, 2019
k-paxian added a commit that referenced this issue Mar 1, 2019
@k-paxian k-paxian added the enhancement New feature or request label Mar 1, 2019
@k-paxian
Copy link
Owner

k-paxian commented Mar 1, 2019

Hey Niklas,

Suggested solution has been implemented. Please consult the unit tests on how to resolve your use case.

btw, thank you for feedback and really useful edge cases.

@k-paxian k-paxian closed this as completed Mar 1, 2019
@HerrNiklasRaab
Copy link
Contributor Author

HerrNiklasRaab commented Mar 2, 2019

Hello @k-paxian,

man, you're faster than light! 🥇

But I have one question left, would something like this also be possible?

@jsonSerializable
class CEO<TBusiness extends Business> {
  TBusiness business;
  String fullName;

  CEO(this.fullName, this.business);
}

test(
    "Should dump typeName to json property when"
    " @JsonSerializable(includeTypeName: true)", () {
  // given
  final john = CEO<Startup>("John", Startup(10));

  // when
  final String json = JsonMapper.serialize(john);
  final CEO<Business> target = JsonMapper.deserialize(json);

  // then
  expect(target.business, TypeMatcher<Startup>());
});

Thanks,

Niklas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants