public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Aaron Lauterer <a.lauterer@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH dart_api_client 2/4] Add int serializer to handle string values
Date: Wed, 23 Jun 2021 12:04:42 +0200	[thread overview]
Message-ID: <20210623100444.1440650-3-a.lauterer@proxmox.com> (raw)
In-Reply-To: <20210623100444.1440650-1-a.lauterer@proxmox.com>

Adding a custom serializer for integers, allows us to handle situations
in which the PVE API provides the values in other types. For now, the
most likely situation is that the API formats the JSON value as string
instead of a number, therefore we try to catch that and parse it as int.

Signed-off-by: Aaron Lauterer <a.lauterer@proxmox.com>
---
 lib/src/models/pve_int_serializer.dart | 25 +++++++++++++++++++++++++
 lib/src/models/serializers.dart        |  2 ++
 2 files changed, 27 insertions(+)
 create mode 100644 lib/src/models/pve_int_serializer.dart

diff --git a/lib/src/models/pve_int_serializer.dart b/lib/src/models/pve_int_serializer.dart
new file mode 100644
index 0000000..8986463
--- /dev/null
+++ b/lib/src/models/pve_int_serializer.dart
@@ -0,0 +1,25 @@
+import 'package:built_collection/built_collection.dart';
+import 'package:built_value/serializer.dart';
+
+class PveIntSerializer implements PrimitiveSerializer<int> {
+  final bool structured = false;
+  @override
+  final Iterable<Type> types = BuiltList<Type>([int]);
+  @override
+  final String wireName = 'String';
+
+  @override
+  Object serialize(Serializers serializers, int value,
+      {FullType specifiedType = FullType.unspecified}) {
+    return value;
+  }
+
+  @override
+  int deserialize(Serializers serializers, Object? serialized,
+      {FullType specifiedType = FullType.unspecified}) {
+    if (serialized is String) {
+      return int.parse(serialized);
+    }
+    return serialized as int;
+  }
+}
diff --git a/lib/src/models/serializers.dart b/lib/src/models/serializers.dart
index 57b06f4..2a0d501 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_int_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';
@@ -42,6 +43,7 @@ part 'serializers.g.dart';
 final Serializers serializers = (_$serializers.toBuilder()
       ..addPlugin(StandardJsonPlugin())
       ..add(PveBoolSerializer())
+      ..add(PveIntSerializer())
       ..add(PveStringSerializer())
       ..add(PveDateTimeFromEpoch()))
     .build();
-- 
2.30.2





  parent reply	other threads:[~2021-06-23 10:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-23 10:04 [pve-devel] [PATCH series/flutter 0/4] fixes related to PVE 7 Aaron Lauterer
2021-06-23 10:04 ` [pve-devel] [PATCH dart_api_client 1/4] Add string serializer to handle int and double values Aaron Lauterer
2021-06-23 10:04 ` Aaron Lauterer [this message]
2021-06-23 10:04 ` [pve-devel] [PATCH dart_api_client 3/4] Add double serializer to handle String and int values Aaron Lauterer
2021-06-23 10:04 ` [pve-devel] [PATCH flutter_frontend 4/4] power settings: fix handling null for template status Aaron Lauterer
2021-06-23 11:36 ` [pve-devel] applied-series: [PATCH series/flutter 0/4] fixes related to PVE 7 Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210623100444.1440650-3-a.lauterer@proxmox.com \
    --to=a.lauterer@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal