A Swiss Army knife for developers.
C# blazorcross-platformcsharpdesktopdesktop-appdesktop-applicationdeveloper-toolsextensiblefluentfluent-designlinuxmacosmicasyntax-highlightingtypescriptwindowswindows-10windows-11winui
Fixed Json Formatter altering date's time zone. (#574)
Etienne Baudoux10ea1f81892d
diff --git a/src/dev/impl/DevToys/Helpers/JsonYaml/JsonHelper.cs b/src/dev/impl/DevToys/Helpers/JsonYaml/JsonHelper.csindex b231561f9..ea95d5252 100644--- a/src/dev/impl/DevToys/Helpers/JsonYaml/JsonHelper.cs+++ b/src/dev/impl/DevToys/Helpers/JsonYaml/JsonHelper.cs@@ -61,40 +61,57 @@ namespace DevToys.Helpers.JsonYamltry{- var jtoken = JToken.Parse(input);- if (jtoken is not null)+ var jsonLoadSettings = new JsonLoadSettings()+ {+ CommentHandling = CommentHandling.Ignore,+ DuplicatePropertyNameHandling = DuplicatePropertyNameHandling.Ignore,+ LineInfoHandling = LineInfoHandling.Load+ };++ using (var jsonReader = new JsonTextReader(new StringReader(input))){- var stringBuilder = new StringBuilder();- using (var stringWriter = new StringWriter(stringBuilder))- using (var jsonTextWriter = new JsonTextWriter(stringWriter))+ jsonReader.DateParseHandling = DateParseHandling.None;+ jsonReader.DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;++ var jtoken = JToken.ReadFrom(jsonReader, jsonLoadSettings);+ if (jtoken is not null){- switch (indentationMode)+ var stringBuilder = new StringBuilder();+ using (var stringWriter = new StringWriter(stringBuilder))+ using (var jsonTextWriter = new JsonTextWriter(stringWriter)){- case Indentation.TwoSpaces:- jsonTextWriter.Formatting = Formatting.Indented;- jsonTextWriter.IndentChar = ' ';- jsonTextWriter.Indentation = 2;- break;- case Indentation.FourSpaces:- jsonTextWriter.Formatting = Formatting.Indented;- jsonTextWriter.IndentChar = ' ';- jsonTextWriter.Indentation = 4;- break;- case Indentation.OneTab:- jsonTextWriter.Formatting = Formatting.Indented;- jsonTextWriter.IndentChar = '\t';- jsonTextWriter.Indentation = 1;- break;- case Indentation.Minified:- jsonTextWriter.Formatting = Formatting.None;- break;- default:- throw new NotSupportedException();+ switch (indentationMode)+ {+ case Indentation.TwoSpaces:+ jsonTextWriter.Formatting = Formatting.Indented;+ jsonTextWriter.IndentChar = ' ';+ jsonTextWriter.Indentation = 2;+ break;+ case Indentation.FourSpaces:+ jsonTextWriter.Formatting = Formatting.Indented;+ jsonTextWriter.IndentChar = ' ';+ jsonTextWriter.Indentation = 4;+ break;+ case Indentation.OneTab:+ jsonTextWriter.Formatting = Formatting.Indented;+ jsonTextWriter.IndentChar = '\t';+ jsonTextWriter.Indentation = 1;+ break;+ case Indentation.Minified:+ jsonTextWriter.Formatting = Formatting.None;+ break;+ default:+ throw new NotSupportedException();+ }++ jsonTextWriter.DateFormatHandling = DateFormatHandling.IsoDateFormat;+ jsonTextWriter.DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;++ jtoken.WriteTo(jsonTextWriter);}- jtoken.WriteTo(jsonTextWriter);- }- return stringBuilder.ToString();+ return stringBuilder.ToString();+ }}return string.Empty;diff --git a/src/tests/DevToys.Tests/Helpers/JsonHelperTests.cs b/src/tests/DevToys.Tests/Helpers/JsonHelperTests.csindex a045290dd..653425053 100644--- a/src/tests/DevToys.Tests/Helpers/JsonHelperTests.cs+++ b/src/tests/DevToys.Tests/Helpers/JsonHelperTests.cs@@ -65,6 +65,13 @@ namespace DevToys.Tests.HelpersAssert.AreEqual(expectedResult, JsonHelper.Format(input, Indentation.Minified));}+ [DataTestMethod]+ [DataRow("{ \"Date\": \"2012-04-21T18:25:43-05:00\" }", "{\"Date\":\"2012-04-21T18:25:43-05:00\"}")]+ public void FormatDoesNotAlterateDateTimes(string input, string expectedResult)+ {+ Assert.AreEqual(expectedResult, JsonHelper.Format(input, Indentation.Minified));+ }+[DataTestMethod][DataRow(null, "")][DataRow("", "")]