[pve-devel] [PATCH dart_api_client 1/4] Add string serializer to handle int and double values
Aaron Lauterer
a.lauterer at proxmox.com
Wed Jun 23 12:04:41 CEST 2021
Adding a custom serializer for Strings allows us to catch situations
where the PVE API provides an int or a double instead of the expected
string and convert that to a string.
Signed-off-by: Aaron Lauterer <a.lauterer at proxmox.com>
---
lib/src/models/pve_string_serializer.dart | 25 +++++++++++++++++++++++
lib/src/models/serializers.dart | 2 ++
2 files changed, 27 insertions(+)
create mode 100644 lib/src/models/pve_string_serializer.dart
diff --git a/lib/src/models/pve_string_serializer.dart b/lib/src/models/pve_string_serializer.dart
new file mode 100644
index 0000000..0622360
--- /dev/null
+++ b/lib/src/models/pve_string_serializer.dart
@@ -0,0 +1,25 @@
+import 'package:built_collection/built_collection.dart';
+import 'package:built_value/serializer.dart';
+
+class PveStringSerializer implements PrimitiveSerializer<String> {
+ final bool structured = false;
+ @override
+ final Iterable<Type> types = BuiltList<Type>([String]);
+ @override
+ final String wireName = 'String';
+
+ @override
+ Object serialize(Serializers serializers, String string,
+ {FullType specifiedType = FullType.unspecified}) {
+ return string;
+ }
+
+ @override
+ String deserialize(Serializers serializers, Object? serialized,
+ {FullType specifiedType = FullType.unspecified}) {
+ if (serialized is int || serialized is double) {
+ return serialized.toString();
+ }
+ return serialized as String;
+ }
+}
diff --git a/lib/src/models/serializers.dart b/lib/src/models/serializers.dart
index c079ee3..57b06f4 100644
--- a/lib/src/models/serializers.dart
+++ b/lib/src/models/serializers.dart
@@ -3,6 +3,7 @@ import 'package:built_value/json_object.dart';
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'package:proxmox_dart_api_client/src/models/pve_bool_serializer.dart';
+import 'package:proxmox_dart_api_client/src/models/pve_string_serializer.dart';
import 'package:proxmox_dart_api_client/src/models/pve_datetime_from_epoch_serializer.dart';
import 'package:proxmox_dart_api_client/src/models/pve_models.dart';
@@ -41,5 +42,6 @@ part 'serializers.g.dart';
final Serializers serializers = (_$serializers.toBuilder()
..addPlugin(StandardJsonPlugin())
..add(PveBoolSerializer())
+ ..add(PveStringSerializer())
..add(PveDateTimeFromEpoch()))
.build();
--
2.30.2
More information about the pve-devel
mailing list