data_class
data_class copied to clipboard
feat: Add support for toJson and fromJson
This is one of the most important features for Data classes in Dart
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
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 😄
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.
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.
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)
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
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
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
Serializableannotation 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)
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
Serializableannotation 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.
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
Serializableannotation for instance: #13Curious 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:jsonso it would just be a matter of addingpkg:jsonto yourpubspecand 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?
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.
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 👍