json_serializable.dart
json_serializable.dart copied to clipboard
Add ignoreDecode/ignoreEncode support
Fixes #569
This pull request adds the ability to annotate a field with ignoreDecode or ignoreEncode keys, which work similar to the ignore key, but only for one side of the process, allowing for keys that are decoded but not serialized, or serialized but not decoded.
Travis build appears to be failing because json_serializable needs the update from json_annotation, which pub can't find. Should I split this into two pull requests?
This functionality will be really helpful to me
I would suggest to reuse existing field's attributes toJson and fromJson - ignore it when set to null. I use following workaround with ignore global function which always returns null and null values are not processed (by includeIfNull: false).
@JsonSerializable(includeIfNull: false)
class Model {
@JsonKey(fromJson: ignore)
int writeOnlyNumber;
@JsonKey(toJson: ignore)
String readOnlyText;
}
T? ignore<T>(dynamic _) => null;
I would suggest to reuse existing field's attributes
toJsonandfromJson- ignore it when set tonull. I use following workaround withignoreglobal function which always returns null and null values are not processed (byincludeIfNull: false).@JsonSerializable(includeIfNull: false) class Model { @JsonKey(fromJson: ignore) int writeOnlyNumber; @JsonKey(toJson: ignore) String readOnlyText; } T? ignore<T>(dynamic _) => null;
This is a perfect workaround
More likely to go with something like https://github.com/google/json_serializable.dart/pull/1178/