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

Feature Request Remove objects where all fields are null

Open martipello opened this issue 8 months ago • 0 comments

Hopefully this already has a solution, but i want to request a flag similar to includeIfNull which removes an object where all its fields are null, so given

@freezed
class Transfer with _$Transfer {
    @JsonSerializable(explicitToJson: true, includeIfNull: false)
    const factory Transfer({
    String? provider,
    String? comment,
    double? amount,
  }) = _Transfer;

  factory Transfer.fromJson(Map<String, dynamic> json) => _$TransferFromJson(json);

}`

Given the model is nested in a parent if all fields were null the output would be :

{
   "transfer": {}
}

without includeIfNull the output would be :

{
   "transfer":{
      "provider":null,
      "comment":null,
      "amount":null
   }
}

i am requesting a flag to make the output :

{}

currently i have to make a check that any of these fields are not null before i include the object, so if there is a better way please let me know.

Many thanks

martipello avatar May 10 '25 18:05 martipello