Skip to content

VerifyTests/Verify.SystemJson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify.SystemJson

Discussions Build status NuGet Status

Adds Verify support for converting System.Text.Json types.

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

NuGet

Usage

[ModuleInitializer]
public static void Init() =>
    VerifySystemJson.Initialize();

snippet source | anchor

JsonDocument

[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);
}

snippet source | anchor

Results in:

{
  short: {
    original: http://www.foo.com/,
    short: foo,
    error: {
      code: 0,
      msg: No action taken
    }
  }
}

snippet source | anchor

Strict Json

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();

snippet source | anchor

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();
}

snippet source | anchor

Results in:

{
  "short": {
    "original": "http://www.foo.com/",
    "short": "foo",
    "error": {
      "code": 0,
      "msg": "No action taken"
    }
  }
}

snippet source | anchor

Ignoring and Scrubbing

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");
}

snippet source | anchor

Results in:

{
  node: {
    original: http://www.foo.com/,
    short: Scrubbed,
    error: {
      code: 0
    }
  }
}

snippet source | anchor

Guid scrubbing

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);
}

snippet source | anchor

Results in:

{
  node: {
    short: foo,
    error: {
      guid: Guid_1,
      msg: No action taken
    }
  }
}

snippet source | anchor

Inline dates and Guids

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();
}

snippet source | anchor

Results in:

{
  node: {
    date: Date_1,
    short: foo Date_1,
    error: {
      guid: Guid_1,
      msg: No action taken Guid_1
    }
  }
}

snippet source | anchor

Inline date and Guids scrubbing can also be defined globally:

Icon

Pattern designed by Trevor Dsouza from The Noun Project.

About

Adds Verify support for converting System.Text.Json types

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors 5

Languages