-
-
Notifications
You must be signed in to change notification settings - Fork 33
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
Deserialization fails when using generic types #101
Comments
Generics are tough in terms of deserialization 🤷 Anyway, I've got some examples for you
Hope this helps somehow 😄 |
Hello @k-paxian I have the same problem, but the error is different void main() {
initializeJsonMapper();
String json = '{"Success":true,"Result":{"Id":1,"Name":"aa"}}';
ApiResult<UserModel>? result = JsonMapper.deserialize<ApiResult<UserModel>>(json);
...
} my @jsonSerializable
class ApiResult<T> {
bool? Success;
T? Result;
ApiResult({
this.Success,
this.Result,
});
ApiResult<T>? fromJson(dynamic json) =>
JsonMapper.deserialize<ApiResult<T>>(json);
dynamic toJson() =>
JsonMapper.serialize(this, SerializationOptions(indent: ''));
}
@jsonSerializable
class UserModel {
int? Id;
String? Name;
UserModel({
this.Id,
this.Name,
});
}
@jsonSerializable
@Json(valueDecorators: ApiResultUserModel.valueDecorators)
class ApiResultUserModel extends ApiResult<UserModel> {
static Map<Type, ValueDecoratorFunction> valueDecorators() =>
{typeOf<ApiResult<UserModel>>(): (value) => ApiResultUserModel.of(value)};
ApiResultUserModel({
bool? Success,
UserModel? Result,
}) : super(
Success: Success,
Result: Result,
);
factory ApiResultUserModel.of(ApiResult other) => ApiResultUserModel(
Success: other.Success,
Result: other.Result,
);
} Serialization is done without errors The package you produced is very simple and practical compared to other packages 👍 |
@masoodmrx Thank you for reporting that, I've got your use case working at this commit. Anyway, the I've got a future plan to handle generics in a more clean way, just like this copy cat package does, due to lack of time this might be not soon enough 🤷 |
Hello @k-paxian The error I sent above has not been resolved yet and only gives this error in the web browser |
#101 (fix) Deserialization fails on flutter web target when using generic types
With version @jsonSerializable
class ApiResult<T> {
bool? Success;
T? Result;
ApiResult({
this.Success,
this.Result,
});
ApiResult<T>? fromJson(dynamic json) =>
JsonMapper.deserialize<ApiResult<T>>(json);
dynamic toJson() =>
JsonMapper.serialize(this, SerializationOptions(indent: ''));
}
@jsonSerializable
class UserModel {
int? Id;
String? Name;
UserModel({
this.Id,
this.Name,
});
}
@jsonSerializable
@Json(valueDecorators: ApiResultUserModel.valueDecorators)
class ApiResultUserModel extends ApiResult<UserModel> {
static Map<Type, ValueDecoratorFunction> valueDecorators() =>
{typeOf<ApiResult<UserModel>>(): (value) => ApiResultUserModel.of(value)};
ApiResultUserModel({
bool? Success,
UserModel? Result,
}) : super(
Success: Success,
Result: Result,
);
factory ApiResultUserModel.of(ApiResult other) => ApiResultUserModel(
Success: other.Success,
Result: JsonMapper.deserialize<UserModel>(other.Result), /// <= Attention: here is the change
);
} |
Error fixed |
Hello @k-paxian! I'm here once again 😃
So, given the following models:
I get the following error when trying to deserialize it:
my
main()
:var bar = Bar();
it works fine.@Json(typeNameProperty: 'typeName')
onFoo
andBarBase
, but it doesn't seem to help.I understand that what I'm trying to do is kinda weird (why would someone want to do it like that?), and if that is not possible to do (not a bug) that's fine, I'll find an alternative.
The text was updated successfully, but these errors were encountered: