Return null if the string to deserialize to a XenRef<T> is null. This can

happen for calls returning a XenRef<T> 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 <konstantina.chremmou@citrix.com>
This commit is contained in:
Konstantina Chremmou 2018-03-07 12:47:37 +00:00 committed by Mihaela Stoica
parent 929ae78942
commit 34ec571fa1

View File

@ -55,7 +55,7 @@ namespace XenAPI
{
JToken jToken = JToken.Load(reader);
var str = jToken.ToObject<string>();
return new XenRef<T>(str);
return string.IsNullOrEmpty(str) ? null : new XenRef<T>(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<string>();
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<string>());
}
}
}