This repository has been archived by the owner on Dec 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated to support automapper base mappings
- Loading branch information
1 parent
970974a
commit f21bc20
Showing
8 changed files
with
290 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
158
src/Operational.MediatR.NewtonsoftJson/JTokenConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
src/Operational.MediatR.NewtonsoftJson/NewtonsoftJsonMapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...erational.MediatR.NewtonsoftJson/Rocket.Surgery.Operational.MediatR.NewtonsoftJson.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |