data_class icon indicating copy to clipboard operation
data_class copied to clipboard

feat: Add support for toJson and fromJson

Open pedromassango opened this issue 1 year ago • 9 comments

This is one of the most important features for Data classes in Dart

pedromassango avatar May 16 '24 07:05 pedromassango

I think this is not so important since we already have a JsonCodable Macro (package:json/json.dart), that is, how can we add multiple macros to the class https://github.com/dart-lang/language/blob/main/working/macros/feature-specification.md#application-order

warioddly avatar May 16 '24 07:05 warioddly

I am in favor of having those related features in one single package

You don't go to a restaurant to get the bread and go to another one to get the fillings 😄

pedromassango avatar May 16 '24 18:05 pedromassango

I wouldn't put it in a single package. These are two very different things. For example, you might want to implement fromJson and toJson without needing equality comparisons, such as in a DTO.

Additionally, you might want a macro for parsing into other types (like XML). If you add JSON support here, an argument can be made to add XML support as well.

MrEdgarsz avatar May 16 '24 18:05 MrEdgarsz

For example, you might want to implement fromJson and toJson without needing equality comparisons, such as in a DTO.

I don't believe you want that as you will likely run into troubles.

Additionally, you might want a macro for parsing into other types (like XML). If you add JSON support here, an argument can be made to add XML support as well.

Keep in that what is being proposed is not a parser to a data.json file but a convenient way of mapping an object into a Map<String, dynamic>. So I believe we're talking about different things here.

pedromassango avatar May 16 '24 18:05 pedromassango

Json seems like a different responsibly and imo should be kept separate. E.g. some people might want XML or protobuf parsing. A custom wrapper around Json and Struct feels more appropriate.

class DataClass with Json, Struct { }

(Unsure if this works with macros, but just a thought)

josiahsrc avatar May 17 '24 13:05 josiahsrc

I tend to agree that serialization is tangential and doesn’t make as much sense to be included especially given annotations can be combined but happy to be convinced otherwise if folks feel strongly

felangel avatar May 17 '24 14:05 felangel

I tend to agree that serialization is tangential and doesn’t make as much sense to be included especially given annotations can be combined

As long as they're part of the same package then I would be happy with it :)

I like the proposal of multiple annotations, so this issue could track the addition of an Serializable annotation for instance: https://github.com/felangel/data_class/issues/13

pedromassango avatar May 17 '24 17:05 pedromassango

I tend to agree that serialization is tangential and doesn’t make as much sense to be included especially given annotations can be combined

As long as they're part of the same package then I would be happy with it :)

I like the proposal of multiple annotations, so this issue could track the addition of an Serializable annotation for instance: #13

I would like toJson() and fromJson() to be separate macros too, since sometimes we won't use one or the other method. It would be cool if it were like this)

warioddly avatar May 17 '24 17:05 warioddly

As long as they're part of the same package then I would be happy with it :)

I tend to agree that serialization is tangential and doesn’t make as much sense to be included especially given annotations can be combined

As long as they're part of the same package then I would be happy with it :)

I like the proposal of multiple annotations, so this issue could track the addition of an Serializable annotation for instance: #13

Curious why you feel they need to be part of the same package? It seems like the dart team is already working on json serialization via pkg:json so it would just be a matter of adding pkg:json to your pubspec and then you could add @Json() to any class.

felangel avatar May 20 '24 16:05 felangel

As long as they're part of the same package then I would be happy with it :)

I tend to agree that serialization is tangential and doesn’t make as much sense to be included especially given annotations can be combined

As long as they're part of the same package then I would be happy with it :) I like the proposal of multiple annotations, so this issue could track the addition of an Serializable annotation for instance: #13

Curious why you feel they need to be part of the same package? It seems like the dart team is already working on json serialization via pkg:json so it would just be a matter of adding pkg:json to your pubspec and then you could add @Json() to any class.

Honestly, because:

  • it is a very "simple" (usage PoV) that, as a user, it doesn't make sense to add another package just for that;
  • (I personally believe) most of Flutter project rely on both: Data class + Serializable data classes

So why not provide both?

pedromassango avatar May 22 '24 18:05 pedromassango

Data classes and JSON are pretty much orthogonal. To put them to same package for the sole reason of not having to do dart pub add json seems like a wrong decision.

knopp avatar May 25 '24 09:05 knopp

Closing this for now since json serialization is out of scope for this this package and multiple macros can be applied to a single class. Happy to continue the discussion if anyone strongly disagrees 👍

felangel avatar May 25 '24 14:05 felangel