using System.Text; using JsonConverter.Abstractions; using JsonConverter.Newtonsoft.Json; using WireMock.Admin.Mappings; namespace WireMock.Client.Extensions; /// /// ResponseModelBuilder /// public static class ResponseModelBuilderExtensions { private static readonly Encoding Utf8NoBom = new UTF8Encoding(false); private static readonly IJsonConverter JsonConverter = new NewtonsoftJsonConverter(); /// /// WithBodyAsJson /// /// The ResponseModelBuilder. /// The body. /// The body encoding. /// Define whether child objects to be indented. public static ResponseModelBuilder WithBodyAsJson(this ResponseModelBuilder builder, object body, Encoding? encoding = null, bool? indented = null) { return builder.WithBodyAsBytes(() => { var options = new JsonConverterOptions { WriteIndented = indented == true }; var jsonBody = JsonConverter.Serialize(body, options); return (encoding ?? Utf8NoBom).GetBytes(jsonBody); }); } /// /// WithBodyAsJson /// /// The ResponseModelBuilder. /// The body. /// Define whether child objects to be indented. public static ResponseModelBuilder WithBodyAsJson(this ResponseModelBuilder builder, object body, bool indented) { return builder.WithBodyAsJson(body, null, indented); } }