-
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.
Add JSON, MessagePack and BinaryFormatter Serializer (#15)
* Add JSON, MessagePack and BinaryFormatter Serializer * Added ISerializer class to allow users to extend with additional serializers. * Clean up Codacy static analysis errors * Remove BinaryFotmatterObjectSerializer
- Loading branch information
1 parent
91c6a0e
commit b561e91
Showing
12 changed files
with
116 additions
and
20 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
43 changes: 43 additions & 0 deletions
43
gj.autofac.caching.redis.tests/MessagePackObjectSerializerTest.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,43 @@ | ||
using gj.autofac.caching.redis; | ||
using gj.autofac.caching.redis.Serialization; | ||
|
||
namespace gj.autofac.caching.redis.tests; | ||
|
||
[TestFixture] | ||
[TestOf(typeof(MessagePackObjectSerializer))] | ||
public class MessagePackObjectSerializerTest | ||
{ | ||
private TestData _data; | ||
private MessagePackObjectSerializer _objectSerializer; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_data = new TestData | ||
{ | ||
Id = 8, | ||
UserId = 8, | ||
Title = "TEST", | ||
Completed = true | ||
}; | ||
_objectSerializer = new MessagePackObjectSerializer(); | ||
} | ||
|
||
[Test] | ||
public void SerializationTest() | ||
{ | ||
var arr = _objectSerializer.SerializeToBinary(_data); | ||
Assert.That(arr, Is.Not.Empty); | ||
} | ||
|
||
[Test] | ||
public void DeserializationTest() | ||
{ | ||
var arr = _objectSerializer.SerializeToBinary(_data); | ||
var data2 = (TestData)_objectSerializer.DeserializeFromBinary(arr, typeof(TestData))!; | ||
Assert.That(data2.Id, Is.EqualTo(8)); | ||
Assert.That(data2.UserId, Is.EqualTo(8)); | ||
Assert.That(data2.Title, Is.EqualTo("TEST")); | ||
Assert.That(data2.Completed, Is.True); | ||
} | ||
} |
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
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,7 @@ | ||
namespace gj.autofac.caching.redis.Serialization; | ||
|
||
public interface IObjectSerializer | ||
{ | ||
public byte[] SerializeToBinary(object? obj); | ||
public object? DeserializeFromBinary(byte[] binaryData, Type type); | ||
} |
29 changes: 29 additions & 0 deletions
29
gj.autofac.caching.redis/Serialization/JSONObjectSerializer.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,29 @@ | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace gj.autofac.caching.redis.Serialization; | ||
|
||
public class JSONObjectSerializer: IObjectSerializer | ||
{ | ||
/// <summary> | ||
/// Serializes an object to binary using JSON. | ||
/// </summary> | ||
/// <param name="obj">The object to serialize.</param> | ||
/// <returns>The binary representation of the object.</returns> | ||
public byte[] SerializeToBinary(object? obj) | ||
{ | ||
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj)); | ||
} | ||
|
||
/// <summary> | ||
/// Deserializes binary data to the specified type using JSON. | ||
/// </summary> | ||
/// <param name="binaryData">The binary data to deserialize.</param> | ||
/// <param name="type">The type to deserialize into.</param> | ||
/// <returns>The deserialized object.</returns> | ||
public object? DeserializeFromBinary(byte[] binaryData, Type type) | ||
{ | ||
string resultString = Encoding.UTF8.GetString(binaryData); | ||
return JsonConvert.DeserializeObject(resultString, type); | ||
} | ||
} |
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