-
-
Notifications
You must be signed in to change notification settings - Fork 236
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
Nested Freezed class not converted TO JSON #86
Comments
I have a very deeply nested object and it works fine for me. The generated code you gave is generated by |
The @JsonSerializable()
class _$_Nested implements _Nested {...} |
Then it may be an issue with build_runner. Try regenerating the sources (potentially deleting In any case, freezed does nothing besides adding this annotation. So if it's there, then I can't do anything 🙃 |
Whoops! Sorry to break it to you, Rémi, but the bug is probably on your side 😅 I replicated the non-serializing behavior with regular classes too. All it took for the nested class to serialize was |
I mean it depends on what encoder you use. The documentation of If you need it, it's your job to add it or your encoder's job |
OK, pardon me 🤦♂️ I'll leave the targets:
$default:
builders:
json_serializable:
options:
explicit_to_json: true Thank you! |
thanks, resoDev and Remi, I am from the future and this conversation helped me |
Works great! But I was wondering if we can set I tried annotating each field but that didn't seem to work Here's my use case:
I want customClass to be explicit to json, but the category to not be explicit to Json. Here's my CategoryEnum class:
|
I don't think you can, no |
Thanks for the quick response. I was able to fix this by setting
|
Hey guys, I think QuickType is worth giving consideration. It thus use Freezed and work with many languages, It converts JSON like magic. |
@mathiasgodwin i love the idea of quicktype a LOT but it doesn't add the explicityTOJson and it doesn't handle underscores |
a-ha, the build also handles snake field renaming. good call then @mathiasgodwin |
Hi, where is |
@pishguy You have to create it on root folder (where |
Without build.yaml we can specify like this: @freezed
class TopLevel with _$TopLevel {
TopLevel._();
// Here 👇 the magic
JsonSerializable(explicitToJson: true)
const factory TopLevel(
String niceString,
Nested doesNotSerializeButDoesDeserialize,
) = _TopLevel;
factory TopLevel.fromJson(Map<String, dynamic> json) =>
_$TopLevelFromJson(json);
}
@freezed
class Nested with _$Nested {
Nested._();
const factory Nested(int niceInteger) = _Nested;
factory Nested.fromJson(Map<String, dynamic> json) => _$NestedFromJson(json);
} |
It's kinda weird it freezed doesn't handle that by default, could anyone provide an example of code that works with nested json with explicit_to_json hack? |
Well I agree. But the problem isn't Freezed, it's json_serializable. Anyway, yo enable explicit_to_json, create a targets:
$default:
builders:
json_serializable:
options:
explicit_to_json: true |
It works, just doesn't feel right 😆 |
I've requested for json_serializable to enable explicit_to_json by default before. They don't want to I've alternatively raised google/json_serializable.dart#1126 as a middle ground (preserving explicit_to_json:false by default, but also fixing the issue of invalid JSON output for nested classes) |
after trying most of the solutions here and non of them worked for me, I found my own solution. @freezed
class ChargingProfile with _$ChargingProfile implements Insertable<ChargingProfile> {
const ChargingProfile._();
@Implements<Insertable<ChargingProfile>>()
// Here 👇🏼 is the important line
@JsonSerializable(explicitToJson: true)
const factory ChargingProfile({
final int? profileId,
final String? name,
final int? stackLevel,
final int? operatorId,
final DateTime? validFromDate,
final DateTime? validToDate,
@Default(ProfilePurposeEnum.TxDefaultProfile) final ProfilePurposeEnum profilePurpose,
final ProfileKindEnum? profileKind,
final RecurrencyKindEnum? recurrencyKind,
final ChargingSchedule? chargingSchedule,
}) = _Profile;
factory ChargingProfile.fromJson(Map<String, dynamic> json) => _$ChargingProfileFromJson(json);
@override
Map<String, Expression> toColumns(bool nullToAbsent) {
return ChargingProfilesCompanion(
profileId: Value(profileId),
name: Value(name),
stackLevel: Value(stackLevel),
operatorId: Value(operatorId),
validFromDate: Value(validFromDate),
validToDate: Value(validToDate),
profilePurpose: Value(profilePurpose),
profileKind: Value(profileKind),
recurrencyKind: Value(recurrencyKind),
chargingSchedule: Value(chargingSchedule),
).toColumns(nullToAbsent);
}
} |
👋
Given two freezed classes where one is "nested" in the other.
The generated code for json_serializable doesn't serialize the nested object. DEserialization works as expected.
At first, I thought that it's an issue with json_serializable but if that library is used by itself, it serializes nested objects marked with
@JsonSerializable
just fine.The text was updated successfully, but these errors were encountered: