Skip to content

Commit

Permalink
Support JSON comments (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
Turnerj authored May 10, 2023
1 parent 5c01227 commit a183ff7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Source/Common/SchemaSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static class SchemaSerializer
AllowTrailingCommas = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
ReadCommentHandling = JsonCommentHandling.Skip,
};

/// <summary>
Expand All @@ -31,6 +32,7 @@ public static class SchemaSerializer
{
AllowTrailingCommas = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
ReadCommentHandling = JsonCommentHandling.Skip,
};

/// <summary>
Expand Down
31 changes: 31 additions & 0 deletions Tests/Schema.NET.Test/ValuesJsonConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,37 @@ public void ReadJson_ImplicitExternalTypes_AllowSharedNamespace()
Assert.Equal(new[] { "My Test String" }, ((ExternalSchemaModelSharedNamespace)actual).MyCustomProperty);
}

[Fact]
public void ReadJson_Values_AllowComments()
{
var json = /*lang=json*/
"""
{
"Property": [
{
"@context": "https://schema.org",
"@type": "Book",
"@id": "https://example.com/book/1",
"name": "The Catcher in the Rye", // This is the book name
"url": "https://www.barnesandnoble.com/store/info/offer/JDSalinger",
"author": {
"@type": "Person",
"name": "J.D. Salinger" // Author name
}
}
]
}
""";
var result = DeserializeObject<Values<string, IBook>>(json);
var actual = result.Value2.ToArray();

Assert.Equal(new Uri("https://example.com/book/1"), ((Book)actual[0]).Id);
Assert.Equal("The Catcher in the Rye", actual[0].Name);
Assert.Equal(new Uri("https://www.barnesandnoble.com/store/info/offer/JDSalinger"), (Uri)actual[0].Url!);
var author1 = Assert.Single(actual[0].Author.Value2);
Assert.Equal("J.D. Salinger", author1.Name);
}

private static string SerializeObject<T>(T value)
where T : struct, IValues
=> SchemaSerializer.SerializeObject(new TestModel<T> { Property = value });
Expand Down

0 comments on commit a183ff7

Please sign in to comment.