diff options
Diffstat (limited to 'Software/Visual_Studio/Tango.Core')
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/Bson/BsonUtcSerializer.cs | 68 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/Tango.Core.csproj | 3 |
2 files changed, 70 insertions, 1 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; + } + } +} 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 @@ <Compile Include="..\Versioning\GlobalVersionInfo.cs"> <Link>GlobalVersionInfo.cs</Link> </Compile> + <Compile Include="Bson\BsonUtcSerializer.cs" /> <Compile Include="Components\CmdCommand.cs" /> <Compile Include="CustomAttributes\PropertyIndexAttribute.cs" /> <Compile Include="CustomAttributes\StringFormatAttribute.cs" /> @@ -216,7 +217,7 @@ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <ProjectExtensions> <VisualStudio> - <UserProperties BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UseGlobalSettings="False" BuildVersion_StartDate="2000/1/1" /> + <UserProperties BuildVersion_StartDate="2000/1/1" BuildVersion_UseGlobalSettings="False" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" /> </VisualStudio> </ProjectExtensions> <Import Project="..\packages\System.Data.SQLite.Core.1.0.108.0\build\net46\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.108.0\build\net46\System.Data.SQLite.Core.targets')" /> |
