Skip to content

Commit 8826a68

Browse files
Anouar HassineNicolas ERNY
authored andcommitted
Adding support for custom serialization (#73)
1 parent 52a66b9 commit 8826a68

10 files changed

Lines changed: 95 additions & 16 deletions
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Newtonsoft.Json.Linq;
2-
using ReactiveXComponent.Serializer;
1+
using ReactiveXComponent.Serializer;
32

43
namespace ReactiveXComponent.Common
54
{
@@ -22,19 +21,8 @@ public MessageEventArgs(StateMachineRefHeader stateMachineRefHeader, object mess
2221

2322
public T GetMessage<T>() where T : class
2423
{
25-
if (SerializationType == SerializationType.Binary)
26-
{
27-
return MessageReceived as T;
28-
}
29-
30-
if (SerializationType == SerializationType.Json
31-
|| SerializationType == SerializationType.Bson)
32-
{
33-
var jResult = MessageReceived as JObject;
34-
return jResult?.ToObject<T>();
35-
}
36-
37-
return null;
24+
var serializer = SerializerFactory.CreateSerializer(SerializationType);
25+
return serializer.CastObject<T>(MessageReceived);
3826
}
3927
}
4028
}

ReactiveXComponent/ReactiveXComponent.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@
116116
<Compile Include="Common\ReactiveXComponentException.cs" />
117117
<Compile Include="Serializer\BinarySerializer.cs" />
118118
<Compile Include="Serializer\BsonSerializer.cs" />
119+
<Compile Include="Serializer\CustomSerializerAttribute.cs" />
120+
<Compile Include="Serializer\CustomSerializerContainerAttribute.cs" />
119121
<Compile Include="Serializer\ISerializer.cs" />
120122
<Compile Include="Serializer\JsonSerializer.cs" />
121123
<Compile Include="Serializer\SerializationType.cs" />

ReactiveXComponent/Serializer/BinarySerializer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@ public object Deserialize(Stream stream)
3838

3939
return obj;
4040
}
41+
42+
public T CastObject<T>(object message) where T : class
43+
{
44+
return message as T;
45+
}
4146
}
4247
}

ReactiveXComponent/Serializer/BsonSerializer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO;
22
using Newtonsoft.Json.Bson;
3+
using Newtonsoft.Json.Linq;
34

45
namespace ReactiveXComponent.Serializer
56
{
@@ -20,5 +21,10 @@ public object Deserialize(Stream stream)
2021
return _serializer.Deserialize(bsonReader);
2122
}
2223

24+
public T CastObject<T>(object message) where T : class
25+
{
26+
var jResult = message as JObject;
27+
return jResult?.ToObject<T>();
28+
}
2329
}
2430
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace ReactiveXComponent.Serializer
4+
{
5+
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
6+
public class CustomSerializerAttribute : Attribute
7+
{
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace ReactiveXComponent.Serializer
4+
{
5+
[AttributeUsage(AttributeTargets.Assembly)]
6+
public class CustomSerializerContainerAttribute : Attribute
7+
{
8+
}
9+
}

ReactiveXComponent/Serializer/ISerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace ReactiveXComponent.Serializer
55
public interface ISerializer
66
{
77
object Deserialize(Stream stream);
8+
T CastObject<T>(object message) where T : class;
89
void Serialize(Stream stream, object message);
910
}
1011
}

ReactiveXComponent/Serializer/JsonSerializer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
45

56
namespace ReactiveXComponent.Serializer
67
{
@@ -40,5 +41,11 @@ public object Deserialize(Stream stream)
4041
throw new XCSerializationException(e.Message, e);
4142
}
4243
}
44+
45+
public T CastObject<T>(object message) where T : class
46+
{
47+
var jResult = message as JObject;
48+
return jResult?.ToObject<T>();
49+
}
4350
}
4451
}

ReactiveXComponent/Serializer/SerializationType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public enum SerializationType
44
{
55
Binary = 0,
66
Json = 1,
7-
Bson = 2
7+
Bson = 2,
8+
Custom = 3
89
}
910
}

ReactiveXComponent/Serializer/SerializerFactory.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
15
namespace ReactiveXComponent.Serializer
26
{
37
public static class SerializerFactory
48
{
9+
private static ISerializer _customSerializer;
10+
private static readonly object CustomSerializerLock = new object();
11+
512
public static ISerializer CreateSerializer(SerializationType serializationType)
613
{
714
switch (serializationType)
@@ -12,9 +19,53 @@ public static ISerializer CreateSerializer(SerializationType serializationType)
1219
return new JsonSerializer();
1320
case SerializationType.Bson:
1421
return new BsonSerializer();
22+
case SerializationType.Custom:
23+
return FindCustomSerializer();
1524
default:
1625
throw new XCSerializationException("Unhandled serialization type " + serializationType);
1726
}
1827
}
28+
29+
private static ISerializer FindCustomSerializer()
30+
{
31+
if (_customSerializer == null)
32+
{
33+
lock (CustomSerializerLock)
34+
{
35+
if (_customSerializer == null)
36+
{
37+
ISerializer customSerializer = null;
38+
39+
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
40+
{
41+
if (assembly.GetCustomAttributes().OfType<CustomSerializerContainerAttribute>().Any())
42+
{
43+
foreach (var exportedType in assembly.GetExportedTypes())
44+
{
45+
var customSerializerAttribute = exportedType.GetCustomAttributes()
46+
.OfType<CustomSerializerAttribute>()
47+
.FirstOrDefault();
48+
49+
if (customSerializerAttribute != null)
50+
{
51+
customSerializer = (ISerializer)Activator.CreateInstance(exportedType);
52+
break;
53+
}
54+
}
55+
}
56+
}
57+
58+
if (customSerializer == null)
59+
{
60+
throw new XCSerializationException("Unable to find a custom serializer");
61+
}
62+
63+
_customSerializer = customSerializer;
64+
}
65+
}
66+
}
67+
68+
return _customSerializer;
69+
}
1970
}
2071
}

0 commit comments

Comments
 (0)