From 02ae577faa0bd4938507061d603e4f9447e2b64f Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Sun, 23 Aug 2020 08:44:31 +0300 Subject: Fixed issue with insights file datetime utc. added insights application exception/crash. added remote actions service. fixed issue with LiteDB hang on application start/exit. reduced insights listener empty frames. --- .../Tango.Core/Bson/BsonUtcSerializer.cs | 68 ++++++++++++++++++++++ .../Visual_Studio/Tango.Core/Tango.Core.csproj | 3 +- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 Software/Visual_Studio/Tango.Core/Bson/BsonUtcSerializer.cs (limited to 'Software/Visual_Studio/Tango.Core') diff --git a/Software/Visual_Studio/Tango.Core/Bson/BsonUtcSerializer.cs b/Software/Visual_Studio/Tango.Core/Bson/BsonUtcSerializer.cs new file mode 100644 index 000000000..6f5cb7700 --- /dev/null +++ b/Software/Visual_Studio/Tango.Core/Bson/BsonUtcSerializer.cs @@ -0,0 +1,68 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Core.Bson +{ + public class BsonUtcSerializer : JsonSerializer + { + private class DateTimeConverter : JsonConverter + { + public override bool CanConvert(Type objectType) + { + return typeof(DateTime) == objectType + || typeof(DateTime?) == objectType; + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + var dateTimeOffset = (DateTime)value; + // Serialize DateTimeOffset as round-trip formatted string + serializer.Serialize(writer, dateTimeOffset.ToString("O")); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + if (reader.TokenType != JsonToken.String && reader.TokenType != JsonToken.Date) + return null; + + DateTime dt; + + var dateWithOffset = (String)reader.Value; + + if (String.IsNullOrEmpty(dateWithOffset)) + return null; + + if (DateTime.TryParseExact(dateWithOffset, "O", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dt)) + return dt; + + return null; + } + + } + + private class DateTimeContractResolver : DefaultContractResolver + { + protected override JsonContract CreateContract(Type objectType) + { + var contract = base.CreateContract(objectType); + + if (objectType == typeof(DateTime) || objectType == typeof(DateTime?)) + contract.Converter = new DateTimeConverter(); + + return contract; + } + } + + public BsonUtcSerializer() + { + ContractResolver = new DateTimeContractResolver(); + DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind; + } + } +} diff --git a/Software/Visual_Studio/Tango.Core/Tango.Core.csproj b/Software/Visual_Studio/Tango.Core/Tango.Core.csproj index 2db9c8643..2ecd441f2 100644 --- a/Software/Visual_Studio/Tango.Core/Tango.Core.csproj +++ b/Software/Visual_Studio/Tango.Core/Tango.Core.csproj @@ -95,6 +95,7 @@ GlobalVersionInfo.cs + @@ -216,7 +217,7 @@ - + -- cgit v1.3.1