Skip to content

includeToJson: false is not working properly #1280

@mernen

Description

@mernen

Given the following class:

@JsonSerializable(createFactory: false, createToJson: true)
class SerializationTest {
  SerializationTest(this.x, this.y);

  final int x;
  @JsonKey(includeToJson: false)
  final int y;

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

I would expect the generated serializer to only include x. However, this is the produced function:

Map<String, dynamic> _$SerializationTestToJson(SerializationTest instance) =>
    <String, dynamic>{
      'x': instance.x,
      'y': instance.y,
    };

Also serializing y.

y disappears if we change the annotation to includeFromJson: false.

I believe the culprit is this line:

bool get explicitNoToJson => includeFromJson == false;

It should test for includeToJson == false.


Environment:

dependencies:
  json_annotation: ^4.8.0

dev_dependencies:
  build_runner: ^2.3.3
  json_serializable: ^6.6.0
$ dart --version
Dart SDK version: 2.19.0-444.2.beta (beta) (Thu Dec 15 17:12:16 2022 +0000) on "macos_arm64"

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions