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

fix: support update JSON that uses comment or trailing comma #313

Merged
merged 3 commits into from
Jun 8, 2024
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
10 changes: 8 additions & 2 deletions src/Nogic.WritableOptions/JsonWritableOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ namespace Nogic.WritableOptions;
private static readonly JsonWriterOptions _jsonWriterOptions = new()
{
Indented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};

private static readonly JsonReaderOptions _jsonReaderOptions = new()
{
CommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
};

/// <summary>
Expand Down Expand Up @@ -117,7 +123,7 @@ public void Update(TOptions changedValue, bool reload = false)
stream.Position = 0;
}

var reader = new Utf8JsonReader(utf8Json.Length > 0 ? utf8Json : "{}"u8);
var reader = new Utf8JsonReader(utf8Json.Length > 0 ? utf8Json : "{}"u8, _jsonReaderOptions);
var currentJson = JsonElement.ParseValue(ref reader);
var writer = new Utf8JsonWriter(stream, _jsonWriterOptions);

Expand Down
41 changes: 29 additions & 12 deletions test/Nogic.WritableOptions.Tests/JsonWritableOptions.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,30 @@ public void OnChange_Called_IOptionsMonitor_OnChange()
}
}
""";
// lang=json,strict
private const string HasCommentJson = $$"""
{
// Line Comment
"{{nameof(SampleOption)}}": {
/*
Block Comment
*/
"{{nameof(SampleOption.LastLaunchedAt)}}": "2020-10-01T00:00:00",
"{{nameof(SampleOption.Interval)}}": 1000,
"{{nameof(SampleOption.ConnectionString)}}": "bar"
}
}
""";
// lang=json,strict
private const string TrailingCommaJson = $$"""
{
"{{nameof(SampleOption)}}": {
"{{nameof(SampleOption.LastLaunchedAt)}}": "2020-10-01T00:00:00",
"{{nameof(SampleOption.Interval)}}": 1000,
"{{nameof(SampleOption.ConnectionString)}}": "bar",
},
}
""";

/// <summary>
/// <see cref="JsonWritableOptions{TOptions}.Update"/> writes expected JSON.
Expand All @@ -175,6 +199,8 @@ public void OnChange_Called_IOptionsMonitor_OnChange()
[DataRow(EmptyJson, DisplayName = ".Update(TOptions) writes expected JSON")]
[DataRow(ClassEmptyJson, DisplayName = ".Update(TOptions) writes expected JSON")]
[DataRow(HasConfigJson, DisplayName = ".Update(TOptions) writes expected JSON")]
[DataRow(HasCommentJson, DisplayName = ".Update(TOptions) writes expected JSON")]
[DataRow(TrailingCommaJson, DisplayName = ".Update(TOptions) writes expected JSON")]
public void Update_Writes_Json(string fileText)
{
// Arrange
Expand Down Expand Up @@ -202,6 +228,8 @@ public void Update_Writes_Json(string fileText)
[DataRow(EmptyJson, DisplayName = ".Update(TOptions) writes expected JSON with BOM")]
[DataRow(ClassEmptyJson, DisplayName = ".Update(TOptions) writes expected JSON with BOM")]
[DataRow(HasConfigJson, DisplayName = ".Update(TOptions) writes expected JSON with BOM")]
[DataRow(HasCommentJson, DisplayName = ".Update(TOptions) writes expected JSON with BOM")]
[DataRow(TrailingCommaJson, DisplayName = ".Update(TOptions) writes expected JSON")]
public void Update_Writes_Json_WithBOM(string fileText)
{
// Arrange
Expand All @@ -217,18 +245,7 @@ public void Update_Writes_Json_WithBOM(string fileText)
sut.Update(_updatedOption);

// Assert
/*lang=json,strict*/
const string expectedJson =
$$"""
{
"{{nameof(SampleOption)}}": {
"{{nameof(SampleOption.LastLaunchedAt)}}": "2020-12-01T00:00:00",
"{{nameof(SampleOption.Interval)}}": 5000,
"{{nameof(SampleOption.ConnectionString)}}": "foo"
}
}
""";
_ = tempFile.ReadAllText(Encoding.UTF8).Should().Be(NormalizeEndLine(expectedJson));
_ = tempFile.ReadAllText(Encoding.UTF8).Should().Be(NormalizeEndLine(ExpectedJson));
configStub.Verify(static m => m.Reload(), Times.Never());
}

Expand Down
Loading