json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

Add ignoreDecode/ignoreEncode support

Open charles-toller opened this issue 5 years ago • 4 comments

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.

charles-toller avatar Aug 19 '20 18:08 charles-toller

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?

charles-toller avatar Aug 19 '20 18:08 charles-toller

This functionality will be really helpful to me

FernandoUFS avatar Oct 31 '20 19:10 FernandoUFS

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;

edlman avatar Mar 24 '21 09:03 edlman

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;

This is a perfect workaround

a-wallen avatar Apr 26 '22 02:04 a-wallen

More likely to go with something like https://github.com/google/json_serializable.dart/pull/1178/

kevmoo avatar Oct 01 '22 01:10 kevmoo