Cretezy / redux_persist

Persist Redux State
https://pub.dartlang.org/packages/redux_persist
MIT License
130 stars 41 forks source link

Integration with json_serializable #10

Closed giorgiogross closed 6 years ago

giorgiogross commented 6 years ago

Hi,

how would you integrate redux_persist with json_serializable? json_serializable requires a factory method in the AppState class as such:

factory AppState.fromJson(Map<String, dynamic> json) {
  // generated deserialization code.
}

As it's not possible to pass a reference to the AppState.fromJson(....) constructor to the Persistor.decoder field I would do something like this:

factory AppState.fromJson(Map<String, dynamic> json) {
  // generated deserialization code.
}

static AppState fromJsonWrapper(dynamic json){
  return AppState.fromJson(json);
}

Is it save to assume that the dynamic json will always be of type Map<String, dynamic>? Having a look at you code it looks like that's the case, but I'm not sure though. Do you know a way to use the factory directly?

Cretezy commented 6 years ago

dynamic json can actually be anything, because your toMap/Json function could return any dynamic.

I think the simplest thing is to do the JSON serialization code elsewhere and then call it from the fromJson that redux_persist accepts.

giorgiogross commented 6 years ago

Okay thanks! So basically if I enforce my toJson to return a map (just as in your example) then fromJson will get that map as parameter during deserialization?

I just noticed there is a typo in my question: I meant to call return AppState.fromJson(json) instead of return AppState.fromJsonWrapper(json)