28

Is there a way to ignore the serialization of a property within a JsonSerializable class?

I'm using build_runner to generate the mapping code.

One way to achieve this is by commenting the mapping for that specific property within the .g.dart-file though it would be great if an ignore attribute could be added on the property.

import 'package:json_annotation/json_annotation.dart';

part 'example.g.dart';

@JsonSerializable()
class Example {
  Example({this.a, this.b, this.c,});

  int a;
  int b;

  /// Ignore this property
  int c;

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

  Map<String, dynamic> toJson() => _$ExampleToJson(this);
}

Which results in

Example _$ExampleFromJson(Map<String, dynamic> json) {
  return Example(a: json['a'] as int, b: json['b'] as int, c: json['c'] as int);
}

Map<String, dynamic> _$ExampleToJson(Example instance) =>
    <String, dynamic>{'a': instance.a, 'b': instance.b, 'c': instance.c};

What I do to achieve this is by commenting the mapping of c.

Example _$ExampleFromJson(Map<String, dynamic> json) {
  return Example(a: json['a'] as int, b: json['b'] as int, c: json['c'] as int);
}

Map<String, dynamic> _$ExampleToJson(Example instance) =>
    <String, dynamic>{'a': instance.a, 'b': instance.b, /* 'c': instance.c */};

4 Answers 4

66

Update

This has changed to

@JsonKey(includeFromJson: false, includeToJson: false)

from before

Add @JsonKey(ignore: true) before the field that you don't want to include

 @JsonKey(ignore: true)
 int c;

See also https://github.com/dart-lang/json_serializable/blob/06718b94d8e213e7b057326e3d3c555c940c1362/json_annotation/lib/src/json_key.dart#L45-L49

9
  • 1
    That's just great, it works like a charm! I just checked the build_runner documentation and couldn't find it there. I guess it is within the JsonAnnotation-documentation? Thank you for the very quick response.
    – Alex
    Mar 15, 2019 at 9:54
  • 2
    This doesn't seem to work when you use inheritance... Jun 30, 2020 at 2:53
  • 5
    how to make it only ignore toJson method?
    – buncis
    Apr 21, 2021 at 20:30
  • 2
    pardon my language before, my use case is: there is property that I want transform from the json to my model but I want to ignore when I create the json from the model, hope this reply clarify it.
    – buncis
    Apr 23, 2021 at 17:28
  • 1
    @buncis I think I got it, but I don't have a solution Apr 24, 2021 at 16:20
12

Depreciation warning

@Deprecated(
      'Use `includeFromJson` and `includeToJson` with a value of `false` '
      'instead.',
    )
this.ignore,

NEW

As @Peking mentioned The new way is to specify whether to add or remove in fromJson and toJson functions

@JsonKey(includeFromJson: true, includeToJson: false)

OLD:

As @Günter Zöchbauer mentioned The old way is by passing ignore: true to the JsonKey

@JsonKey(ignore: true)
String value;
6

A workaround is to set the property to null and set includeIfNull flag to false:

toNull(_) => null;

@freezed
class User with _$User {
  const factory User({
    ...

    @Default(false)
    @JsonKey(toJson: toNull, includeIfNull: false)
        bool someReadOnlyProperty,
  }) = _User;

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}

And the generated code is:

/// user.g.dart

Map<String, dynamic> _$$_UserToJson(_$_User instance) {
  final val = <String, dynamic>{
    ...
  };

  void writeNotNull(String key, dynamic value) {
    if (value != null) {
      val[key] = value;
    }
  }

  writeNotNull('someReadOnlyProperty', toNull(instance.someReadOnlyProperty));
  return val;
}
4
  • I have checked this approach and it does not work. Receiving The parameter someReadOnlyProperty of User is non-nullable but is neither required nor marked with @Default @Andrey Gordeev Oct 23, 2022 at 14:42
  • Also if anyone knows a valid solution related to Freeze, please answer this question stackoverflow.com/questions/74171868 Oct 23, 2022 at 14:43
  • @genericUser did you forget to add @Default? Oct 24, 2022 at 9:44
  • Yes! Thanks @Andrey Gordeev Oct 24, 2022 at 11:47
0

@JsonKey(includeFromJson: false, includeToJson: false)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.