Adds Verify support for converting System.Text.Json types.
See Milestones for release notes.
Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.
[ModuleInitializer]
public static void Init() =>
VerifySystemJson.Initialize();
[Test]
public Task JsonDocumentSample()
{
var json =
"""
{
"short": {
"original": "http://www.foo.com/",
"short": "foo",
"error": {
"code": 0,
"msg": "No action taken"
}
}
}
""";
var document = JsonDocument.Parse(json);
return Verify(document);
}
Results in:
{
short: {
original: http://www.foo.com/,
short: foo,
error: {
code: 0,
msg: No action taken
}
}
}
Note that the above does not result in json files. This is by design. If json files are required then use UseStrictJson
This can be done at the Globally in a ModuleInitializer
[ModuleInitializer]
public static void Init() =>
VerifierSettings.UseStrictJson();
Or at the test level
[Test]
public Task JsonDocumentSample()
{
var json =
"""
{
"short": {
"original": "http://www.foo.com/",
"short": "foo",
"error": {
"code": 0,
"msg": "No action taken"
}
}
}
""";
var document = JsonDocument.Parse(json);
return Verify(document)
.UseStrictJson();
}
Results in:
{
"short": {
"original": "http://www.foo.com/",
"short": "foo",
"error": {
"code": 0,
"msg": "No action taken"
}
}
}
Values in the json can be ignored or scrubbed:
[Test]
public Task ScrubIgnoreMemberSample()
{
var json =
"""
{
"node": {
"original": "http://www.foo.com/",
"short": "foo",
"error": {
"code": 0,
"msg": "No action taken"
}
}
}
""";
var document = JsonDocument.Parse(json);
return Verify(document)
.ScrubMember("short")
.IgnoreMember("msg");
}
Results in:
{
node: {
original: http://www.foo.com/,
short: Scrubbed,
error: {
code: 0
}
}
}
Json values that map to known guid formats are scrubbed. See Guids scrubbing
[Test]
public Task GuidsSample()
{
var json =
"""
{
"node": {
"short": "foo",
"error": {
"guid": "123e4567-e89b-12d3-a456-426614174000",
"msg": "No action taken"
}
}
}
""";
var document = JsonDocument.Parse(json);
return Verify(document);
}
Results in:
{
node: {
short: foo,
error: {
guid: Guid_1,
msg: No action taken
}
}
}
Inline dates and Guids can be scrubbed:
[Test]
public Task InlineGuidsAndDatesSample()
{
var json =
"""
{
"node": {
"date": "2023/10/01",
"short": "foo 2023/10/01",
"error": {
"guid": "123e4567-e89b-12d3-a456-426614174000",
"msg": "No action taken 123e4567-e89b-12d3-a456-426614174000"
}
}
}
""";
var document = JsonDocument.Parse(json);
return Verify(document)
.ScrubInlineDates("yyyy/MM/dd")
.ScrubInlineGuids();
}
Results in:
{
node: {
date: Date_1,
short: foo Date_1,
error: {
guid: Guid_1,
msg: No action taken Guid_1
}
}
}
Inline date and Guids scrubbing can also be defined globally:
Pattern designed by Trevor Dsouza from The Noun Project.