fix: Fixing FormatException when parsing some JSON DateTimes

This commit is contained in:
Chris Cameron
2019-04-03 16:19:53 -04:00
parent f60de4321b
commit 5756d9654b

View File

@@ -1,5 +1,4 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using ICD.Common.Properties;
@@ -16,9 +15,6 @@ namespace ICD.Common.Utils.Json
[PublicAPI]
public static class JsonUtils
{
// 2016-02-26T19:24:59
private const string DATE_FORMAT = @"yyyy-MM-dd\THH:mm:ss";
private const string MESSAGE_NAME_PROPERTY = "m";
private const string MESSAGE_DATA_PROPERTY = "d";
@@ -44,6 +40,16 @@ namespace ICD.Common.Utils.Json
JsonConvert.DeserializeObject(serialized, type);
}
/// <summary>
/// Gets the data as a DateTime value.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static DateTime ParseDateTime(string data)
{
return DateTime.Parse(data);
}
/// <summary>
/// Gets the token as a DateTime value.
/// </summary>
@@ -55,11 +61,7 @@ namespace ICD.Common.Utils.Json
if (token == null)
throw new ArgumentNullException("token");
#if SIMPLSHARP
return DateTime.ParseExact((string)token, DATE_FORMAT, CultureInfo.CurrentCulture);
#else
return (DateTime)token;
#endif
return ParseDateTime((string)token);
}
/// <summary>
@@ -81,6 +83,10 @@ namespace ICD.Common.Utils.Json
output = ParseDateTime(token);
return true;
}
catch (FormatException)
{
return false;
}
catch (InvalidCastException)
{
return false;