freezed
freezed copied to clipboard
Can't create custom method toJson
Describe the bug
While creating a freezed class with json_serializable I want to create a custom method toJson but generating freezer code taking always method created by json_serializable in this case _$$_CustomToJson
in generated file custom.freezed.dart
@override
Map<String, dynamic> toJson() {
return _$$_CustomToJson(this);
}
To Reproduce
fluter 3.0.2
dependencies: freezed_annotation: ^2.0.3 json_annotation: ^4.5.0
dev_dependencies: freezed: ^2.0.3+1 json_serializable: ^6.2.0
import 'package:freezed_annotation/freezed_annotation.dart';
part 'custom.freezed.dart';
part 'custom.g.dart';
@freezed
class Custom with _$Custom {
factory Custom({
required String id,
required String name,
}) = _Custom;
factory Custom.fromJson(Map<String, dynamic> json) => _$CustomFromJson(json);
Map<String, dynamic> toJson() {
return {
'custom_id': id,
'name': name,
};
}
}
also tried with
@override
Map<String, dynamic> toJson() {
return {
'custom_id': id,
'name': name,
};
}
and wit annotation @Freezed(toJson: false) but with this, I can't add toJson without error in freezed file
Expected behavior if defined in class method toJosn should be executed instead of generated one
As specified in the readme, if you want to define methods in your Freezed classes, you need to define an empty private constructor:
@freezed
class Custom with _$Custom {
+ Custom._();
factory Custom({
required String id,
required String name,
}) = _Custom;
factory Custom.fromJson(Map<String, dynamic> json) => _$CustomFromJson(json);
Map<String, dynamic> toJson() {
return {
'custom_id': id,
'name': name,
};
}
}
The main problem is that if someone wants to override toJson() method generated by json_serializable it is not possible without removing part 'custom.g.dart';
to not generate a file and create their own method fromJson() as well
Why so?
why does it is happen in this way you ask?
@rrousselGit Can we return dynamic
value from toJson
. The scenario is while using Unions, I want to return either a Json or a String or an integer based on the type. Is this possible?
import 'package:freezed_annotation/freezed_annotation.dart';
part 'custom.freezed.dart';
part 'custom.g.dart';
@freezed
class Custom with _$Custom {
Custom._();
factory Custom.test1({
required String id,
required String name,
}) = CustomTest1;
factory Custom.test2({
required String id,
required String description,
}) = CustomTest2;
factory Custom.fromJson(Map<String, dynamic> json) => _$CustomFromJson(json);
Map<String, dynamic> toJson() {
return {
'custom_id': id,
};
}
}
Custom custom = CustomTest1(id: 'id', name: 'name');
print('custom: ${custom.toJson()}');
The above code's output was this:
custom: {id: id, name: name, runtimeType: test1}
I can't get an overriden toJson()
to work either, even after defining an empty private constructor.
import 'package:freezed_annotation/freezed_annotation.dart'; part 'custom.freezed.dart'; part 'custom.g.dart'; @freezed class Custom with _$Custom { Custom._(); factory Custom.test1({ required String id, required String name, }) = CustomTest1; factory Custom.test2({ required String id, required String description, }) = CustomTest2; factory Custom.fromJson(Map<String, dynamic> json) => _$CustomFromJson(json); Map<String, dynamic> toJson() { return { 'custom_id': id, }; } }
Custom custom = CustomTest1(id: 'id', name: 'name'); print('custom: ${custom.toJson()}');
The above code's output was this:
custom: {id: id, name: name, runtimeType: test1}
Which version of freezed are you using? This doesn't work for me
import 'package:freezed_annotation/freezed_annotation.dart'; part 'custom.freezed.dart'; part 'custom.g.dart'; @freezed class Custom with _$Custom { Custom._(); factory Custom.test1({ required String id, required String name, }) = CustomTest1; factory Custom.test2({ required String id, required String description, }) = CustomTest2; factory Custom.fromJson(Map<String, dynamic> json) => _$CustomFromJson(json); Map<String, dynamic> toJson() { return { 'custom_id': id, }; } }
Custom custom = CustomTest1(id: 'id', name: 'name'); print('custom: ${custom.toJson()}');
The above code's output was this:
custom: {id: id, name: name, runtimeType: test1}
Which version of freezed are you using? This doesn't work for me
Using 2.0.3
version.
Faced the same problem and so far there is nothing left but to define my own toJson
and disable generation via the flag @Freezed(toJson: false)
, or use another method like toCustomJson
within which to reuse the generated one as needed.