Open jtdLab opened 1 year ago
You can use @JsonKey(includeToJson: false)
on props
getter.
And also you can create abstract class like bellow for farther implementation :)
abstract class EquatableJsonSerializable extends Equatable {
const EquatableJsonSerializable() : super();
@JsonKey(includeToJson: false)
@override
bool? get stringify => super.stringify;
@JsonKey(includeToJson: false)
@override
int get hashCode => super.hashCode;
}
Yeah but thats just a workaround. When generating both fromJson
and toJson
the package already works correctly without the need for a workaround. So the package should also handle only generating toJson
correctly.
When using this package to generate fromJson
and toJson
methods, it ensures that when you serialize and then deserialize an object, you receive the same object as before. However, when you use only the toJson generator, it includes all getter calls and their values and also standard class fields in the JSON.
Exactly but why is it done this way?
Exactly but why is it done this way?
I had to pick a default when I started. Seemed reasonable 🤷
I had to pick a default when I started. Seemed reasonable 🤷
Makes sence to start this way, but would you agree that @JsonSerializable(createFactory: false)
should include the same fields as @JsonSerializable()
and @JsonSerializable(createToJson: false)
?
Reading the comment from @BarteeX-Q it sounds like this is expected behaviour, but having an option createFactory
altering the output of the toJson
method is very confusing and for me it is a bug.
I agree with @jtdLab, they should be the same, in my opinion.
Including the hashCode
, stringify
, etc. fields should be a separate option to avoid confusion.
I spent half a day trying to find out what the problem was.
Using
json_serializable: ^6.7.1
on a model class which extendsEquatable
leads to unwanted fields being included in generated code.Given:
the output is
with multiple unwanted fields being included in the json output.
Hint: When using
@JsonSerializable()
or@JsonSerializable(createToJson: false)
all works as expected.