json_serializable.dart
json_serializable.dart copied to clipboard
Inheritable JsonKey with Freezed
This issue is related to https://github.com/rrousselGit/freezed/issues/423
Is your feature request related to a problem? Please describe. I'm building an API wrapper, and I'm dealing with two JSON object that have numerous shared keys and only a few specific ones. I have an interface like this to implement this:
abstract class MyBaseType {
@JsonKey(name: 'property_key')
final String property;
}
And a couple of freezed classes like this:
@freezed
class MySpecializedType extends MyBaseType with _$MySpecializedType {
const MySpecializedType._();
const factory MySpecializedType({
required String property,
@JsonKey(name: 'another_propery_key') required String anotherProperty,
}) = _MySpecializedType
}
Normally, when using json_serializable, the JsonKey annotation in the base class will be used in subclasses. This is not happening when using freezed.
Describe the solution you'd like I'd like JsonKey annotations used in superclasses to be used in freezed subclasses as well.
Describe alternatives you've considered The only alternative is writing the same annotation everywhere it's needed.
This gets complicated, sadly. It's hard to pick a consistent ordering of fields to search first.
related... With latest v5.0.0 (or perhaps I'm just noticing), I'm getting an annotation warning on 'JsonKey' when annotating in a freezed class:
"The annotation 'JsonKey' can only be used on fields or gettersdart(invalid_annotation_target)"
@freezed
class UserProfileDto with _$UserProfileDto {
factory UserProfileDto({
@JsonKey(ignore: true) @Default('') String id,
required String name,
required String userBio,
required List<PhotoDto> profilePhotosList,
required int numPosts,
required int numMatches,
@TimestampConverterAuto() required Option<Timestamp> modifyDate,
}) = _UserProfileDto;
Does freezed
use JsonKey
?
json_serializabel
DOES NOT support annotating constructor params.
No, looks like it just passes the annotation thru to its generated code (below), which is working ok. But what you're saying makes sense, since json_serializable doesn't support annotating on constructors, the warning is valid then. I just hadn't seen that warning before.
Also looks like there's a thread on freezed issues on this topic.
class _$UserProfileDtoTearOff {
const _$UserProfileDtoTearOff();
_UserProfileDto call(
{@JsonKey(ignore: true) String id = '',
required String name,
required String userBio,
required List<PhotoDto> profilePhotosList,
required int numPosts,
...
The warning IS new as of the latest release of pkg:json_annotation
On Tue, Jul 20, 2021 at 2:54 PM mpiparo @.***> wrote:
No, looks like it just passes the annotation thru to its generated code (below), which is working ok. But what you're saying makes sense, since json_serializable doesn't support annotating on constructors, the warning is valid then. I just hadn't seen that warning before.
Also looks like there's a thread on freezed issues https://github.com/rrousselGit/freezed/issues/476 on this topic.
class _$UserProfileDtoTearOff { const _$UserProfileDtoTearOff();
_UserProfileDto call( {@JsonKey(ignore: true) String id = '', required String name, required String userBio, required List<PhotoDto> profilePhotosList, required int numPosts, ...
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/google/json_serializable.dart/issues/885#issuecomment-883732592, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAEFCRJDLA2BQ2XKDCWQITTYXWDFANCNFSM44FIQDOA .
same issue, Have you found a solution for the warning The annotation 'JsonKey' can only be used on fields or getters