diff --git a/ICD.Common.Utils/DateTimeUtils.cs b/ICD.Common.Utils/DateTimeUtils.cs index 4dd01ef..5380f11 100644 --- a/ICD.Common.Utils/DateTimeUtils.cs +++ b/ICD.Common.Utils/DateTimeUtils.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; namespace ICD.Common.Utils { @@ -33,5 +34,15 @@ namespace ICD.Common.Utils { return FromEpochMilliseconds(seconds * 1000); } + + /// + /// Returns a DateTime for the given ISO-8601 string. + /// + /// + /// + public static DateTime FromIso8601(string iso) + { + return DateTime.Parse(iso, null, DateTimeStyles.RoundtripKind); + } } } diff --git a/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs b/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs index a568178..375cdac 100644 --- a/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs +++ b/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs @@ -1,5 +1,5 @@ using System; -using System.Globalization; +using System.Text.RegularExpressions; using ICD.Common.Properties; using Newtonsoft.Json; @@ -271,35 +271,29 @@ namespace ICD.Common.Utils.Extensions if (extends.DateParseHandling != DateParseHandling.None) return (DateTime)extends.Value; #endif + /* + "\"\\/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 stringValue = extends.GetValueAsString(); - return DateTime.Parse(stringValue, null, DateTimeStyles.RoundtripKind); - } + string serial = extends.GetValueAsString(); - /// - /// Gets the current value as a date. - /// - /// - /// - /// - /// - public static DateTime GetValueAsDateTimeExact([NotNull] this JsonReader extends, [NotNull] string format, - IFormatProvider provider) - { - if (extends == null) - throw new ArgumentNullException("extends"); + 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; - if (format == null) - throw new ArgumentNullException("format"); + // No TimeZoneInfo in CF, so now things get gross + dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified); + serial = dateTime.ToIso() + match.Groups["zone"].Value; + } -#if !SIMPLSHARP - // Newer NewtonSoft tries to be helpful by assuming that anything that looks like a DateTime must be a date. - if (extends.DateParseHandling != DateParseHandling.None) - throw new InvalidOperationException("DateParseHandling needs to be set to None"); -#endif - - string stringValue = extends.GetValueAsString(); - return DateTime.ParseExact(stringValue, format, provider); + return DateTimeUtils.FromIso8601(serial); } } } diff --git a/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs b/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs index cb1ad98..f33ab43 100644 --- a/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs +++ b/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs @@ -6,13 +6,31 @@ namespace ICD.Common.Utils.Extensions { public static class JsonWriterExtensions { + /// + /// Writes the DateTime as an ISO-8601 string. + /// + /// + /// + public static void WriteDateTime([NotNull] this JsonWriter extends, DateTime dateTime) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + string iso = dateTime.ToIso(); + + // Remove redundant ms + iso = iso.Replace(".0000000", ""); + + extends.WriteValue(iso); + } + /// /// Writes the type value. /// /// /// [PublicAPI] - public static void WriteType([NotNull]this JsonWriter extends, [CanBeNull]Type type) + public static void WriteType([NotNull] this JsonWriter extends, [CanBeNull] Type type) { if (extends == null) throw new ArgumentNullException("extends"); @@ -69,7 +87,7 @@ namespace ICD.Common.Utils.Extensions throw new ArgumentNullException("extends"); extends.WritePropertyName(propertyName); - extends.WriteValue(value.ToIso()); + extends.WriteDateTime(value); } /// diff --git a/ICD.Common.Utils/Json/DateTimeIsoConverter.cs b/ICD.Common.Utils/Json/DateTimeIsoConverter.cs index 66e2584..0d95337 100644 --- a/ICD.Common.Utils/Json/DateTimeIsoConverter.cs +++ b/ICD.Common.Utils/Json/DateTimeIsoConverter.cs @@ -1,6 +1,4 @@ using System; -using System.Globalization; -using System.Text.RegularExpressions; using ICD.Common.Utils.Extensions; using Newtonsoft.Json; @@ -16,12 +14,7 @@ namespace ICD.Common.Utils.Json /// The calling serializer. public override void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer) { - string iso = value.ToIso(); - - // Remove redundant ms - iso = iso.Replace(".0000000", ""); - - writer.WriteValue(iso); + writer.WriteDateTime(value); } /// @@ -35,29 +28,7 @@ namespace ICD.Common.Utils.Json /// 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); - serial = dateTime.ToIso() + match.Groups["zone"].Value; - } - - return DateTime.Parse(serial, null, DateTimeStyles.RoundtripKind); + return reader.GetValueAsDateTime(); } } } diff --git a/ICD.Common.Utils/Json/JsonUtils.cs b/ICD.Common.Utils/Json/JsonUtils.cs index cfc3c5d..5528e3e 100644 --- a/ICD.Common.Utils/Json/JsonUtils.cs +++ b/ICD.Common.Utils/Json/JsonUtils.cs @@ -52,7 +52,7 @@ namespace ICD.Common.Utils.Json [PublicAPI] public static string Format(object value) { - string serial = JsonConvert.SerializeObject(value); + string serial = JsonConvert.SerializeObject(value, Formatting.None, CommonSettings); return Format(serial); }