-
Notifications
You must be signed in to change notification settings - Fork 418
More control over which fields should be added to fromJson and toJson #1178
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
Changes from all commits
e3fe2f0
44a0962
ac11a6b
ebcd03f
94d98a8
418c11a
61d0cb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
part of '_json_serializable_test_input.dart'; | ||
|
||
@ShouldGenerate(r''' | ||
IncludeWithToJson<T, S> _$IncludeWithToJsonFromJson<T extends num, S>( | ||
Map<String, dynamic> json) => | ||
IncludeWithToJson<T, S>()..someString = json['someString'] as String; | ||
|
||
Map<String, dynamic> _$IncludeWithToJsonToJson<T extends num, S>( | ||
IncludeWithToJson<T, S> instance) => | ||
<String, dynamic>{ | ||
'someString': instance.someString, | ||
'getterIncluded': instance.getterIncluded, | ||
'finalFieldIncluded': instance.finalFieldIncluded, | ||
}; | ||
''') | ||
@JsonSerializable() | ||
class IncludeWithToJson<T extends num, S> { | ||
late String someString; | ||
|
||
String get getterExcluded => someString.toLowerCase(); | ||
|
||
@JsonKey(includeWith: IncludeWith.toJson) | ||
String get getterIncluded => someString.toLowerCase(); | ||
|
||
final finalFieldExcluded = 'finalFieldExcluded'; | ||
|
||
@JsonKey(includeWith: IncludeWith.toJson) | ||
final finalFieldIncluded = 'finalFieldIncluded'; | ||
|
||
IncludeWithToJson(); | ||
} | ||
|
||
@ShouldGenerate(r''' | ||
Map<String, dynamic> _$IncludeWithToJsonNoFactoryToJson<T extends num, S>( | ||
IncludeWithToJsonNoFactory<T, S> instance) => | ||
<String, dynamic>{ | ||
'someString': instance.someString, | ||
'getterIncludeWithNull': instance.getterIncludeWithNull, | ||
'getterIncludeWithToJson': instance.getterIncludeWithToJson, | ||
'getterIncludeWithBoth': instance.getterIncludeWithBoth, | ||
}; | ||
''') | ||
@JsonSerializable(createFactory: false) | ||
class IncludeWithToJsonNoFactory<T extends num, S> { | ||
late String someString; | ||
|
||
String get getterIncludeWithNull => someString.toLowerCase(); | ||
|
||
@JsonKey(includeWith: IncludeWith.ignore) | ||
String get getterIncludeWithIgnore => someString.toLowerCase(); | ||
|
||
@JsonKey(includeWith: IncludeWith.toJson) | ||
String get getterIncludeWithToJson => someString.toLowerCase(); | ||
|
||
@JsonKey(includeWith: IncludeWith.fromJson) | ||
String get getterIncludeWithFromJson => someString.toLowerCase(); | ||
|
||
@JsonKey(includeWith: IncludeWith.both) | ||
String get getterIncludeWithBoth => someString.toLowerCase(); | ||
|
||
IncludeWithToJsonNoFactory(); | ||
} | ||
|
||
@ShouldGenerate(r''' | ||
IncludeWithFromJson<T, S> _$IncludeWithFromJsonFromJson<T extends num, S>( | ||
Map<String, dynamic> json) => | ||
IncludeWithFromJson<T, S>() | ||
..someString = json['someString'] as String | ||
..setter = json['setter'] as String; | ||
|
||
Map<String, dynamic> _$IncludeWithFromJsonToJson<T extends num, S>( | ||
IncludeWithFromJson<T, S> instance) => | ||
<String, dynamic>{}; | ||
''') | ||
@JsonSerializable() | ||
class IncludeWithFromJson<T extends num, S> { | ||
@JsonKey(includeWith: IncludeWith.fromJson) | ||
late String someString; | ||
|
||
@JsonKey(includeWith: IncludeWith.fromJson) | ||
String get setter => someString; | ||
|
||
set setter(String value) => someString = value; | ||
Comment on lines
+84
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it would be possible to also support fields that only have setters? @kevmoo would you consider this PR, if I implemented this, or are there other issues with this PR or just limited time/resources from your side? |
||
|
||
IncludeWithFromJson(); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a fifth option, currently called
legacy
. Fields that use this option should behave as fields that previously usedignore: false
. Currently this is the default option.