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
Improve encoding of values to handle null cases #1223
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -143,6 +143,24 @@ class _JsonConvertData { | |||||||||||||
accessor.isEmpty ? '' : '.$accessor'; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// If there is no converter for the params, return `null`. | ||||||||||||||
/// | ||||||||||||||
/// Otherwise, returns `true` if the converter has a null return value. | ||||||||||||||
/// | ||||||||||||||
/// Used to make sure we create a smart encoding function. | ||||||||||||||
bool? hasConverterNullEncode( | ||||||||||||||
DartType targetType, | ||||||||||||||
TypeHelperContextWithConfig ctx, | ||||||||||||||
) { | ||||||||||||||
final data = _typeConverter(targetType, ctx); | ||||||||||||||
|
||||||||||||||
if (data == null) { | ||||||||||||||
return null; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return data.jsonType.isNullableType; | ||||||||||||||
Comment on lines
+157
to
+161
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.
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
_JsonConvertData? _typeConverter( | ||||||||||||||
DartType targetType, | ||||||||||||||
TypeHelperContextWithConfig ctx, | ||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
json_serializable/test/integration/converter_examples.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'converter_examples.g.dart'; | ||
|
||
@JsonEnum(valueField: 'value') | ||
enum Issue1202RegressionEnum { | ||
normalValue(42), | ||
nullValue(null); | ||
|
||
const Issue1202RegressionEnum(this.value); | ||
|
||
final int? value; | ||
} | ||
|
||
@JsonSerializable(includeIfNull: false) | ||
class Issue1202RegressionClass { | ||
@JsonKey(fromJson: _fromJson, toJson: _toJson) | ||
final int valueWithFunctions; | ||
|
||
@_Issue1202RegressionNotNullConverter() | ||
final int notNullableValueWithConverter; | ||
|
||
final Issue1202RegressionEnum value; | ||
final int? normalNullableValue; | ||
|
||
@_Issue1202RegressionConverter() | ||
final int notNullableValueWithNullableConverter; | ||
|
||
@JsonKey(fromJson: _fromJsonNullable, toJson: _toJsonNullable) | ||
final int valueWithNullableFunctions; | ||
|
||
Issue1202RegressionClass({ | ||
required this.value, | ||
required this.normalNullableValue, | ||
required this.notNullableValueWithNullableConverter, | ||
required this.notNullableValueWithConverter, | ||
required this.valueWithFunctions, | ||
required this.valueWithNullableFunctions, | ||
}); | ||
|
||
factory Issue1202RegressionClass.fromJson(Map<String, dynamic> json) => | ||
_$Issue1202RegressionClassFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$Issue1202RegressionClassToJson(this); | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
other is Issue1202RegressionClass && | ||
jsonEncode(other) == jsonEncode(this); | ||
|
||
@override | ||
int get hashCode => jsonEncode(this).hashCode; | ||
|
||
static int _fromJsonNullable(String? json) { | ||
if (json == null) return _default; | ||
return int.parse(json); | ||
} | ||
|
||
static String? _toJsonNullable(int object) { | ||
if (object == _default) return null; | ||
return object.toString(); | ||
} | ||
|
||
static int _fromJson(String json) => int.parse(json); | ||
|
||
static String _toJson(int object) => object.toString(); | ||
} | ||
|
||
const _default = 42; | ||
|
||
class _Issue1202RegressionConverter extends JsonConverter<int, String?> { | ||
const _Issue1202RegressionConverter(); | ||
|
||
@override | ||
int fromJson(String? json) { | ||
if (json == null) return _default; | ||
return int.parse(json); | ||
} | ||
|
||
@override | ||
String? toJson(int object) { | ||
if (object == _default) return null; | ||
return object.toString(); | ||
} | ||
} | ||
|
||
class _Issue1202RegressionNotNullConverter extends JsonConverter<int, String> { | ||
const _Issue1202RegressionNotNullConverter(); | ||
|
||
@override | ||
int fromJson(String json) => int.parse(json); | ||
|
||
@override | ||
String toJson(int object) => object.toString(); | ||
} |
60 changes: 60 additions & 0 deletions
60
json_serializable/test/integration/converter_examples.g.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.