From e2c485aedecabad9110c2e54af277b2299681d52 Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Sun, 25 Apr 2021 15:51:19 +0200 Subject: [PATCH] Adding some conversion checks for JSON State serialization --- TelegramBotBase/SessionBase.cs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/TelegramBotBase/SessionBase.cs b/TelegramBotBase/SessionBase.cs index 67e309d..7096000 100644 --- a/TelegramBotBase/SessionBase.cs +++ b/TelegramBotBase/SessionBase.cs @@ -198,6 +198,8 @@ namespace TelegramBotBase catch (ArgumentException ex) { + CustomConversionChecks(form, p, f); + } catch { @@ -238,6 +240,35 @@ namespace TelegramBotBase } + private static void CustomConversionChecks(FormBase form, KeyValuePair p, System.Reflection.PropertyInfo f) + { + //Newtonsoft Int64/Int32 converter issue + if (f.PropertyType == typeof(Int32)) + { + int i = 0; + if(int.TryParse(p.Value.ToString(), out i)) + { + f.SetValue(form, i); + } + return; + } + + //Newtonsoft Double/Decimal converter issue + if(f.PropertyType == typeof(Decimal) | f.PropertyType == typeof(Nullable)) + { + decimal d = 0; + if(decimal.TryParse(p.Value.ToString(), out d)) + { + f.SetValue(form, d); + } + return; + } + + + } + + + /// /// Saves all open states into the machine. ///