Skip to content

Introduced ignore attribute to JsonKey. When set to true the generato… #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

AlexanderJohr
Copy link
Contributor

Hello Kevin Moore, from all the serialization libraries for dart out there, yours was always my favorite. But for me, it was a major feature: an ignore annotation.

This pull request is for the ignore attribute for JsonKey. When set to true the generator excludes the field.

@JsonSerializable(includeIfNull: false, nullable: false)
class Column extends _$ColumnSerializerMixin {
// [...]
// ColumnSettings is serializable
ColumnSettings settings = new ColumnSettings();

// ColumnSettings is already serializable and includes witdh.
// Without 'ignore' the generator would generate the same field for ColumnSettings and Column 
@JsonKey(ignore: true)
num get width => settings.width;
set width (num value) =>settings.width = value;
// [...]
}

What is also does is:

  • exclude private fields
  • refactored _writeFactory to return the fields that it used to generate the factory instead of those which are not set by it.
  • adds a test "works to ignore a field"
  • adds a test 'includes final field in toJson when set in ctor' which ensures the final field 'a' that is set in the constructor is included in the factory and in toJson.
  • adds a test 'excludes final field in toJson when not set in ctor' which ensures that the same field is excluded in factory and toJson if it isn't set in the constructor.

…r excludes the field.

Refactored _writeFactory to return the fields that were used to set, including final fields set in the constructor parameter list but excluding other final fields.
Added test "works to ignore a field"
Added test 'includes final field in toJson when set in ctor' which ensures the final field 'a' that is set in the constructor is included in the factory and in toJson.
Added test 'excludes final field in toJson when not set in ctor' which ensures that the same field is excluded in factory and toJson if it isn't set in the constructor.
Copy link
Collaborator

@kevmoo kevmoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a changelog entry to json_serializable for this.

Check the error if a ctor references a field that's ignored.

Also add an example to the integration test

diff --git a/json_serializable/test/test_files/json_test_example.dart b/json_serializable/test/test_files/json_test_example.dart
index 2a82b8b..06356cc 100644
--- a/json_serializable/test/test_files/json_test_example.dart
+++ b/json_serializable/test/test_files/json_test_example.dart
@@ -49,6 +49,12 @@ class Order extends Object with _$OrderSerializerMixin {
   Platform platform;
   Map<String, Platform> altPlatforms;

+  String get platformValue => platform?.description;
+
+  set platformValue(String value) {
+    throw 'not impld';
+  }
+
   int get price => items.fold(0, (total, item) => item.price + total);

   Order(this.category, [Iterable<Item> items])

@@ -65,10 +65,16 @@ class JsonKey {
/// enclosing class.
final bool includeIfNull;

/// `true` if the generator should ignore this field completely
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a . at the end of the sentence.

@@ -65,10 +65,16 @@ class JsonKey {
/// enclosing class.
final bool includeIfNull;

/// `true` if the generator should ignore this field completely
///
/// If `null` or `false`, the generator includes the field if it is not
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If `null` (the default) or `false`, the field will be considered for serialization.

@@ -65,10 +65,16 @@ class JsonKey {
/// enclosing class.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: bump the minor version of json_annotation in the pubspec to 0.2.3-dev.

Add an entry to the changelog (under 0.2.3) that includes this change.

@AlexanderJohr
Copy link
Contributor Author

What should I do with this hint from travis hint • 'parameterKind' is deprecated and shouldn't be used at lib/src/json_serializable_generator.dart:355:17 • deprecated_member_use

It's caused by an update of Dart 2 and I can't find info how to replace it.

@kevmoo
Copy link
Collaborator

kevmoo commented Mar 26, 2018

Rebase on this branch and you'll be good - https://github.com/dart-lang/json_serializable/tree/exclude_analyzer_alpha

AlexanderJohr and others added 11 commits March 26, 2018 14:09
…r excludes the field.

Refactored _writeFactory to return the fields that were used to set, including final fields set in the constructor parameter list but excluding other final fields.
Added test "works to ignore a field"
Added test 'includes final field in toJson when set in ctor' which ensures the final field 'a' that is set in the constructor is included in the factory and in toJson.
Added test 'excludes final field in toJson when not set in ctor' which ensures that the same field is excluded in factory and toJson if it isn't set in the constructor.
… if private field is referenced by ctor' to ensure the right error is thrown if a private or ignored field is used as a required ctor argument.
Copy link
Collaborator

@kevmoo kevmoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@kevmoo kevmoo merged commit a4b377c into google:master Mar 27, 2018
@AlexanderJohr
Copy link
Contributor Author

Yes! Thank you for your reviews and your approval!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants