From 34ec571fa1ab4663d55a6753e58a81dc2ed6a703 Mon Sep 17 00:00:00 2001 From: Konstantina Chremmou Date: Wed, 7 Mar 2018 12:47:37 +0000 Subject: [PATCH] Return null if the string to deserialize to a XenRef is null. This can happen for calls returning a XenRef when the server is using the JsonRpc v1.0 and we have an error, in which case the result is null. Similarly, return DateTime.MinValue when the datetime string is null (or in general unparasable). Signed-off-by: Konstantina Chremmou --- XenModel/XenAPI/Converters.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/XenModel/XenAPI/Converters.cs b/XenModel/XenAPI/Converters.cs index a462ee177..65b6aeff8 100644 --- a/XenModel/XenAPI/Converters.cs +++ b/XenModel/XenAPI/Converters.cs @@ -55,7 +55,7 @@ namespace XenAPI { JToken jToken = JToken.Load(reader); var str = jToken.ToObject(); - return new XenRef(str); + return string.IsNullOrEmpty(str) ? null : new XenRef(str); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) @@ -392,7 +392,20 @@ namespace XenAPI public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken jToken = JToken.Load(reader); - return DateTime.ParseExact(jToken.ToString(), DateFormats, CultureInfo.InvariantCulture, DateTimeStyles.None); + var str = jToken.ToObject(); + + try + { + return DateTime.ParseExact(str, DateFormats, CultureInfo.InvariantCulture, DateTimeStyles.None); + } + catch (FormatException) + { + return DateTime.MinValue; + } + catch (ArgumentException) + { + return DateTime.MinValue; + } } } @@ -402,7 +415,7 @@ namespace XenAPI public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken jToken = JToken.Load(reader); - return Helper.EnumParseDefault(objectType, jToken.ToString()); + return Helper.EnumParseDefault(objectType, jToken.ToObject()); } } }