From 8c8e97075205469d0dd302f078e40226d66a5013 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Mon, 11 Dec 2023 20:50:01 +0330 Subject: [PATCH] Support for DeserializeFromBytes and SerializeToBytes --- ...icroservices.Serialization.BinaryGo.csproj | 4 +-- ...s.Serialization.DependencyInjection.csproj | 4 +-- ...roservices.Serialization.MemoryPack.csproj | 4 +-- ...oservices.Serialization.MessagePack.csproj | 6 +++-- ...vices.Serialization.Newtonsoft.Json.csproj | 6 +++-- ...ices.Serialization.System.Text.Json.csproj | 21 +++++++++++---- ...vices.Serialization.System.Text.Xml.csproj | 5 ++-- ...syMicroservices.Serialization.Tests.csproj | 2 +- ...roservices.Serialization.YamlDotNet.csproj | 6 +++-- .../EasyMicroservices.Serialization.csproj | 4 +-- .../Interfaces/IBaseSerializationProvider.cs | 13 +++++++++ .../Providers/BaseProvider.cs | 16 +++++++++++ .../BinarySerializationBaseProvider.cs | 22 +++++++++++++++ .../TextSerializationBaseProvider.cs | 27 ++++++++++++++++++- 14 files changed, 117 insertions(+), 23 deletions(-) diff --git a/src/CSharp/EasyMicroservices.Serialization.BinaryGo/EasyMicroservices.Serialization.BinaryGo.csproj b/src/CSharp/EasyMicroservices.Serialization.BinaryGo/EasyMicroservices.Serialization.BinaryGo.csproj index a7a826e..4083dd8 100644 --- a/src/CSharp/EasyMicroservices.Serialization.BinaryGo/EasyMicroservices.Serialization.BinaryGo.csproj +++ b/src/CSharp/EasyMicroservices.Serialization.BinaryGo/EasyMicroservices.Serialization.BinaryGo.csproj @@ -8,7 +8,7 @@ - netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net48;net45;net7.0 + netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net48;net45;net7.0;net8.0 @@ -17,7 +17,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.8 + 0.0.0.9 Serialize and deserialize wrapper EasyMicroservices@gmail.com Serialize,deserialize diff --git a/src/CSharp/EasyMicroservices.Serialization.DependencyInjection/EasyMicroservices.Serialization.DependencyInjection.csproj b/src/CSharp/EasyMicroservices.Serialization.DependencyInjection/EasyMicroservices.Serialization.DependencyInjection.csproj index 8634191..a61603e 100644 --- a/src/CSharp/EasyMicroservices.Serialization.DependencyInjection/EasyMicroservices.Serialization.DependencyInjection.csproj +++ b/src/CSharp/EasyMicroservices.Serialization.DependencyInjection/EasyMicroservices.Serialization.DependencyInjection.csproj @@ -8,7 +8,7 @@ - netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net48;net7.0 + netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net48;net7.0;net8.0 @@ -17,7 +17,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.8 + 0.0.0.9 Serialize and deserialize wrapper EasyMicroservices@gmail.com Serialize,deserialize diff --git a/src/CSharp/EasyMicroservices.Serialization.MemoryPack/EasyMicroservices.Serialization.MemoryPack.csproj b/src/CSharp/EasyMicroservices.Serialization.MemoryPack/EasyMicroservices.Serialization.MemoryPack.csproj index b349e2a..c95fdfb 100644 --- a/src/CSharp/EasyMicroservices.Serialization.MemoryPack/EasyMicroservices.Serialization.MemoryPack.csproj +++ b/src/CSharp/EasyMicroservices.Serialization.MemoryPack/EasyMicroservices.Serialization.MemoryPack.csproj @@ -8,7 +8,7 @@ - net7.0 + net7.0;net8.0 @@ -17,7 +17,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.8 + 0.0.0.9 Serialize and deserialize wrapper EasyMicroservice@gmail.com Serialize,deserialize diff --git a/src/CSharp/EasyMicroservices.Serialization.MessagePack/EasyMicroservices.Serialization.MessagePack.csproj b/src/CSharp/EasyMicroservices.Serialization.MessagePack/EasyMicroservices.Serialization.MessagePack.csproj index bb58505..3b5ae99 100644 --- a/src/CSharp/EasyMicroservices.Serialization.MessagePack/EasyMicroservices.Serialization.MessagePack.csproj +++ b/src/CSharp/EasyMicroservices.Serialization.MessagePack/EasyMicroservices.Serialization.MessagePack.csproj @@ -8,7 +8,7 @@ - netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net48;net7.0 + netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net48;net7.0;net8.0 @@ -17,7 +17,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.8 + 0.0.0.9 Serilize and deserilize wrapper EasyMicroservices@gmail.com Serilize,deserilize @@ -26,8 +26,10 @@ true .\bin\$(Configuration)\$(TargetFramework)\EasyMicroservice.Serialization.MessagePack.xml + + \ No newline at end of file diff --git a/src/CSharp/EasyMicroservices.Serialization.NewtonSoft.Json/EasyMicroservices.Serialization.Newtonsoft.Json.csproj b/src/CSharp/EasyMicroservices.Serialization.NewtonSoft.Json/EasyMicroservices.Serialization.Newtonsoft.Json.csproj index e10ff9f..e117eff 100644 --- a/src/CSharp/EasyMicroservices.Serialization.NewtonSoft.Json/EasyMicroservices.Serialization.Newtonsoft.Json.csproj +++ b/src/CSharp/EasyMicroservices.Serialization.NewtonSoft.Json/EasyMicroservices.Serialization.Newtonsoft.Json.csproj @@ -8,7 +8,7 @@ - netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net48;net452;net7.0 + netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net48;net452;net7.0;net8.0 @@ -17,7 +17,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.8 + 0.0.0.9 Serialize and deserialize wrapper EasyMicroservices@gmail.com Serialize,deserialize @@ -26,8 +26,10 @@ true .\bin\$(Configuration)\$(TargetFramework)\EasyMicroservice.Serialization.Newtonsoft.Json.xml + + diff --git a/src/CSharp/EasyMicroservices.Serialization.System.Text.Json/EasyMicroservices.Serialization.System.Text.Json.csproj b/src/CSharp/EasyMicroservices.Serialization.System.Text.Json/EasyMicroservices.Serialization.System.Text.Json.csproj index c0ec22d..2033b46 100644 --- a/src/CSharp/EasyMicroservices.Serialization.System.Text.Json/EasyMicroservices.Serialization.System.Text.Json.csproj +++ b/src/CSharp/EasyMicroservices.Serialization.System.Text.Json/EasyMicroservices.Serialization.System.Text.Json.csproj @@ -8,7 +8,7 @@ - netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net7.0;net48 + netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net7.0;net8.0;net48 @@ -17,7 +17,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.8 + 0.0.0.9 Serialize and deserialize wrapper EasyMicroservice@gmail.com Serialize,deserialize @@ -27,10 +27,21 @@ .\bin\$(Configuration)\$(TargetFramework)\EasyMicroservice.Serialization.System.Text.json.xml - - - + + + + + + + + + + + + + + diff --git a/src/CSharp/EasyMicroservices.Serialization.System.Text.Xml/EasyMicroservices.Serialization.System.Text.Xml.csproj b/src/CSharp/EasyMicroservices.Serialization.System.Text.Xml/EasyMicroservices.Serialization.System.Text.Xml.csproj index 888da81..edfa4df 100644 --- a/src/CSharp/EasyMicroservices.Serialization.System.Text.Xml/EasyMicroservices.Serialization.System.Text.Xml.csproj +++ b/src/CSharp/EasyMicroservices.Serialization.System.Text.Xml/EasyMicroservices.Serialization.System.Text.Xml.csproj @@ -8,7 +8,7 @@ - netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net48;net452;net7.0 + netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net48;net452;net7.0;net8.0 @@ -17,7 +17,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.8 + 0.0.0.9 Serialize and deserialize wrapper EasyMicroservice@gmail.com Serialize,deserialize @@ -34,4 +34,5 @@ + diff --git a/src/CSharp/EasyMicroservices.Serialization.Tests/EasyMicroservices.Serialization.Tests.csproj b/src/CSharp/EasyMicroservices.Serialization.Tests/EasyMicroservices.Serialization.Tests.csproj index 08f9e25..d1c2c55 100644 --- a/src/CSharp/EasyMicroservices.Serialization.Tests/EasyMicroservices.Serialization.Tests.csproj +++ b/src/CSharp/EasyMicroservices.Serialization.Tests/EasyMicroservices.Serialization.Tests.csproj @@ -8,7 +8,7 @@ - netcoreapp3.1;net6.0;net48;net452;net7.0 + netcoreapp3.1;net6.0;net48;net452;net7.0;net8.0 diff --git a/src/CSharp/EasyMicroservices.Serialization.YamlDotNet/EasyMicroservices.Serialization.YamlDotNet.csproj b/src/CSharp/EasyMicroservices.Serialization.YamlDotNet/EasyMicroservices.Serialization.YamlDotNet.csproj index 049fdbb..bc3841d 100644 --- a/src/CSharp/EasyMicroservices.Serialization.YamlDotNet/EasyMicroservices.Serialization.YamlDotNet.csproj +++ b/src/CSharp/EasyMicroservices.Serialization.YamlDotNet/EasyMicroservices.Serialization.YamlDotNet.csproj @@ -8,7 +8,7 @@ - netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net48;net452;net7.0 + netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0;net48;net452;net7.0;net8.0 @@ -17,7 +17,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.8 + 0.0.0.9 Serialize and deserialize wrapper EasyMicroservices@gmail.com Serialize,deserialize @@ -26,8 +26,10 @@ true .\bin\$(Configuration)\$(TargetFramework)\EasyMicroservice.Serialization.YamlDotNet.xml + + diff --git a/src/CSharp/EasyMicroservices.Serialization/EasyMicroservices.Serialization.csproj b/src/CSharp/EasyMicroservices.Serialization/EasyMicroservices.Serialization.csproj index d508984..88987aa 100644 --- a/src/CSharp/EasyMicroservices.Serialization/EasyMicroservices.Serialization.csproj +++ b/src/CSharp/EasyMicroservices.Serialization/EasyMicroservices.Serialization.csproj @@ -8,7 +8,7 @@ - netstandard2.0;netstandard2.1;net6.0;net45;net7.0 + netstandard2.0;netstandard2.1;net6.0;net45;net7.0;net8.0 @@ -17,7 +17,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.8 + 0.0.0.9 Serialize and deserialize wrapper EasyMicroservices@gmail.com Serialize,deserialize diff --git a/src/CSharp/EasyMicroservices.Serialization/Interfaces/IBaseSerializationProvider.cs b/src/CSharp/EasyMicroservices.Serialization/Interfaces/IBaseSerializationProvider.cs index 97956f6..f3d83e3 100644 --- a/src/CSharp/EasyMicroservices.Serialization/Interfaces/IBaseSerializationProvider.cs +++ b/src/CSharp/EasyMicroservices.Serialization/Interfaces/IBaseSerializationProvider.cs @@ -13,5 +13,18 @@ public interface IBaseSerializationProvider /// /// bool CanConvert(Type objectType); + /// + /// + /// + /// + /// + ReadOnlySpan SerializeToBytes(T value); + /// + /// + /// + /// + /// + /// + T DeserializeFromBytes(ReadOnlySpan bytes); } } diff --git a/src/CSharp/EasyMicroservices.Serialization/Providers/BaseProvider.cs b/src/CSharp/EasyMicroservices.Serialization/Providers/BaseProvider.cs index 806ea49..786f475 100644 --- a/src/CSharp/EasyMicroservices.Serialization/Providers/BaseProvider.cs +++ b/src/CSharp/EasyMicroservices.Serialization/Providers/BaseProvider.cs @@ -58,5 +58,21 @@ protected virtual Type ReplaceType(Type type) { return type; } + + /// + /// + /// + /// + /// + /// + public abstract ReadOnlySpan SerializeToBytes(T value); + + /// + /// + /// + /// + /// + /// + public abstract T DeserializeFromBytes(ReadOnlySpan bytes); } } diff --git a/src/CSharp/EasyMicroservices.Serialization/Providers/BinarySerializationBaseProvider.cs b/src/CSharp/EasyMicroservices.Serialization/Providers/BinarySerializationBaseProvider.cs index 161c639..4221d07 100644 --- a/src/CSharp/EasyMicroservices.Serialization/Providers/BinarySerializationBaseProvider.cs +++ b/src/CSharp/EasyMicroservices.Serialization/Providers/BinarySerializationBaseProvider.cs @@ -21,5 +21,27 @@ public abstract class BinarySerializationBaseProvider : BaseProvider, IBinarySer /// /// public abstract ReadOnlySpan Serialize(T value); + + /// + /// + /// + /// + /// + /// + public override ReadOnlySpan SerializeToBytes(T value) + { + return Serialize(value); + } + + /// + /// + /// + /// + /// + /// + public override T DeserializeFromBytes(ReadOnlySpan bytes) + { + return Deserialize(bytes); + } } } diff --git a/src/CSharp/EasyMicroservices.Serialization/Providers/TextSerializationBaseProvider.cs b/src/CSharp/EasyMicroservices.Serialization/Providers/TextSerializationBaseProvider.cs index bb23e54..970287a 100644 --- a/src/CSharp/EasyMicroservices.Serialization/Providers/TextSerializationBaseProvider.cs +++ b/src/CSharp/EasyMicroservices.Serialization/Providers/TextSerializationBaseProvider.cs @@ -1,4 +1,8 @@ -using System; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Text; using EasyMicroservices.Serialization.Interfaces; namespace EasyMicroservices.Serialization.Providers @@ -24,5 +28,26 @@ public abstract class TextSerializationBaseProvider : BaseProvider, ITextSeriali /// /// public abstract string Serialize(T value); + /// + /// + /// + /// + /// + /// + public override ReadOnlySpan SerializeToBytes(T value) + { + return Encoding.UTF8.GetBytes(Serialize(value)); + } + + /// + /// + /// + /// + /// + /// + public override T DeserializeFromBytes(ReadOnlySpan bytes) + { + return Deserialize(Encoding.UTF8.GetString(bytes.ToArray())); + } } }