Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve encoding of values to handle null cases (#1223)
Specifically with enums and converter functions when `includeIfNull` is `false`. Enum values with `null` as an output where not properly wrapped. This is now fixed. Fixes #1202 Also 'unwrap' the cases where conversion functions never return `null`.
- Loading branch information
Showing
13 changed files
with
313 additions
and
50 deletions.
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
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.