-
Notifications
You must be signed in to change notification settings - Fork 405
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
Generic JsonConverter Not Working #1066
Comments
I had the same problem. Still looking for a solution. |
Try using @JsonSerializable()
class Question {
const Question({
required this.choices,
});
@JsonKey(fromJson: _choiceListFromJson)
final List<Choice> choices;
}
List<Choice> _choiceListFromJson(List<dynamic> items) =>
const IndexedConverter<Choice>(Choice.fromJson).fromJson(items); |
This is a workaround, but it doesn't fix the issue. This issue is about annotating members directly with |
I just wanted to open a similar issue. I don't feel like this is resolved. Can this issue get some attention again please? |
Please post the code you'd like to write here ⬇️ |
@kevmoo I have attached a reproduction repo in the issue's description, it might be helpful. |
Could you try upgrading to Dart 2.17? I just ran your reproduction on my machine – everything ran cleanly! |
I am facing the same issue in json_annotation 4.8.1, json_serializable 6.7.1 and dart 3.1.5. Here is a MRE: import 'package:json_annotation/json_annotation.dart';
part 'test.g.dart';
typedef JsonObject = Map<String, dynamic>;
// A data class to be converted
class UselessGeneric<T> {
final T value;
const UselessGeneric(this.value);
}
// The json converter which does not work currently
class UselessGenericJsonConverter<T>
extends JsonConverter<UselessGeneric<T>, T> {
const UselessGenericJsonConverter();
@override
UselessGeneric<T> fromJson(T json) => UselessGeneric(json);
@override
T toJson(UselessGeneric<T> object) => object.value;
}
// Workaround json converter
class StringUselessGenericJsonConverter
extends UselessGenericJsonConverter<String> {
const StringUselessGenericJsonConverter();
}
@JsonSerializable()
@UselessGenericJsonConverter<String>() // Not Work : Exception when `dart run build_runner build`
// @StringUselessGenericJsonConverter() // This work
class Example {
final UselessGeneric<String> text;
Example(this.text);
factory Example.fromJson(JsonObject json) => _$ExampleFromJson(json);
Map<String, dynamic> toJson() => _$ExampleToJson(this);
} I think |
Having the same problem. I wrote in my
But in
What's interesting is that
UPD: versions |
If we have a simple generic
JsonConverter
like this:Then it was used on a class field like this:
The actual output is:
While the expected output is:
The text was updated successfully, but these errors were encountered: