diff --git a/ICD.Common.Utils/Extensions/DateTimeExtensions.cs b/ICD.Common.Utils/Extensions/DateTimeExtensions.cs index 8901c49..697c5e8 100644 --- a/ICD.Common.Utils/Extensions/DateTimeExtensions.cs +++ b/ICD.Common.Utils/Extensions/DateTimeExtensions.cs @@ -116,6 +116,16 @@ namespace ICD.Common.Utils.Extensions return extends.StartOfDay() + new TimeSpan(24, 0, 0); } + /// + /// Returns the give date in ISO-8601 format. + /// + /// + /// + public static string ToIso(this DateTime extends) + { + return extends.ToString("o"); + } + /// /// Returns the given date in unix timestamp format. /// diff --git a/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs b/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs index 8b5346a..cb1ad98 100644 --- a/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs +++ b/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs @@ -69,7 +69,7 @@ namespace ICD.Common.Utils.Extensions throw new ArgumentNullException("extends"); extends.WritePropertyName(propertyName); - extends.WriteValue(value); + extends.WriteValue(value.ToIso()); } /// diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index 7e27079..abb9f32 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -131,6 +131,7 @@ + diff --git a/ICD.Common.Utils/Json/DateTimeIsoConverter.cs b/ICD.Common.Utils/Json/DateTimeIsoConverter.cs new file mode 100644 index 0000000..42271eb --- /dev/null +++ b/ICD.Common.Utils/Json/DateTimeIsoConverter.cs @@ -0,0 +1,58 @@ +using System; +using System.Text.RegularExpressions; +using ICD.Common.Utils.Extensions; +using Newtonsoft.Json; + +namespace ICD.Common.Utils.Json +{ + public sealed class DateTimeIsoConverter : AbstractGenericJsonConverter + { + /// + /// Writes the JSON representation of the object. + /// + /// The to write to. + /// The value. + /// The calling serializer. + public override void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer) + { + writer.WriteValue(value.ToIso()); + } + + /// + /// Reads the JSON representation of the object. + /// + /// The to read from. + /// The existing value of object being read. + /// The calling serializer. + /// + /// The object value. + /// + public override DateTime ReadJson(JsonReader reader, DateTime existingValue, JsonSerializer serializer) + { + /* + "\"\\/Date(1335205592410)\\/\"" .NET JavaScriptSerializer + "\"\\/Date(1335205592410-0500)\\/\"" .NET DataContractJsonSerializer + "2012-04-23T18:25:43.511Z" JavaScript built-in JSON object + "2012-04-21T18:25:43-05:00" ISO 8601 + */ + + string serial = reader.GetValueAsString(); + + Match match; + if (RegexUtils.Matches(serial, @"Date\((?'date'\d+)(?'zone'(-|\+)\d+)?\)", out match)) + { + long ms = long.Parse(match.Groups["date"].Value); + DateTime dateTime = DateTimeUtils.FromEpochMilliseconds(ms); + if (!match.Groups["zone"].Success) + return dateTime; + + // No TimeZoneInfo in CF, so now things get gross + dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified); + string iso = dateTime.ToIso() + match.Groups["zone"].Value; + return DateTime.Parse(iso); + } + + return DateTime.Parse(serial); + } + } +} diff --git a/ICD.Common.Utils/Json/JsonUtils.cs b/ICD.Common.Utils/Json/JsonUtils.cs index 4051688..cfc3c5d 100644 --- a/ICD.Common.Utils/Json/JsonUtils.cs +++ b/ICD.Common.Utils/Json/JsonUtils.cs @@ -17,6 +17,33 @@ namespace ICD.Common.Utils.Json private const string MESSAGE_NAME_PROPERTY = "m"; private const string MESSAGE_DATA_PROPERTY = "d"; + private static JsonSerializerSettings s_CommonSettings; + + /// + /// Gets the common JSON serializer settings for cross-platform support. + /// + public static JsonSerializerSettings CommonSettings + { + get + { + if (s_CommonSettings != null) + return s_CommonSettings; + + s_CommonSettings = new JsonSerializerSettings + { +#if !SIMPLSHARP + // Turn off the ridiculous new behaviour of DateTiming anything vaguely resembling a date + DateParseHandling = DateParseHandling.None, +#endif + }; + + // Serialize DateTimes to ISO + s_CommonSettings.Converters.Add(new DateTimeIsoConverter()); + + return s_CommonSettings; + } + } + /// /// Serializes the given item and formats the JSON into a human-readable form. ///