diff --git a/VectoConsole/Properties/Version.tt b/VectoConsole/Properties/Version.tt index 5fd5704ae6c1af994a8634347a5f6bca23ee4617..3cafed29337e598e2b64161c96560c61315df647 100644 --- a/VectoConsole/Properties/Version.tt +++ b/VectoConsole/Properties/Version.tt @@ -1,11 +1,8 @@ <#@ template language="C#" #> <#@ output extension=".cs"#> using System.Reflection; - -// generated on <#= DateTime.UtcNow.ToString() #> [assembly: AssemblyVersion("3.0.1.<#= this.RevisionNumber #>")] [assembly: AssemblyFileVersion("3.0.1.<#= this.RevisionNumber #>")] - -<#+ - int RevisionNumber = (int)(DateTime.UtcNow - new DateTime(2015, 1, 1)).TotalDays; +<#+ + int RevisionNumber = (int)(DateTime.UtcNow - new DateTime(2015, 1, 1)).TotalDays; #> \ No newline at end of file diff --git a/VectoCore/FileIO/JsonDataHeader.cs b/VectoCore/FileIO/JsonDataHeader.cs index 36b5e9cb5862febdf93475b0d8598bbd1b29636c..9768d9ab373d70e9bdf103a642977297b9740dcc 100644 --- a/VectoCore/FileIO/JsonDataHeader.cs +++ b/VectoCore/FileIO/JsonDataHeader.cs @@ -1,12 +1,22 @@ -using Newtonsoft.Json; +using System; +using Newtonsoft.Json; +using TUGraz.VectoCore.Utils; namespace TUGraz.VectoCore.FileIO { + /// <summary> + /// "Header": { + /// "CreatedBy": "Raphael Luz IVT TU-Graz (85407225-fc3f-48a8-acda-c84a05df6837)", + /// "Date": "29.07.2015 16:59:03", + /// "AppVersion": "2.2", + /// "FileVersion": 7 + /// }, + /// </summary> public class JsonDataHeader { - [JsonProperty(Required = Required.Always)] public string AppVersion; [JsonProperty(Required = Required.Always)] public string CreatedBy; - [JsonProperty(Required = Required.Always)] public string Date; + [JsonProperty(Required = Required.Always), JsonConverter(typeof(DateTimeFallbackDeserializer))] public DateTime Date; + [JsonProperty(Required = Required.Always)] public string AppVersion; [JsonProperty(Required = Required.Always)] public uint FileVersion; #region Equality members diff --git a/VectoCore/Properties/Version.tt b/VectoCore/Properties/Version.tt index d2a128cd5f45d0b169d4418477d3469a9a78cf59..3cafed29337e598e2b64161c96560c61315df647 100644 --- a/VectoCore/Properties/Version.tt +++ b/VectoCore/Properties/Version.tt @@ -1,10 +1,8 @@ <#@ template language="C#" #> <#@ output extension=".cs"#> using System.Reflection; -// generated on <#= DateTime.UtcNow.ToString() #> [assembly: AssemblyVersion("3.0.1.<#= this.RevisionNumber #>")] [assembly: AssemblyFileVersion("3.0.1.<#= this.RevisionNumber #>")] - -<#+ - int RevisionNumber = (int)(DateTime.UtcNow - new DateTime(2015, 1, 1)).TotalDays; +<#+ + int RevisionNumber = (int)(DateTime.UtcNow - new DateTime(2015, 1, 1)).TotalDays; #> \ No newline at end of file diff --git a/VectoCore/Utils/DateTimeFallbackDeserializer.cs b/VectoCore/Utils/DateTimeFallbackDeserializer.cs new file mode 100644 index 0000000000000000000000000000000000000000..0863e67aabcbe6fec6048d9bd41e7329a12d7a8d --- /dev/null +++ b/VectoCore/Utils/DateTimeFallbackDeserializer.cs @@ -0,0 +1,35 @@ +using System; +using System.Globalization; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; + +namespace TUGraz.VectoCore.Utils +{ + /// <summary> + /// Accepts following date formats: + /// * German: d.M.yyyy HH:mm:ss + /// * English: M/d/yyyy HH:mm:ss tt + /// * ISO8601: yyyy-MM-DDTHH:mm:ssZ + /// * Local culture format (based on current localization of the user) + /// Output is in ISO8601 format. + /// </summary> + public class DateTimeFallbackDeserializer : IsoDateTimeConverter + { + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + DateTime dateTime; + if (reader.TokenType == JsonToken.Date) { + return reader.Value; + } + if (DateTime.TryParseExact((string)reader.Value, new[] { "d.M.yyyy HH:mm:ss", "M/d/yyyy HH:mm:ss tt" }, + CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) { + return DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); + } + + if (DateTime.TryParse((string)reader.Value, out dateTime)) { + return DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); + } + return base.ReadJson(reader, objectType, existingValue, serializer); + } + } +} \ No newline at end of file diff --git a/VectoCore/VectoCore.csproj b/VectoCore/VectoCore.csproj index afa07c2c560682996bd396f2613b03028f6e972e..8d619a1d7765dfe0a0ebf409902f2c950ff74fdd 100644 --- a/VectoCore/VectoCore.csproj +++ b/VectoCore/VectoCore.csproj @@ -189,6 +189,7 @@ <DependentUpon>Version.tt</DependentUpon> </Compile> <Compile Include="Utils\DataTableExtensionMethods.cs" /> + <Compile Include="Utils\DateTimeFallbackDeserializer.cs" /> <Compile Include="Utils\EnumHelper.cs" /> <Compile Include="Utils\RessourceHelper.cs" /> <Compile Include="Models\Declaration\Segment.cs" /> diff --git a/VectoCoreTest/FileIO/JsonTest.cs b/VectoCoreTest/FileIO/JsonTest.cs index 7c40c6b9fb84746284491394cbb9e7829443877e..24be786a27bcaebb4be257bbaaf46c32cfbba824 100644 --- a/VectoCoreTest/FileIO/JsonTest.cs +++ b/VectoCoreTest/FileIO/JsonTest.cs @@ -1,4 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json; using TUGraz.VectoCore.FileIO; namespace TUGraz.VectoCore.Tests.FileIO @@ -6,19 +8,34 @@ namespace TUGraz.VectoCore.Tests.FileIO [TestClass] public class JsonTest { + private const string jsonExpected = @"{ + ""CreatedBy"": ""Michael Krisper"", + ""Date"": ""2015-11-17T11:49:03Z"", + ""AppVersion"": ""3.0.1.320"", + ""FileVersion"": 7 +}"; + + private const string jsonExpected2 = @"{ + ""CreatedBy"": ""Michael Krisper"", + ""Date"": ""2015-01-07T11:49:03Z"", + ""AppVersion"": ""3.0.1.320"", + ""FileVersion"": 7 +}"; + + [TestMethod] public void TestJsonHeaderEquality() { var h1 = new JsonDataHeader { AppVersion = "MyVecto3", CreatedBy = "UnitTest", - Date = "1.1.1970", + Date = new DateTime(1970, 1, 1), FileVersion = 3 }; var h2 = new JsonDataHeader { AppVersion = "MyVecto3", CreatedBy = "UnitTest", - Date = "1.1.1970", + Date = new DateTime(1970, 1, 1), FileVersion = 3 }; Assert.AreEqual(h1, h1); @@ -26,5 +43,126 @@ namespace TUGraz.VectoCore.Tests.FileIO Assert.AreNotEqual(h1, null); Assert.AreNotEqual(h1, "hello world"); } + + [TestMethod] + public void Test_Json_DateFormat_German() + { + var json = @"{ + ""CreatedBy"": ""Michael Krisper"", + ""Date"": ""17.11.2015 11:49:03"", + ""AppVersion"": ""3.0.1.320"", + ""FileVersion"": 7 +}"; + var header = JsonConvert.DeserializeObject<JsonDataHeader>(json); + + Assert.AreEqual("3.0.1.320", header.AppVersion); + Assert.AreEqual(7u, header.FileVersion); + Assert.AreEqual("Michael Krisper", header.CreatedBy); + Assert.AreEqual(new DateTime(2015, 11, 17, 11, 49, 3, DateTimeKind.Utc), header.Date); + + var jsonCompare = JsonConvert.SerializeObject(header, Formatting.Indented); + Assert.AreEqual(jsonExpected, jsonCompare); + } + + [TestMethod] + public void Test_Json_DateFormat_German2() + { + var json = @"{ + ""CreatedBy"": ""Michael Krisper"", + ""Date"": ""7.1.2015 11:49:03"", + ""AppVersion"": ""3.0.1.320"", + ""FileVersion"": 7 +}"; + var header = JsonConvert.DeserializeObject<JsonDataHeader>(json); + + Assert.AreEqual("3.0.1.320", header.AppVersion); + Assert.AreEqual(7u, header.FileVersion); + Assert.AreEqual("Michael Krisper", header.CreatedBy); + Assert.AreEqual(new DateTime(2015, 1, 7, 11, 49, 3, DateTimeKind.Utc), header.Date); + + var jsonCompare = JsonConvert.SerializeObject(header, Formatting.Indented); + Assert.AreEqual(jsonExpected2, jsonCompare); + } + + [TestMethod] + public void Test_Json_DateFormat_English() + { + var json = @"{ + ""CreatedBy"": ""Michael Krisper"", + ""Date"": ""11/17/2015 11:49:03 AM"", + ""AppVersion"": ""3.0.1.320"", + ""FileVersion"": 7 +}"; + var header = JsonConvert.DeserializeObject<JsonDataHeader>(json); + + Assert.AreEqual("3.0.1.320", header.AppVersion); + Assert.AreEqual(7u, header.FileVersion); + Assert.AreEqual("Michael Krisper", header.CreatedBy); + Assert.AreEqual(new DateTime(2015, 11, 17, 11, 49, 3, DateTimeKind.Utc), header.Date); + + var jsonCompare = JsonConvert.SerializeObject(header, Formatting.Indented); + Assert.AreEqual(jsonExpected, jsonCompare); + } + + [TestMethod] + public void Test_Json_DateFormat_English2() + { + var json = @"{ + ""CreatedBy"": ""Michael Krisper"", + ""Date"": ""1/7/2015 11:49:03 AM"", + ""AppVersion"": ""3.0.1.320"", + ""FileVersion"": 7 +}"; + var header = JsonConvert.DeserializeObject<JsonDataHeader>(json); + + Assert.AreEqual("3.0.1.320", header.AppVersion); + Assert.AreEqual(7u, header.FileVersion); + Assert.AreEqual("Michael Krisper", header.CreatedBy); + Assert.AreEqual(new DateTime(2015, 1, 7, 11, 49, 3, DateTimeKind.Utc), header.Date); + + var jsonCompare = JsonConvert.SerializeObject(header, Formatting.Indented); + Assert.AreEqual(jsonExpected2, jsonCompare); + } + + + [TestMethod] + public void Test_Json_DateFormat_ISO8601() + { + var json = @"{ + ""CreatedBy"": ""Michael Krisper"", + ""Date"": ""2015-11-17T11:49:03Z"", + ""AppVersion"": ""3.0.1.320"", + ""FileVersion"": 7 +}"; + var header = JsonConvert.DeserializeObject<JsonDataHeader>(json); + + Assert.AreEqual("3.0.1.320", header.AppVersion); + Assert.AreEqual(7u, header.FileVersion); + Assert.AreEqual("Michael Krisper", header.CreatedBy); + Assert.AreEqual(new DateTime(2015, 11, 17, 11, 49, 3, DateTimeKind.Utc), header.Date); + + var jsonCompare = JsonConvert.SerializeObject(header, Formatting.Indented); + Assert.AreEqual(json, jsonCompare); + } + + [TestMethod] + public void Test_Json_DateFormat_ISO8601_CET() + { + var json = @"{ + ""CreatedBy"": ""Michael Krisper"", + ""Date"": ""2015-11-17T11:49:03+01:00"", + ""AppVersion"": ""3.0.1.320"", + ""FileVersion"": 7 +}"; + var header = JsonConvert.DeserializeObject<JsonDataHeader>(json); + + Assert.AreEqual("3.0.1.320", header.AppVersion); + Assert.AreEqual(7u, header.FileVersion); + Assert.AreEqual("Michael Krisper", header.CreatedBy); + Assert.AreEqual(new DateTime(2015, 11, 17, 11, 49, 3, DateTimeKind.Utc), header.Date); + + var jsonCompare = JsonConvert.SerializeObject(header, Formatting.Indented); + Assert.AreEqual(json, jsonCompare); + } } } \ No newline at end of file diff --git a/VectoCoreTest/VectoCoreTest.csproj b/VectoCoreTest/VectoCoreTest.csproj index ace6d23cf28490106842ebd484a47686ac1f7aa9..176794c9104fde36ab3cfb424eb0c3d6dc31b827 100644 --- a/VectoCoreTest/VectoCoreTest.csproj +++ b/VectoCoreTest/VectoCoreTest.csproj @@ -36,6 +36,10 @@ </PropertyGroup> <ItemGroup> <Reference Include="Microsoft.CSharp" /> + <Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath> + </Reference> <Reference Include="NLog"> <HintPath>..\packages\NLog.4.0.1\lib\net45\NLog.dll</HintPath> </Reference> diff --git a/VectoCoreTest/packages.config b/VectoCoreTest/packages.config index 573e2fd16f8cd28f35e521d53253818d0d0b2e95..d47bf188ae6866b9bf155f95bad1b46ecfd71bb3 100644 --- a/VectoCoreTest/packages.config +++ b/VectoCoreTest/packages.config @@ -1,4 +1,5 @@ <?xml version="1.0" encoding="utf-8"?> <packages> + <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" /> <package id="NLog" version="4.0.1" targetFramework="net45" /> </packages> \ No newline at end of file