diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-08-23 08:44:31 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-08-23 08:44:31 +0300 |
| commit | 02ae577faa0bd4938507061d603e4f9447e2b64f (patch) | |
| tree | 7daf0e275338ec92e93bfca39f2d529d93858162 /Software/Visual_Studio/Tango.Core/Bson | |
| parent | 67770063ff1a1c5c522e3bc29f442c42eb6dc521 (diff) | |
| download | Tango-02ae577faa0bd4938507061d603e4f9447e2b64f.tar.gz Tango-02ae577faa0bd4938507061d603e4f9447e2b64f.zip | |
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.
Diffstat (limited to 'Software/Visual_Studio/Tango.Core/Bson')
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/Bson/BsonUtcSerializer.cs | 68 |
1 files changed, 68 insertions, 0 deletions
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; + } + } +} |
