diff --git a/AFVMR.sln b/AFVMR.sln index 8ce4ec3..959a3a6 100644 --- a/AFVMR.sln +++ b/AFVMR.sln @@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rocket.Surgery.Operational. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rocket.Surgery.Operational.AspNetCore.MediatR", "src\Operational.MediatR.AspNetCore\Rocket.Surgery.Operational.AspNetCore.MediatR.csproj", "{6CD8BEB5-B592-4B4C-8EE8-B5CEF00B6BC2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rocket.Surgery.Operational.MediatR.NewtonsoftJson", "src\Operational.MediatR.NewtonsoftJson\Rocket.Surgery.Operational.MediatR.NewtonsoftJson.csproj", "{FAB16AD1-9607-4BAC-84DE-18715FAE61C1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -67,6 +69,18 @@ Global {6CD8BEB5-B592-4B4C-8EE8-B5CEF00B6BC2}.Release|x64.Build.0 = Release|Any CPU {6CD8BEB5-B592-4B4C-8EE8-B5CEF00B6BC2}.Release|x86.ActiveCfg = Release|Any CPU {6CD8BEB5-B592-4B4C-8EE8-B5CEF00B6BC2}.Release|x86.Build.0 = Release|Any CPU + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1}.Debug|x64.ActiveCfg = Debug|Any CPU + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1}.Debug|x64.Build.0 = Debug|Any CPU + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1}.Debug|x86.ActiveCfg = Debug|Any CPU + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1}.Debug|x86.Build.0 = Debug|Any CPU + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1}.Release|Any CPU.Build.0 = Release|Any CPU + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1}.Release|x64.ActiveCfg = Release|Any CPU + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1}.Release|x64.Build.0 = Release|Any CPU + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1}.Release|x86.ActiveCfg = Release|Any CPU + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -75,6 +89,7 @@ Global {55EFAE2F-A7CA-4917-A381-4A6EF5E79610} = {E3DCB78E-EE06-4EA3-88AC-762D8910387C} {6EE2D4B5-A8C4-4BC9-8E5A-D6DF67041B4D} = {8FFDF555-DB50-45F9-9A2D-6410F39151C3} {6CD8BEB5-B592-4B4C-8EE8-B5CEF00B6BC2} = {8FFDF555-DB50-45F9-9A2D-6410F39151C3} + {FAB16AD1-9607-4BAC-84DE-18715FAE61C1} = {8FFDF555-DB50-45F9-9A2D-6410F39151C3} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {439897C2-CCBD-44FE-B2DC-A3E4670ADA59} diff --git a/GitVersion.yml b/GitVersion.yml index 3764d3a..ba50bef 100644 --- a/GitVersion.yml +++ b/GitVersion.yml @@ -1,7 +1,7 @@ assembly-versioning-scheme: MajorMinorPatch mode: ContinuousDeployment continuous-delivery-fallback-tag: beta -next-version: 1.0.0 +next-version: 8.0.0 branches: {} ignore: sha: [] diff --git a/src/Operational.MediatR.NewtonsoftJson/JTokenConverter.cs b/src/Operational.MediatR.NewtonsoftJson/JTokenConverter.cs new file mode 100644 index 0000000..b845eb1 --- /dev/null +++ b/src/Operational.MediatR.NewtonsoftJson/JTokenConverter.cs @@ -0,0 +1,158 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Text; +using AutoMapper; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace Rocket.Surgery.Operational.MediatR.NewtonsoftJson +{ + [ExcludeFromCodeCoverage] + class JTokenConverter : + ITypeConverter, + ITypeConverter, + ITypeConverter, + ITypeConverter, + ITypeConverter, + ITypeConverter, + ITypeConverter, + ITypeConverter, + ITypeConverter, + ITypeConverter, + ITypeConverter, + ITypeConverter, + ITypeConverter, + ITypeConverter, + ITypeConverter + { + public byte[]? Convert(JToken? source, byte[]? destination, ResolutionContext context) + { + if (source == null || source.Type == JTokenType.None) + return destination ?? Array.Empty(); + return WriteToBytes(source); + } + + + public JToken? Convert(byte[]? source, JToken? destination, ResolutionContext context) + { + try + { + return source == null || source.Length == 0 + ? destination + : JToken.Parse(Encoding.UTF8.GetString(source)); + } + catch (JsonReaderException) + { + return destination; + } + } + + + public string? Convert(JToken? source, string? destination, ResolutionContext context) + => source?.ToString(Formatting.None) ?? destination; + + + public JToken? Convert(string? source, JToken? destination, ResolutionContext context) + { + try + { + return string.IsNullOrEmpty(source) ? destination : JToken.Parse(source); + } + catch (JsonReaderException) + { + return destination; + } + } + + public byte[]? Convert(JArray? source, byte[]? destination, ResolutionContext context) + { + if (source == null || source.Type == JTokenType.None) + return destination ?? Array.Empty(); + return WriteToBytes(source); + } + + public JArray? Convert(byte[]? source, JArray? destination, ResolutionContext context) + { + try + { + return source == null || source.Length == 0 + ? destination ?? new JArray() + : JArray.Parse(Encoding.UTF8.GetString(source)); + } + catch (JsonReaderException) + { + return destination ?? new JArray(); + } + } + + public string? Convert(JArray? source, string? destination, ResolutionContext context) + => source?.ToString(Formatting.None) ?? destination; + + public JArray? Convert(string? source, JArray? destination, ResolutionContext context) + { + try + { + return string.IsNullOrEmpty(source) ? destination ?? new JArray() : JArray.Parse(source); + } + catch (JsonReaderException) + { + return destination ?? new JArray(); + } + } + + public byte[]? Convert(JObject? source, byte[]? destination, ResolutionContext context) + { + if (source == null || source.Type == JTokenType.None) + return destination ?? Array.Empty(); + return WriteToBytes(source); + } + + public JObject? Convert(byte[]? source, JObject? destination, ResolutionContext context) + { + try + { + return source == null || source.Length == 0 + ? destination ?? new JObject() + : JObject.Parse(Encoding.UTF8.GetString(source)); + } + catch (JsonReaderException) + { + return destination ?? new JObject(); + } + } + + public string? Convert(JObject? source, string? destination, ResolutionContext context) + => source?.ToString(Formatting.None) ?? destination; + + public JObject? Convert(string? source, JObject? destination, ResolutionContext context) + { + try + { + return string.IsNullOrEmpty(source) ? destination ?? new JObject() : JObject.Parse(source); + } + catch (JsonReaderException) + { + return destination ?? new JObject(); + } + } + + public JToken? Convert(JToken? source, JToken? destination, ResolutionContext context) => source ?? destination; + + public JArray? Convert(JArray? source, JArray? destination, ResolutionContext context) => source ?? destination; + + public JObject? Convert(JObject? source, JObject? destination, ResolutionContext context) + => source ?? destination; + + private byte[] WriteToBytes(JToken source) + { + var memory = new MemoryStream(); + using var sw = new StreamWriter(memory); + var jw = new JsonTextWriter(sw) { Formatting = Formatting.None }; + source.WriteTo(jw); + jw.Flush(); + memory.Position = 0; + return memory.ToArray(); + } + } +} \ No newline at end of file diff --git a/src/Operational.MediatR.NewtonsoftJson/NewtonsoftJsonMapper.cs b/src/Operational.MediatR.NewtonsoftJson/NewtonsoftJsonMapper.cs new file mode 100644 index 0000000..32bb658 --- /dev/null +++ b/src/Operational.MediatR.NewtonsoftJson/NewtonsoftJsonMapper.cs @@ -0,0 +1,28 @@ +using AutoMapper; +using Newtonsoft.Json.Linq; + +namespace Rocket.Surgery.Operational.MediatR.NewtonsoftJson +{ + class NewtonsoftJsonMapper : Profile + { + public NewtonsoftJsonMapper() + { + var converter = new JTokenConverter(); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + } + } +} \ No newline at end of file diff --git a/src/Operational.MediatR.NewtonsoftJson/Rocket.Surgery.Operational.MediatR.NewtonsoftJson.csproj b/src/Operational.MediatR.NewtonsoftJson/Rocket.Surgery.Operational.MediatR.NewtonsoftJson.csproj new file mode 100644 index 0000000..c110501 --- /dev/null +++ b/src/Operational.MediatR.NewtonsoftJson/Rocket.Surgery.Operational.MediatR.NewtonsoftJson.csproj @@ -0,0 +1,10 @@ + + + netstandard2.1;netstandard2.0 + Rocket.Surgery FluentValidation with MediatR for .NET Core + + + + + + diff --git a/src/Operational.MediatR/JsonElementConverter.cs b/src/Operational.MediatR/JsonElementConverter.cs new file mode 100644 index 0000000..1e3b910 --- /dev/null +++ b/src/Operational.MediatR/JsonElementConverter.cs @@ -0,0 +1,56 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; +using AutoMapper; + +namespace Rocket.Surgery.Operational.MediatR { + [ExcludeFromCodeCoverage] + class JsonElementConverter : + ITypeConverter, ITypeConverter, + ITypeConverter, ITypeConverter, + ITypeConverter, ITypeConverter, + ITypeConverter, ITypeConverter + { + private static readonly JsonElement _empty = JsonSerializer.Deserialize("null"); + + public byte[]? Convert(JsonElement source, byte[]? destination, ResolutionContext context) => + source.ValueKind == JsonValueKind.Undefined + ? destination ?? Array.Empty() + : JsonSerializer.SerializeToUtf8Bytes(source); + + public JsonElement Convert(byte[]? source, JsonElement destination, ResolutionContext context) => + source == null || source.Length == 0 + ? destination.ValueKind == JsonValueKind.Undefined ? destination : _empty + : JsonSerializer.Deserialize(source); + + public string? Convert(JsonElement source, string? destination, ResolutionContext context) => + source.ValueKind == JsonValueKind.Undefined + ? destination ?? string.Empty + : JsonSerializer.Serialize(source); + + public JsonElement Convert(string? source, JsonElement destination, ResolutionContext context) => + string.IsNullOrEmpty(source) + ? destination.ValueKind == JsonValueKind.Undefined ? destination : _empty + : JsonSerializer.Deserialize(source); + + public byte[]? Convert(JsonElement? source, byte[]? destination, ResolutionContext context) => + !source.HasValue || source.Value.ValueKind == JsonValueKind.Undefined + ? destination ?? Array.Empty() + : JsonSerializer.SerializeToUtf8Bytes(source); + + public JsonElement? Convert(byte[]? source, JsonElement? destination, ResolutionContext context) => + source == null || source.Length == 0 + ? destination?.ValueKind == JsonValueKind.Undefined ? destination : _empty + : JsonSerializer.Deserialize(source); + + public string? Convert(JsonElement? source, string? destination, ResolutionContext context) => + !source.HasValue || source.Value.ValueKind == JsonValueKind.Undefined + ? string.Empty + : JsonSerializer.Serialize(source); + + public JsonElement? Convert(string? source, JsonElement? destination, ResolutionContext context) => + string.IsNullOrEmpty(source) + ? destination?.ValueKind == JsonValueKind.Undefined ? destination : _empty + : JsonSerializer.Deserialize(source); + } +} \ No newline at end of file diff --git a/src/Operational.MediatR/MediatorROperationsConvention.cs b/src/Operational.MediatR/MediatorROperationsConvention.cs index aa91c45..6990dbd 100644 --- a/src/Operational.MediatR/MediatorROperationsConvention.cs +++ b/src/Operational.MediatR/MediatorROperationsConvention.cs @@ -1,4 +1,5 @@ using System; +using System.Xml; using JetBrains.Annotations; using MediatR; using Microsoft.Extensions.DependencyInjection; diff --git a/src/Operational.MediatR/SystemJsonTextMapper.cs b/src/Operational.MediatR/SystemJsonTextMapper.cs new file mode 100644 index 0000000..6dbff73 --- /dev/null +++ b/src/Operational.MediatR/SystemJsonTextMapper.cs @@ -0,0 +1,21 @@ +using System.Text.Json; +using AutoMapper; + +namespace Rocket.Surgery.Operational.MediatR +{ + class SystemJsonTextMapper : Profile + { + public SystemJsonTextMapper() + { + var converter = new JsonElementConverter(); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + CreateMap().ConvertUsing(converter); + } + } +} \ No newline at end of file