Skip to main content

Customization

Set a value under Customize.aweXpect.Json() for the current async flow, or under Customize.aweXpect.Global.Json() for all tests.

ValueDefaultSummary
DefaultJsonDocumentOptionsAllowTrailingCommas = truethe options to parse JSON strings
DefaultJsonSerializerOptionsAllowTrailingCommas = truethe options to format JSON values, serialize the subject of IsJsonSerializable and IsJsonSerializedAs and, once set, expected values

You can change the default JsonDocumentOptions and the default JsonSerializerOptions. Each value is set on its own:

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

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

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

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