google / reflectable.dart

Reflectable is a Dart library that allows programmers to eliminate certain usages of dynamic reflection by specialization of reflective code to an equivalent implementation using only static techniques. The use of dynamic reflection is constrained in order to ensure that the specialized code can be generated and will have a reasonable size.
https://pub.dev/packages/reflectable
BSD 3-Clause "New" or "Revised" License
381 stars 57 forks source link

Serialize nested generics #260

Open aymenbs2 opened 2 years ago

aymenbs2 commented 2 years ago

Hi, I' need to know how can i Serialize nets generic object for like this example : const reflectable = MyReflectable() @reflectable class Test{ //some attrs

} //methode to save this class as json file save(reflectable){ //ssome code to save as json } //methode to read list of saved reflectable read(key){ //some code } now i went to use the read mthode like this : read< List >(somKey);

eernstg commented 2 years ago

I don't understand the question. Let me restate the code in order to clarify what you mean:

// Before a snippet of Dart code, please insert a line containing "```dart"
// to get source code formatting and highlighting.

// Assumed: class MyReflectable extends Reflectable ...

const reflectable = MyReflectable();

@reflectable
class Test {
  // Some attrs

  // Method to save instances of this class as a json file.
  void save(Reflectable reflectable) {
    // Some code to save as json.
  }

  // Method to read a list of saved reflectable.
  Reflectable read(key) {
    // Some code.
  }
}

void main() {
  // ...
  var refl = read<List>(someKey);
}

// After the snippet of Dart code, insert a line containing "```".

There are several confusing elements here.

First, Reflectable is a class which is used as the immediate superclass of a class like MyReflectable in order to create a mirror system, and a constant instance of MyReflectable like reflectable is normally used as the access device to that mirror system.

In other words, whenever you want to do something with mirrors, reflectable is the object that allows you to get started. For instance reflectable.reflectType(Test) would give you a ClassMirror that allows you to get started using reflection on the class Test, and reflectable.reflect(myTest) where myTest is an instance of Test would yield an InstanceMirror on the object myTest which will allow you to work on that object in a reflective manner.

This means that it is extremely unlikely that you'd want to pass an instance of MyReflectable as the argument to save: You would pass an instance of the actual data that you want to serialize. So why is the parameter name of save chosen to be reflectable? Similarly for read: I'd expect the return type of read to be the same as the parameter type of save (because we'd save and then read "the same thing"), but it doesn't make sense for read to return an instance of MyReflectable (or any other subclass of Reflectable).

You can take a look at https://github.com/google/reflectable.dart/blob/master/test_reflectable/test/basic_test.dart in order to see how to declare a suitable subclass of Reflectable and use it to obtain reflection support for specific classes and instances.

You can take a look at https://github.com/google/reflectable.dart/blob/master/test_reflectable/test/serialize_test.dart in order to see how you can create a very basic kind of support for serialization.

However, if you're actually just looking for serialization support then it's probably much better to go directly to a package which is created for the very purpose of supporting serialization (several of them are using reflectable, but you probably wouldn't even have to care about that). For instance, you could check out https://pub.dev/packages/dart_json_mapper or https://pub.dev/packages/json_serializable.