From 9277315ef9a57bb1ce5c4c5def003237877fb15e Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Thu, 30 Apr 2026 22:51:47 +0200 Subject: [PATCH] delete jsonutils.cs --- .../Assertions/WireMockAssertions.WithBody.cs | 55 ++++++++++---- .../Assertions/WireMockAssertions.WithBody.cs | 55 ++++++++++---- .../Serialization/PactMapper.cs | 24 +++++- .../Serialization/SwaggerMapper.cs | 38 +++++++++- .../WireMockAdminApiAssertions.WithBody.cs | 53 ++++++++++--- .../JsonSerializationConstants.cs | 6 -- src/WireMock.Net.Shared/Util/JsonUtils.cs | 74 ------------------- 7 files changed, 183 insertions(+), 122 deletions(-) delete mode 100644 src/WireMock.Net.Shared/Util/JsonUtils.cs diff --git a/src/WireMock.Net.AwesomeAssertions/Assertions/WireMockAssertions.WithBody.cs b/src/WireMock.Net.AwesomeAssertions/Assertions/WireMockAssertions.WithBody.cs index e4b7aa06..70d12a64 100644 --- a/src/WireMock.Net.AwesomeAssertions/Assertions/WireMockAssertions.WithBody.cs +++ b/src/WireMock.Net.AwesomeAssertions/Assertions/WireMockAssertions.WithBody.cs @@ -31,19 +31,19 @@ public partial class WireMockAssertions } [CustomAssertion] - public AndConstraint WithBodyAsJson(object body, string because = "", params object[] becauseArgs) + public AndConstraint WithBodyAsJson(object body, IJsonMatcher? jsonMatcher = null, string because = "", params object[] becauseArgs) { - return WithBodyAsJson(new JsonMatcher(body), because, becauseArgs); + return WithBodyAsJson(jsonMatcher ?? new JsonMatcher(body), because, becauseArgs); } [CustomAssertion] - public AndConstraint WithBodyAsJson(string body, string because = "", params object[] becauseArgs) + public AndConstraint WithBodyAsJson(string body, IJsonMatcher? jsonMatcher = null, string because = "", params object[] becauseArgs) { - return WithBodyAsJson(new JsonMatcher(body), because, becauseArgs); + return WithBodyAsJson(jsonMatcher ?? new JsonMatcher(body), because, becauseArgs); } [CustomAssertion] - public AndConstraint WithBodyAsJson(IObjectMatcher matcher, string because = "", params object[] becauseArgs) + public AndConstraint WithBodyAsJson(IJsonMatcher matcher, string because = "", params object[] becauseArgs) { var (filter, condition) = BuildFilterAndCondition(r => r.BodyAsJson, matcher); @@ -126,15 +126,44 @@ public partial class WireMockAssertions private static string? FormatBody(object? body) { - return body switch + if (body == null) { - null => null, - string str => str, - AnyOf[] stringPatterns => FormatBodies(stringPatterns.Select(p => p.GetPattern())), - byte[] bytes => $"byte[{bytes.Length}] {{...}}", - JToken jToken => jToken.ToString(Formatting.None), - _ => JToken.FromObject(body).ToString(Formatting.None) - }; + return null; + } + + if (body is string str) + { + return str; + } + + if (body is AnyOf[] stringPatterns) + { + return FormatBodies(stringPatterns.Select(p => p.GetPattern())); + } + + if (body is byte[] bytes) + { + return $"byte[{bytes.Length}] {{...}}"; + } + + if (body is JToken jToken) + { + return jToken.ToString(Formatting.None); + } + + // System.IO.FileNotFoundException : Could not load file or assembly 'System.Text.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. + var typeName = body.GetType().FullName; + if (typeName == "System.Text.Json.JsonElement") + { + return ((dynamic)body).GetRawText(); + } + + if (typeName == "System.Text.Json.JsonDocument") + { + return ((dynamic)body).RootElement.GetRawText(); + } + + return JToken.FromObject(body).ToString(Formatting.None); } private static string? FormatBodies(IEnumerable bodies) diff --git a/src/WireMock.Net.FluentAssertions/Assertions/WireMockAssertions.WithBody.cs b/src/WireMock.Net.FluentAssertions/Assertions/WireMockAssertions.WithBody.cs index a7b06ff8..19e48283 100644 --- a/src/WireMock.Net.FluentAssertions/Assertions/WireMockAssertions.WithBody.cs +++ b/src/WireMock.Net.FluentAssertions/Assertions/WireMockAssertions.WithBody.cs @@ -31,19 +31,19 @@ public partial class WireMockAssertions } [CustomAssertion] - public AndConstraint WithBodyAsJson(object body, string because = "", params object[] becauseArgs) + public AndConstraint WithBodyAsJson(object body, IJsonMatcher? jsonMatcher = null, string because = "", params object[] becauseArgs) { - return WithBodyAsJson(new JsonMatcher(body), because, becauseArgs); + return WithBodyAsJson(jsonMatcher ?? new JsonMatcher(body), because, becauseArgs); } [CustomAssertion] - public AndConstraint WithBodyAsJson(string body, string because = "", params object[] becauseArgs) + public AndConstraint WithBodyAsJson(string body, IJsonMatcher? jsonMatcher = null, string because = "", params object[] becauseArgs) { - return WithBodyAsJson(new JsonMatcher(body), because, becauseArgs); + return WithBodyAsJson(jsonMatcher ?? new JsonMatcher(body), because, becauseArgs); } [CustomAssertion] - public AndConstraint WithBodyAsJson(IObjectMatcher matcher, string because = "", params object[] becauseArgs) + public AndConstraint WithBodyAsJson(IJsonMatcher matcher, string because = "", params object[] becauseArgs) { var (filter, condition) = BuildFilterAndCondition(r => r.BodyAsJson, matcher); @@ -126,15 +126,44 @@ public partial class WireMockAssertions private static string? FormatBody(object? body) { - return body switch + if (body == null) { - null => null, - string str => str, - AnyOf[] stringPatterns => FormatBodies(stringPatterns.Select(p => p.GetPattern())), - byte[] bytes => $"byte[{bytes.Length}] {{...}}", - JToken jToken => jToken.ToString(Formatting.None), - _ => JToken.FromObject(body).ToString(Formatting.None) - }; + return null; + } + + if (body is string str) + { + return str; + } + + if (body is AnyOf[] stringPatterns) + { + return FormatBodies(stringPatterns.Select(p => p.GetPattern())); + } + + if (body is byte[] bytes) + { + return $"byte[{bytes.Length}] {{...}}"; + } + + if (body is JToken jToken) + { + return jToken.ToString(Formatting.None); + } + + // System.IO.FileNotFoundException : Could not load file or assembly 'System.Text.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. + var typeName = body.GetType().FullName; + if (typeName == "System.Text.Json.JsonElement") + { + return ((dynamic)body).GetRawText(); + } + + if (typeName == "System.Text.Json.JsonDocument") + { + return ((dynamic)body).RootElement.GetRawText(); + } + + return JToken.FromObject(body).ToString(Formatting.None); } private static string? FormatBodies(IEnumerable bodies) diff --git a/src/WireMock.Net.Minimal/Serialization/PactMapper.cs b/src/WireMock.Net.Minimal/Serialization/PactMapper.cs index eae41935..ec7266f5 100644 --- a/src/WireMock.Net.Minimal/Serialization/PactMapper.cs +++ b/src/WireMock.Net.Minimal/Serialization/PactMapper.cs @@ -1,6 +1,8 @@ // Copyright © WireMock.Net using System.Linq; +using System.Text; +using Newtonsoft.Json; using WireMock.Admin.Mappings; using WireMock.Extensions; using WireMock.Pact.Models.V2; @@ -49,7 +51,7 @@ internal static class PactMapper pact.Interactions.Add(interaction); } - return (filename, JsonUtils.SerializeAsPactFile(pact)); + return (filename, SerializeAsPactFile(pact)); } private static PactRequest MapRequest(RequestModel request, string path) @@ -152,7 +154,7 @@ internal static class PactMapper /// private static object? TryDeserializeJsonStringAsObject(string? value) { - return value != null ? JsonUtils.TryDeserializeObject(value) ?? value : null; + return value != null ? TryDeserializeObject(value) ?? value : null; } //private static string GetPatternAsStringFromMatchers(MatcherModel[]? matchers, string defaultValue) @@ -164,4 +166,22 @@ internal static class PactMapper // return defaultValue; //} + + private static byte[] SerializeAsPactFile(object value) + { + var json = JsonConvert.SerializeObject(value, JsonSerializationConstants.JsonSerializerSettingsPact); + return Encoding.UTF8.GetBytes(json); + } + + private static T? TryDeserializeObject(string json) + { + try + { + return JsonConvert.DeserializeObject(json); + } + catch + { + return default; + } + } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Serialization/SwaggerMapper.cs b/src/WireMock.Net.Minimal/Serialization/SwaggerMapper.cs index 7116171a..58873c0e 100644 --- a/src/WireMock.Net.Minimal/Serialization/SwaggerMapper.cs +++ b/src/WireMock.Net.Minimal/Serialization/SwaggerMapper.cs @@ -1,7 +1,8 @@ // Copyright © WireMock.Net -using System.Linq; +using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using NJsonSchema; using NJsonSchema.Extensions; using NSwag; @@ -281,7 +282,7 @@ internal static class SwaggerMapper if (matcher is { Name: nameof(JsonMatcher) }) { var pattern = GetPatternAsStringFromMatcher(matcher); - if (JsonUtils.TryParseAsJObject(pattern, out var jObject)) + if (TryParseAsJObject(pattern, out var jObject)) { return jObject; } @@ -292,6 +293,39 @@ internal static class SwaggerMapper return null; } + private static bool IsJson(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return false; + } + + value = value!.Trim(); + + return (value.StartsWith("{") && value.EndsWith("}")) || (value.StartsWith("[") && value.EndsWith("]")); + } + + private static bool TryParseAsJObject(string? strInput, [NotNullWhen(true)] out JObject? value) + { + value = null; + + if (!IsJson(strInput)) + { + return false; + } + + try + { + // Try to convert this string into a JObject + value = JObject.Parse(strInput!); + return true; + } + catch + { + return false; + } + } + private static string GetContentType(RequestModel request) { var contentType = request.Headers?.FirstOrDefault(h => h.Name == "Content-Type"); diff --git a/src/WireMock.Net.RestClient.AwesomeAssertions/Assertions/WireMockAdminApiAssertions.WithBody.cs b/src/WireMock.Net.RestClient.AwesomeAssertions/Assertions/WireMockAdminApiAssertions.WithBody.cs index b01a305f..35f6930d 100644 --- a/src/WireMock.Net.RestClient.AwesomeAssertions/Assertions/WireMockAdminApiAssertions.WithBody.cs +++ b/src/WireMock.Net.RestClient.AwesomeAssertions/Assertions/WireMockAdminApiAssertions.WithBody.cs @@ -32,15 +32,15 @@ public partial class WireMockAdminApiAssertions } [CustomAssertion] - public AndConstraint WithBodyAsJson(object body, string because = "", params object[] becauseArgs) + public AndConstraint WithBodyAsJson(object body, IJsonMatcher? jsonMatcher = null, string because = "", params object[] becauseArgs) { - return WithBodyAsJson(new JsonMatcher(body), because, becauseArgs); + return WithBodyAsJson(jsonMatcher ?? new JsonMatcher(body), because, becauseArgs); } [CustomAssertion] - public AndConstraint WithBodyAsJson(string body, string because = "", params object[] becauseArgs) + public AndConstraint WithBodyAsJson(string body, IJsonMatcher? jsonMatcher = null, string because = "", params object[] becauseArgs) { - return WithBodyAsJson(new JsonMatcher(body), because, becauseArgs); + return WithBodyAsJson(jsonMatcher ?? new JsonMatcher(body), because, becauseArgs); } [CustomAssertion] @@ -127,15 +127,44 @@ public partial class WireMockAdminApiAssertions private static string? FormatBody(object? body) { - return body switch + if (body == null) { - null => null, - string str => str, - AnyOf[] stringPatterns => FormatBodies(stringPatterns.Select(p => p.GetPattern())), - byte[] bytes => $"byte[{bytes.Length}] {{...}}", - JToken jToken => jToken.ToString(Formatting.None), - _ => JToken.FromObject(body).ToString(Formatting.None) - }; + return null; + } + + if (body is string str) + { + return str; + } + + if (body is AnyOf[] stringPatterns) + { + return FormatBodies(stringPatterns.Select(p => p.GetPattern())); + } + + if (body is byte[] bytes) + { + return $"byte[{bytes.Length}] {{...}}"; + } + + if (body is JToken jToken) + { + return jToken.ToString(Formatting.None); + } + + // System.IO.FileNotFoundException : Could not load file or assembly 'System.Text.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. + var typeName = body.GetType().FullName; + if (typeName == "System.Text.Json.JsonElement") + { + return ((dynamic)body).GetRawText(); + } + + if (typeName == "System.Text.Json.JsonDocument") + { + return ((dynamic)body).RootElement.GetRawText(); + } + + return JToken.FromObject(body).ToString(Formatting.None); } private static string? FormatBodies(IEnumerable bodies) diff --git a/src/WireMock.Net.Shared/Serialization/JsonSerializationConstants.cs b/src/WireMock.Net.Shared/Serialization/JsonSerializationConstants.cs index c0f8aa65..892a3ef9 100644 --- a/src/WireMock.Net.Shared/Serialization/JsonSerializationConstants.cs +++ b/src/WireMock.Net.Shared/Serialization/JsonSerializationConstants.cs @@ -26,12 +26,6 @@ internal static class JsonSerializationConstants DateParseHandling = 0 }; - internal static readonly JsonSerializerSettings JsonSerializerSettingsIncludeNullValues = new() - { - Formatting = Formatting.Indented, - NullValueHandling = NullValueHandling.Include - }; - internal static readonly JsonSerializerSettings JsonDeserializerSettingsWithDateParsingNone = new() { DateParseHandling = DateParseHandling.None diff --git a/src/WireMock.Net.Shared/Util/JsonUtils.cs b/src/WireMock.Net.Shared/Util/JsonUtils.cs deleted file mode 100644 index 6d4aea7c..00000000 --- a/src/WireMock.Net.Shared/Util/JsonUtils.cs +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright © WireMock.Net - -using System.Diagnostics.CodeAnalysis; -using System.Text; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using WireMock.Serialization; - -namespace WireMock.Util; - -internal static class JsonUtils -{ - public static bool IsJson(string? value) - { - if (string.IsNullOrWhiteSpace(value)) - { - return false; - } - - value = value!.Trim(); - - return (value.StartsWith("{") && value.EndsWith("}")) || (value.StartsWith("[") && value.EndsWith("]")); - } - - public static bool TryParseAsJObject(string? strInput, [NotNullWhen(true)] out JObject? value) - { - value = null; - - if (!IsJson(strInput)) - { - return false; - } - - try - { - // Try to convert this string into a JObject - value = JObject.Parse(strInput!); - return true; - } - catch - { - return false; - } - } - - public static byte[] SerializeAsPactFile(object value) - { - var json = JsonConvert.SerializeObject(value, JsonSerializationConstants.JsonSerializerSettingsPact); - return Encoding.UTF8.GetBytes(json); - } - - /// - /// Deserializes the JSON to a .NET object. - /// Using : DateParseHandling = DateParseHandling.None - /// - /// A System.String that contains JSON. - /// The deserialized object from the JSON string. - public static object DeserializeObject(string json) - { - return JsonConvert.DeserializeObject(json, JsonSerializationConstants.JsonDeserializerSettingsWithDateParsingNone)!; - } - - public static T? TryDeserializeObject(string json) - { - try - { - return JsonConvert.DeserializeObject(json); - } - catch - { - return default; - } - } -} \ No newline at end of file