diff --git a/src/Nogic.WritableOptions/JsonWritableOptions.cs b/src/Nogic.WritableOptions/JsonWritableOptions.cs index 661606c..e055eb3 100644 --- a/src/Nogic.WritableOptions/JsonWritableOptions.cs +++ b/src/Nogic.WritableOptions/JsonWritableOptions.cs @@ -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, }; /// @@ -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); diff --git a/test/Nogic.WritableOptions.Tests/JsonWritableOptions.Test.cs b/test/Nogic.WritableOptions.Tests/JsonWritableOptions.Test.cs index e449b90..fcf7f35 100644 --- a/test/Nogic.WritableOptions.Tests/JsonWritableOptions.Test.cs +++ b/test/Nogic.WritableOptions.Tests/JsonWritableOptions.Test.cs @@ -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", + }, + } + """; /// /// writes expected JSON. @@ -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 @@ -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 @@ -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()); }