Skip to main content

Customization

Describes the customization values of aweXpect.Json. They work like the customization of aweXpect.

ValueDefaultUsed to
DefaultJsonDocumentOptionsAllowTrailingCommas = trueparse JSON strings
DefaultJsonSerializerOptionsAllowTrailingCommas = trueformat JSON values and serialize the subject of IsJsonSerializedAs

You can change the default JsonDocumentOptions and the default JsonSerializerOptions. Each value is set on its own and restored when the returned lifetime is disposed:

using (Customize.aweXpect.Json().DefaultJsonDocumentOptions.Set(new JsonDocumentOptions
{
AllowTrailingCommas = false,
}))
{
// parses JSON strings without allowing trailing commas
}

To change a value for all tests, set it on Customize.aweXpect.Global.Json() instead, see global defaults.

To use other JsonDocumentOptions for a single expectation, pass them to IsValidJson(o => …) or, wherever JsonOptions are accepted, with WithJsonOptions(…):

string track = """{"title": "Let It Be", /* 1970 */ "artist": "The Beatles"}""";

await Expect.That(track).IsValidJsonMatching(new { title = "Let It Be" },
o => o.WithJsonOptions(d => d with { CommentHandling = JsonCommentHandling.Skip }));