mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-05-04 06:34:45 +02:00
.
This commit is contained in:
@@ -1,55 +1,31 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using JsonConverter.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER || NET461
|
||||
using System.Text.Json;
|
||||
#endif
|
||||
using JsonConverter.Abstractions.Models;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
internal class MappingSerializer(IJsonConverter jsonConverter)
|
||||
{
|
||||
private static readonly JsonConverterOptions JsonConverterOptions = new JsonConverterOptions
|
||||
{
|
||||
DateParseHandling = (int) DateParseHandling.None
|
||||
};
|
||||
|
||||
internal T[] DeserializeJsonToArray<T>(string value)
|
||||
{
|
||||
// DeserializeObject
|
||||
return DeserializeObjectToArray<T>(jsonConverter.Deserialize<object>(value, JsonConverterOptions)!);
|
||||
switch (JsonTypeHelper.GetJsonType(value))
|
||||
{
|
||||
case JsonType.Array:
|
||||
return jsonConverter.Deserialize<T[]>(value, JsonSerializationConstants.JsonConverterOptionsWithDateParsingNone)!;
|
||||
|
||||
case JsonType.Object:
|
||||
var singleResult = jsonConverter.Deserialize<T>(value, JsonSerializationConstants.JsonConverterOptionsWithDateParsingNone);
|
||||
return [singleResult!];
|
||||
|
||||
default:
|
||||
throw new InvalidOperationException("Cannot deserialize the provided value to an array or object.");
|
||||
}
|
||||
}
|
||||
|
||||
internal static T[] DeserializeObjectToArray<T>(object value)
|
||||
internal T[] DeserializeObjectToArray<T>(object value)
|
||||
{
|
||||
if (value is JArray jArray)
|
||||
{
|
||||
return jArray.ToObject<T[]>()!;
|
||||
}
|
||||
|
||||
if (value is JObject jObject)
|
||||
{
|
||||
var singleResult = jObject.ToObject<T>();
|
||||
return [singleResult!];
|
||||
}
|
||||
|
||||
#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER || NET461
|
||||
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.");
|
||||
var json = jsonConverter.Serialize(value, JsonSerializationConstants.JsonConverterOptionsWithDateParsingNone);
|
||||
return DeserializeJsonToArray<T>(json);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user