-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: ISerializable, Newtonsoft, SystemText Serializers.
- Loading branch information
Showing
35 changed files
with
451 additions
and
79 deletions.
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
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,20 @@ | ||
namespace Reloaded.Messaging.Interfaces | ||
{ | ||
/// <summary> | ||
/// Defines the minimal interface necessary to bootstrap a 3rd party compressor. | ||
/// </summary> | ||
public interface ICompressor | ||
{ | ||
/// <summary> | ||
/// Compresses the provided byte array. | ||
/// </summary> | ||
/// <param name="data">The data to compress.</param> | ||
byte[] Compress(byte[] data); | ||
|
||
/// <summary> | ||
/// Decompresses the provided byte array. | ||
/// </summary> | ||
/// <param name="data">The data to decompress.</param> | ||
byte[] Decompress(byte[] data); | ||
} | ||
} |
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,15 @@ | ||
using Reloaded.Messaging.Interfaces.Message; | ||
|
||
namespace Reloaded.Messaging.Interfaces | ||
{ | ||
/// <summary> | ||
/// Common interface shared by individual messages. | ||
/// </summary> | ||
public interface IMessage<TMessageType> : ISerializable where TMessageType : unmanaged | ||
{ | ||
/// <summary> | ||
/// Returns the unique message type/id for this message. | ||
/// </summary> | ||
TMessageType GetMessageType(); | ||
} | ||
} |
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,22 @@ | ||
namespace Reloaded.Messaging.Interfaces | ||
{ | ||
/// <summary> | ||
/// Defines the minimal interface necessary to bootstrap a 3rd party serializer. | ||
/// </summary> | ||
public interface ISerializer | ||
{ | ||
/// <summary> | ||
/// Deserializes the provided byte array into a concrete type. | ||
/// </summary> | ||
/// <typeparam name="TStruct">The type of the structure to deserialize.</typeparam> | ||
/// <param name="serialized">The data to deserialize.</param> | ||
TStruct Deserialize<TStruct>(byte[] serialized); | ||
|
||
/// <summary> | ||
/// Serializes the provided item into a byte array. | ||
/// </summary> | ||
/// <param name="item">The item to serialize to bytes.</param> | ||
/// <returns>Serialized item.</returns> | ||
byte[] Serialize<TStruct>(ref TStruct item); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Source/Reloaded.Messaging.Interfaces/Message/ISerializable.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,18 @@ | ||
namespace Reloaded.Messaging.Interfaces.Message | ||
{ | ||
/// <summary> | ||
/// An interface that provides serialization/deserialization and compression/decompression support for. | ||
/// </summary> | ||
public interface ISerializable | ||
{ | ||
/// <summary> | ||
/// Returns the serializer for this specific type. | ||
/// </summary> | ||
ISerializer GetSerializer(); | ||
|
||
/// <summary> | ||
/// Returns the compressor for this specific type. | ||
/// </summary> | ||
ICompressor GetCompressor(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Source/Reloaded.Messaging.Interfaces/Reloaded.Messaging.Interfaces.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,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<Description>Contains all of the interfaces (and some extension functionality) used by the base Reloaded.Messaging library. | ||
This package exists to allow you to use various features of the library, such as serializers without the need to import the dependencies of the base package.</Description> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<PackageLicenseFile>LICENSE.md</PackageLicenseFile> | ||
<PackageProjectUrl>https://github.com/Reloaded-Project/Reloaded.Messaging</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/Reloaded-Project/Reloaded.Messaging</RepositoryUrl> | ||
<PackageIconUrl>https://avatars1.githubusercontent.com/u/45473408</PackageIconUrl> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\LICENSE.md"> | ||
<Pack>True</Pack> | ||
<PackagePath></PackagePath> | ||
</None> | ||
</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,54 @@ | ||
using System.Runtime.CompilerServices; | ||
using Reloaded.Messaging.Interfaces.Message; | ||
|
||
namespace Reloaded.Messaging.Interfaces | ||
{ | ||
/// <summary> | ||
/// An extension class providing serialization support to implementers of <see cref="ISerializable"/>. | ||
/// </summary> | ||
public static class Serializable | ||
{ | ||
/// <summary> | ||
/// Serializes and compresses the current instance of the class or struct | ||
/// using the serializer and compressor defined by the <see cref="ISerializable"/>. | ||
/// </summary> | ||
public static byte[] Serialize<TSerializable>(this TSerializable serializable) where TSerializable : ISerializable | ||
{ | ||
var serializer = serializable.GetSerializer(); | ||
var compressor = serializable.GetCompressor(); | ||
|
||
byte[] serialized = serializer.Serialize(ref serializable); | ||
if (compressor != null) | ||
return compressor.Compress(serialized); | ||
|
||
return serialized; | ||
} | ||
|
||
/// <summary> | ||
/// Decompresses and deserializes the current instance of the class or struct using the | ||
/// serializer and compressor defined by the <see cref="ISerializable"/>. | ||
/// </summary> | ||
public static ISerializable Deserialize<TType>(this TType serializable, byte[] bytes) where TType : ISerializable | ||
{ | ||
var compressor = serializable.GetCompressor(); | ||
var serializer = serializable.GetSerializer(); | ||
|
||
byte[] decompressed = bytes; | ||
if (compressor != null) | ||
decompressed = compressor.Decompress(bytes); | ||
|
||
return serializer.Deserialize<TType>(decompressed); | ||
} | ||
|
||
/// <summary> | ||
/// Decompresses and deserializes the current instance of the class or struct using the | ||
/// serializer and compressor defined by the <see cref="ISerializable"/>. | ||
/// </summary> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static ISerializable Deserialize<TType>(byte[] bytes) where TType : ISerializable, new() | ||
{ | ||
var serializable = new TType(); | ||
return Deserialize(serializable, bytes); | ||
} | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
Source/Reloaded.Messaging.Serializer.NewtonsoftJson/NewtonsoftJsonSerializer.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,37 @@ | ||
using System; | ||
using System.Runtime.Serialization; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
using Reloaded.Messaging.Interfaces; | ||
|
||
namespace Reloaded.Messaging.Serializer.NewtonsoftJson | ||
{ | ||
public class NewtonsoftJsonSerializer : ISerializer | ||
{ | ||
/// <summary> | ||
/// Serialization options. | ||
/// </summary> | ||
public JsonSerializerSettings Options { get; private set; } | ||
|
||
/// <summary> | ||
/// Creates the System.Text.Json based serializer. | ||
/// </summary> | ||
/// <param name="serializerOptions">Options to use for serialization/deserialization.</param> | ||
public NewtonsoftJsonSerializer(JsonSerializerSettings serializerOptions) | ||
{ | ||
Options = serializerOptions; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public TStruct Deserialize<TStruct>(byte[] serialized) | ||
{ | ||
return JsonConvert.DeserializeObject<TStruct>(Encoding.UTF8.GetString(serialized), Options); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public byte[] Serialize<TStruct>(ref TStruct item) | ||
{ | ||
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(item, Options)); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...d.Messaging.Serializer.NewtonsoftJson/Reloaded.Messaging.Serializer.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,33 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<PackageId>Reloaded.Messaging.Serializer.NewtonsoftJson</PackageId> | ||
<Authors>Sewer56</Authors> | ||
<Company>Sewer56</Company> | ||
<Description>Basic Json serialization implementation for Reloaded.Messaging based off of Newtonsoft.Json.</Description> | ||
<Copyright>Sewer56</Copyright> | ||
<PackageLicenseFile>README.md</PackageLicenseFile> | ||
<PackageProjectUrl>https://github.com/Reloaded-Project/Reloaded.Messaging</PackageProjectUrl> | ||
<PackageIconUrl>https://avatars1.githubusercontent.com/u/45473408</PackageIconUrl> | ||
<RepositoryUrl>https://github.com/Reloaded-Project/Reloaded.Messaging</RepositoryUrl> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Reloaded.Messaging.Interfaces\Reloaded.Messaging.Interfaces.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\README.md"> | ||
<Pack>True</Pack> | ||
<PackagePath></PackagePath> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.