Skip to content

Commit a183ff7

Browse files
authored
Support JSON comments (#548)
1 parent 5c01227 commit a183ff7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Source/Common/SchemaSerializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static class SchemaSerializer
2121
AllowTrailingCommas = true,
2222
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
2323
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
24+
ReadCommentHandling = JsonCommentHandling.Skip,
2425
};
2526

2627
/// <summary>
@@ -31,6 +32,7 @@ public static class SchemaSerializer
3132
{
3233
AllowTrailingCommas = true,
3334
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
35+
ReadCommentHandling = JsonCommentHandling.Skip,
3436
};
3537

3638
/// <summary>

Tests/Schema.NET.Test/ValuesJsonConverterTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,37 @@ public void ReadJson_ImplicitExternalTypes_AllowSharedNamespace()
754754
Assert.Equal(new[] { "My Test String" }, ((ExternalSchemaModelSharedNamespace)actual).MyCustomProperty);
755755
}
756756

757+
[Fact]
758+
public void ReadJson_Values_AllowComments()
759+
{
760+
var json = /*lang=json*/
761+
"""
762+
{
763+
"Property": [
764+
{
765+
"@context": "https://schema.org",
766+
"@type": "Book",
767+
"@id": "https://example.com/book/1",
768+
"name": "The Catcher in the Rye", // This is the book name
769+
"url": "https://www.barnesandnoble.com/store/info/offer/JDSalinger",
770+
"author": {
771+
"@type": "Person",
772+
"name": "J.D. Salinger" // Author name
773+
}
774+
}
775+
]
776+
}
777+
""";
778+
var result = DeserializeObject<Values<string, IBook>>(json);
779+
var actual = result.Value2.ToArray();
780+
781+
Assert.Equal(new Uri("https://example.com/book/1"), ((Book)actual[0]).Id);
782+
Assert.Equal("The Catcher in the Rye", actual[0].Name);
783+
Assert.Equal(new Uri("https://www.barnesandnoble.com/store/info/offer/JDSalinger"), (Uri)actual[0].Url!);
784+
var author1 = Assert.Single(actual[0].Author.Value2);
785+
Assert.Equal("J.D. Salinger", author1.Name);
786+
}
787+
757788
private static string SerializeObject<T>(T value)
758789
where T : struct, IValues
759790
=> SchemaSerializer.SerializeObject(new TestModel<T> { Property = value });

0 commit comments

Comments
 (0)