Util methods for parsing a JSON datetime value

This commit is contained in:
Chris Cameron
2017-11-14 14:08:52 -05:00
parent 55eb344cdf
commit c6aa7d7b3e

View File

@@ -1,8 +1,11 @@
using System.Linq;
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using ICD.Common.Properties;
using ICD.Common.Utils.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ICD.Common.Utils.Json
{
@@ -12,6 +15,9 @@ 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";
/// <summary>
/// Forces Newtonsoft to cache the given type for faster subsequent usage.
/// </summary>
@@ -23,6 +29,49 @@ namespace ICD.Common.Utils.Json
JsonConvert.DeserializeObject<T>(serialized);
}
/// <summary>
/// Gets the token as a DateTime value.
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
[PublicAPI]
public static DateTime ParseDateTime(JToken token)
{
if (token == null)
throw new ArgumentNullException("token");
#if SIMPLSHARP
return DateTime.ParseExact((string)token, DATE_FORMAT, CultureInfo.CurrentCulture);
#else
return (DateTime)token;
#endif
}
/// <summary>
/// Gets the token as a DateTime value.
/// </summary>
/// <param name="token"></param>
/// <param name="output"></param>
/// <returns></returns>
[PublicAPI]
public static bool TryParseDateTime(JToken token, out DateTime output)
{
if (token == null)
throw new ArgumentNullException("token");
output = default(DateTime);
try
{
output = ParseDateTime(token);
return true;
}
catch (InvalidCastException)
{
return false;
}
}
/// <summary>
/// Pretty-prints the JSON document.
/// </summary>