I have this situation:
class Column extends JsonKey {
const Column({String name}) : super(name: name);
}
class User {
@Column(name: "user_name")
String name;
}
When user.g.dart is generated, the property name isn't changed to user_name. Asking in Gitter, I was informed that the builder doesn't support JsonKey inheritance, so I would like to check if it can be done.
It might be possible. Are you just trying to avoid duplication here?
My only concern: I've been able to make changes assuming there are no subclasses. Supporting this may make future changes more difficult.
Open to discuss.
Yes, just avoid duplications. I was able to inherit JsonSerializable annotation with no problem. So I thought that with JsonKey will be able either.
I'm looking for the same thing and for the same reason. I have lots of fields in different classes all annotated with @JsonKey(required: true, disallowNullValue: true). I wish there was a way to subclass JsonKey with custom classes in order to avoid code duplication.
Thanks.
Hrm. This might be doable. I'll take a look.
v3.3.0 is out now w/ this change!
Thanks!
Please let us know how it works out for you!
On Wed, Mar 25, 2020 at 3:44 PM lirantzairi notifications@github.com
wrote:
Thanks!
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/json_serializable/issues/575#issuecomment-604125969,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAAEFCV55L5N2TOJ4WX4QPTRJKCNHANCNFSM4J4NRX3Q
.
@kevmoo I think I found a small issue, or maybe I'm missing something... At first I have this simple class with regular JsonKey:
@JsonSerializable(createToJson: false)
class MyModel {
static String _fromJson(String val) => val;
@JsonKey(fromJson: _fromJson) final String name;
MyModel(this.name);
factory MyModel.fromJson(Map<String, dynamic> json) => _$MyModelFromJson(json);
}
The generated function looks ok:
MyModel _$MyModelFromJson(Map<String, dynamic> json) {
return MyModel(
MyModel._fromJson(json['name'] as String),
);
}
But if I create a custom subclass of JsonKey and use it, like this:
class MyJsonKey extends JsonKey {
const MyJsonKey({
Object defaultValue,
bool disallowNullValue,
Function fromJson,
bool ignore,
bool includeIfNull,
String name,
bool nullable,
bool required,
Function toJson,
Object unknownEnumValue,
}) : super(
defaultValue: defaultValue,
disallowNullValue: disallowNullValue,
fromJson: fromJson,
ignore: ignore,
includeIfNull: includeIfNull,
name: name,
nullable: nullable,
required: required,
toJson: toJson,
unknownEnumValue: unknownEnumValue,
);
}
@JsonSerializable(createToJson: false)
class MyModel {
static String _fromJson(String val) => val;
@MyJsonKey(fromJson: _fromJson) final String name;
MyModel(this.name);
factory MyModel.fromJson(Map<String, dynamic> json) => _$MyModelFromJson(json);
}
Then it ignores my _fromJson function. This is the new generated code:
MyModel _$MyModelFromJson(Map<String, dynamic> json) {
return MyModel(
json['name'] as String,
);
}
@lirantzairi I observed the same behavior. If you inherit from JsonKey, and specify values for toJson and fromJson functions, those are ignored during the code generation.
So inheritence works, but not for those two properties ... :(
@kevmoo Please re-open this. We still can't use fromJson and toJson
yes want it to work too
One probably a hacky solution is do something like this

and the generated code is

Which was enough to solve my problem
@lirantzairi
Most helpful comment
@lirantzairi I observed the same behavior. If you inherit from JsonKey, and specify values for toJson and fromJson functions, those are ignored during the code generation.
So inheritence works, but not for those two properties ... :(