Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Updated to support automapper base mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll committed Jan 14, 2020
1 parent 970974a commit f21bc20
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 1 deletion.
15 changes: 15 additions & 0 deletions AFVMR.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion GitVersion.yml
Original file line number Diff line number Diff line change
@@ -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: []
158 changes: 158 additions & 0 deletions src/Operational.MediatR.NewtonsoftJson/JTokenConverter.cs
Original file line number Diff line number Diff line change
@@ -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<JToken, byte[]?>,
ITypeConverter<byte[]?, JToken?>,
ITypeConverter<JToken?, string?>,
ITypeConverter<string?, JToken?>,
ITypeConverter<JToken?, JToken?>,
ITypeConverter<JArray?, byte[]?>,
ITypeConverter<byte[]?, JArray?>,
ITypeConverter<JArray?, string?>,
ITypeConverter<string?, JArray?>,
ITypeConverter<JArray?, JArray?>,
ITypeConverter<JObject?, byte[]?>,
ITypeConverter<byte[]?, JObject?>,
ITypeConverter<JObject?, string?>,
ITypeConverter<string?, JObject?>,
ITypeConverter<JObject?, JObject?>
{
public byte[]? Convert(JToken? source, byte[]? destination, ResolutionContext context)
{
if (source == null || source.Type == JTokenType.None)
return destination ?? Array.Empty<byte>();
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<byte>();
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<byte>();
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();
}
}
}
28 changes: 28 additions & 0 deletions src/Operational.MediatR.NewtonsoftJson/NewtonsoftJsonMapper.cs
Original file line number Diff line number Diff line change
@@ -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<JArray?, byte[]?>().ConvertUsing(converter);
CreateMap<JArray?, string?>().ConvertUsing(converter);
CreateMap<byte[]?, JArray?>().ConvertUsing(converter);
CreateMap<string?, JArray?>().ConvertUsing(converter);
CreateMap<JObject?, byte[]?>().ConvertUsing(converter);
CreateMap<JObject?, string?>().ConvertUsing(converter);
CreateMap<byte[]?, JObject?>().ConvertUsing(converter);
CreateMap<string?, JObject?>().ConvertUsing(converter);
CreateMap<JToken?, byte[]?>().ConvertUsing(converter);
CreateMap<JToken?, string?>().ConvertUsing(converter);
CreateMap<byte[]?, JToken?>().ConvertUsing(converter);
CreateMap<string?, JToken?>().ConvertUsing(converter);
CreateMap<JObject?, JObject?>().ConvertUsing(converter);
CreateMap<JArray?, JArray?>().ConvertUsing(converter);
CreateMap<JToken?, JToken?>().ConvertUsing(converter);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
<AssemblyTitle>Rocket.Surgery FluentValidation with MediatR for .NET Core</AssemblyTitle>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../Operational.MediatR/Rocket.Surgery.Operational.MediatR.csproj" />
<PackageReference Include="NodaTime.Serialization.JsonNet" />
</ItemGroup>
</Project>
56 changes: 56 additions & 0 deletions src/Operational.MediatR/JsonElementConverter.cs
Original file line number Diff line number Diff line change
@@ -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<JsonElement, byte[]?>, ITypeConverter<byte[]?, JsonElement>,
ITypeConverter<JsonElement, string?>, ITypeConverter<string?, JsonElement>,
ITypeConverter<JsonElement?, byte[]?>, ITypeConverter<byte[]?, JsonElement?>,
ITypeConverter<JsonElement?, string?>, ITypeConverter<string?, JsonElement?>
{
private static readonly JsonElement _empty = JsonSerializer.Deserialize<JsonElement>("null");

public byte[]? Convert(JsonElement source, byte[]? destination, ResolutionContext context) =>
source.ValueKind == JsonValueKind.Undefined
? destination ?? Array.Empty<byte>()
: 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<JsonElement>(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<JsonElement>(source);

public byte[]? Convert(JsonElement? source, byte[]? destination, ResolutionContext context) =>
!source.HasValue || source.Value.ValueKind == JsonValueKind.Undefined
? destination ?? Array.Empty<byte>()
: 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<JsonElement>(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<JsonElement>(source);
}
}
1 change: 1 addition & 0 deletions src/Operational.MediatR/MediatorROperationsConvention.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Xml;
using JetBrains.Annotations;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
Expand Down
21 changes: 21 additions & 0 deletions src/Operational.MediatR/SystemJsonTextMapper.cs
Original file line number Diff line number Diff line change
@@ -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<JsonElement, byte[]?>().ConvertUsing(converter);
CreateMap<JsonElement, string?>().ConvertUsing(converter);
CreateMap<JsonElement?, byte[]?>().ConvertUsing(converter);
CreateMap<JsonElement?, string?>().ConvertUsing(converter);
CreateMap<byte[]?, JsonElement>().ConvertUsing(converter);
CreateMap<string?, JsonElement>().ConvertUsing(converter);
CreateMap<byte[]?, JsonElement?>().ConvertUsing(converter);
CreateMap<string?, JsonElement?>().ConvertUsing(converter);
}
}
}

0 comments on commit f21bc20

Please sign in to comment.