This commit is contained in:
Stef Heyenrath
2026-04-19 11:05:55 +02:00
parent 65bf469906
commit 21cc70e77b
9 changed files with 46 additions and 64 deletions

View File

@@ -25,7 +25,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="JsonConverter.Abstractions" Version="0.9.0" /> <PackageReference Include="JsonConverter.Abstractions" Version="0.10.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<Description>A SystemTextJsonPathMatcher which can be used to match WireMock.Net Requests using JSONPath with System.Text.Json.</Description> <Description>A SystemTextJsonPathMatcher which can be used to match WireMock.Net Requests using JsonPath.Net.</Description>
<AssemblyTitle>WireMock.Net.Matchers.SystemTextJsonPath</AssemblyTitle> <AssemblyTitle>WireMock.Net.Matchers.SystemTextJsonPath</AssemblyTitle>
<Authors>Stef Heyenrath</Authors> <Authors>Stef Heyenrath</Authors>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks> <TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>

View File

@@ -1,55 +1,31 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
using JsonConverter.Abstractions; using JsonConverter.Abstractions;
using Newtonsoft.Json; using JsonConverter.Abstractions.Models;
using Newtonsoft.Json.Linq;
#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER || NET461
using System.Text.Json;
#endif
namespace WireMock.Serialization; namespace WireMock.Serialization;
internal class MappingSerializer(IJsonConverter jsonConverter) internal class MappingSerializer(IJsonConverter jsonConverter)
{ {
private static readonly JsonConverterOptions JsonConverterOptions = new JsonConverterOptions
{
DateParseHandling = (int) DateParseHandling.None
};
internal T[] DeserializeJsonToArray<T>(string value) internal T[] DeserializeJsonToArray<T>(string value)
{ {
// DeserializeObject switch (JsonTypeHelper.GetJsonType(value))
return DeserializeObjectToArray<T>(jsonConverter.Deserialize<object>(value, JsonConverterOptions)!); {
} case JsonType.Array:
return jsonConverter.Deserialize<T[]>(value, JsonSerializationConstants.JsonConverterOptionsWithDateParsingNone)!;
internal static T[] DeserializeObjectToArray<T>(object value) case JsonType.Object:
{ var singleResult = jsonConverter.Deserialize<T>(value, JsonSerializationConstants.JsonConverterOptionsWithDateParsingNone);
if (value is JArray jArray)
{
return jArray.ToObject<T[]>()!;
}
if (value is JObject jObject)
{
var singleResult = jObject.ToObject<T>();
return [singleResult!]; return [singleResult!];
}
#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER || NET461 default:
if (value is JsonElement jElement)
{
if (jElement.ValueKind == JsonValueKind.Array)
{
return jElement.Deserialize<T[]>()!;
}
if (jElement.ValueKind == JsonValueKind.Object)
{
var singleResult = jElement.Deserialize<T>();
return [singleResult!];
}
}
#endif
throw new InvalidOperationException("Cannot deserialize the provided value to an array or object."); throw new InvalidOperationException("Cannot deserialize the provided value to an array or object.");
} }
} }
internal T[] DeserializeObjectToArray<T>(object value)
{
var json = jsonConverter.Serialize(value, JsonSerializationConstants.JsonConverterOptionsWithDateParsingNone);
return DeserializeJsonToArray<T>(json);
}
}

View File

@@ -834,6 +834,18 @@ public partial class WireMockServer
}; };
} }
private T[] DeserializeRequestMessageToArray<T>(IRequestMessage requestMessage)
{
if (requestMessage.BodyData?.DetectedBodyType == BodyType.Json && requestMessage.BodyData.BodyAsJson != null)
{
var bodyAsJson = requestMessage.BodyData.BodyAsJson!;
return _mappingSerializer.DeserializeObjectToArray<T>(bodyAsJson);
}
throw new NotSupportedException();
}
private static Encoding? ToEncoding(EncodingModel? encodingModel) private static Encoding? ToEncoding(EncodingModel? encodingModel)
{ {
return encodingModel != null ? Encoding.GetEncoding(encodingModel.CodePage) : null; return encodingModel != null ? Encoding.GetEncoding(encodingModel.CodePage) : null;
@@ -853,18 +865,6 @@ public partial class WireMockServer
}; };
} }
private static T[] DeserializeRequestMessageToArray<T>(IRequestMessage requestMessage)
{
if (requestMessage.BodyData?.DetectedBodyType == BodyType.Json && requestMessage.BodyData.BodyAsJson != null)
{
var bodyAsJson = requestMessage.BodyData.BodyAsJson!;
return MappingSerializer.DeserializeObjectToArray<T>(bodyAsJson);
}
throw new NotSupportedException();
}
private static T DeserializeObject<T>(IRequestMessage requestMessage) private static T DeserializeObject<T>(IRequestMessage requestMessage)
{ {
switch (requestMessage.BodyData?.DetectedBodyType) switch (requestMessage.BodyData?.DetectedBodyType)

View File

@@ -25,7 +25,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.9.0" /> <PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.10.0" />
<PackageReference Include="NUnit" Version="4.4.0" /> <PackageReference Include="NUnit" Version="4.4.0" />
<PackageReference Include="Stef.Validation" Version="0.2.0" /> <PackageReference Include="Stef.Validation" Version="0.2.0" />
</ItemGroup> </ItemGroup>

View File

@@ -33,7 +33,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.9.0" /> <PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.10.0" />
<PackageReference Include="RestEase" Version="1.6.4" /> <PackageReference Include="RestEase" Version="1.6.4" />
<PackageReference Include="Stef.Validation" Version="0.2.0" /> <PackageReference Include="Stef.Validation" Version="0.2.0" />
</ItemGroup> </ItemGroup>

View File

@@ -14,11 +14,17 @@ internal static class JsonSerializationConstants
IgnoreNullValues = true IgnoreNullValues = true
}; };
//internal static readonly JsonSerializerSettings JsonSerializerSettingsDefault = new() internal static readonly JsonConverterOptions JsonConverterOptionsIncludeNullValues = new()
//{ {
// Formatting = Formatting.Indented, WriteIndented = true,
// NullValueHandling = NullValueHandling.Ignore IgnoreNullValues = false
//}; };
internal static readonly JsonConverterOptions JsonConverterOptionsWithDateParsingNone = new()
{
WriteIndented = true,
DateParseHandling = 0
};
internal static readonly JsonSerializerSettings JsonSerializerSettingsIncludeNullValues = new() internal static readonly JsonSerializerSettings JsonSerializerSettingsIncludeNullValues = new()
{ {

View File

@@ -30,7 +30,7 @@
<PackageReference Include="Stef.Validation" Version="0.2.0" /> <PackageReference Include="Stef.Validation" Version="0.2.0" />
<PackageReference Include="AnyOf" Version="0.5.0.1" /> <PackageReference Include="AnyOf" Version="0.5.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.9.0" /> <PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.10.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -76,7 +76,7 @@
</PackageReference> </PackageReference>
<PackageReference Include="Moq" Version="4.20.72" /> <PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="SimMetrics.Net" Version="1.0.5" /> <PackageReference Include="SimMetrics.Net" Version="1.0.5" />
<PackageReference Include="JsonConverter.System.Text.Json" Version="0.8.0" /> <PackageReference Include="JsonConverter.System.Text.Json" Version="0.10.0" />
<PackageReference Include="Google.Protobuf" Version="3.33.5" /> <PackageReference Include="Google.Protobuf" Version="3.33.5" />
<PackageReference Include="Grpc.Net.Client" Version="2.76.0" /> <PackageReference Include="Grpc.Net.Client" Version="2.76.0" />
<PackageReference Include="Grpc.Tools" Version="2.78.0"> <PackageReference Include="Grpc.Tools" Version="2.78.0">