Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Json Formatter altering date's time zone. #574

Merged
merged 1 commit into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 46 additions & 29 deletions src/dev/impl/DevToys/Helpers/JsonYaml/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,40 +61,57 @@ internal static string Format(string? input, Indentation indentationMode)

try
{
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;
Expand Down
7 changes: 7 additions & 0 deletions src/tests/DevToys.Tests/Helpers/JsonHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public void FormatMinified(string input, string expectedResult)
Assert.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("", "")]
Expand Down