Skip to content

Commit

Permalink
NatsServiceSerializer: bug fix. Exception thrown when a service had m…
Browse files Browse the repository at this point in the history
…ore than one method with the same retrun type.
  • Loading branch information
fremag committed Nov 21, 2017
1 parent d2d5e06 commit dcdbcad
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 194 deletions.
154 changes: 77 additions & 77 deletions Nats.Services.Core/NatsServiceSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,90 +5,90 @@

namespace Nats.Services.Core
{
public class NatsServiceSerializer<T>
{
DataContractJsonSerializer serializer;
Dictionary<Type, DataContractJsonSerializer> returnTypeSerializers = new Dictionary<Type, DataContractJsonSerializer>();
public class NatsServiceSerializer<T>
{
DataContractJsonSerializer serializer;
Dictionary<Type, DataContractJsonSerializer> returnTypeSerializers = new Dictionary<Type, DataContractJsonSerializer>();

public NatsServiceSerializer()
{
HashSet<Type> types = new HashSet<Type>();
foreach (var methInfo in typeof(T).GetAllMethodInfos())
{
foreach (var paramInfo in methInfo.GetParameters())
{
types.Add(paramInfo.ParameterType);
}
if (methInfo.ReturnType != typeof(void))
{
returnTypeSerializers.Add(methInfo.ReturnType, new DataContractJsonSerializer(methInfo.ReturnType));
}
}
public NatsServiceSerializer()
{
HashSet<Type> types = new HashSet<Type>();
foreach (var methInfo in typeof(T).GetAllMethodInfos())
{
foreach (var paramInfo in methInfo.GetParameters())
{
types.Add(paramInfo.ParameterType);
}
if (methInfo.ReturnType != typeof(void) && ! returnTypeSerializers.ContainsKey(methInfo.ReturnType))
{
returnTypeSerializers.Add(methInfo.ReturnType, new DataContractJsonSerializer(methInfo.ReturnType));
}
}

foreach (var evtInfo in typeof(T).GetAllEventInfos())
{
var methInfo = evtInfo.EventHandlerType.GetMethod(nameof(EventHandler.Invoke));
foreach (var paramInfo in methInfo.GetParameters())
{
types.Add(paramInfo.ParameterType);
}
}
foreach (var evtInfo in typeof(T).GetAllEventInfos())
{
var methInfo = evtInfo.EventHandlerType.GetMethod(nameof(EventHandler.Invoke));
foreach (var paramInfo in methInfo.GetParameters())
{
types.Add(paramInfo.ParameterType);
}
}

serializer = new DataContractJsonSerializer(typeof(List<KeyValuePair<string, object>>), types);
}
serializer = new DataContractJsonSerializer(typeof(List<KeyValuePair<string, object>>), types);
}

public List<KeyValuePair<string, object>> DeserializeMethodArguments(byte[] buffer)
{
return DeserializeObject(serializer, buffer) as List<KeyValuePair<string, object>>;
}
public byte[] SerializeMethodArguments(object obj)
{
return SerializeObject(serializer, obj);
}
public List<KeyValuePair<string, object>> DeserializeMethodArguments(byte[] buffer)
{
return DeserializeObject(serializer, buffer) as List<KeyValuePair<string, object>>;
}
public byte[] SerializeMethodArguments(object obj)
{
return SerializeObject(serializer, obj);
}

public object DeserializeReturnObject(Type type, byte[] buffer)
{
DataContractJsonSerializer returnTypeSerializer = returnTypeSerializers[type];
return DeserializeObject(returnTypeSerializer, buffer);
}
public byte[] SerializeReturnObject(object obj)
{
DataContractJsonSerializer returnTypeSerializer = returnTypeSerializers[obj.GetType()];
return SerializeObject(returnTypeSerializer, obj);
}
public object DeserializeReturnObject(Type type, byte[] buffer)
{
DataContractJsonSerializer returnTypeSerializer = returnTypeSerializers[type];
return DeserializeObject(returnTypeSerializer, buffer);
}
public byte[] SerializeReturnObject(object obj)
{
DataContractJsonSerializer returnTypeSerializer = returnTypeSerializers[obj.GetType()];
return SerializeObject(returnTypeSerializer, obj);
}

public static object DeserializeObject(DataContractJsonSerializer serializer, byte[] buffer)
{
using (var stream = new MemoryStream(buffer))
{
var obj = serializer.ReadObject(stream);
return obj;
}
}
public static object DeserializeObject(DataContractJsonSerializer serializer, byte[] buffer)
{
using (var stream = new MemoryStream(buffer))
{
var obj = serializer.ReadObject(stream);
return obj;
}
}

public static byte[] SerializeObject(DataContractJsonSerializer serializer, object obj)
{
if (obj == null)
{
return null;
}
public static byte[] SerializeObject(DataContractJsonSerializer serializer, object obj)
{
if (obj == null)
{
return null;
}

using (var stream = new MemoryStream())
{
serializer.WriteObject(stream, obj);
var buffer = stream.ToArray();
return buffer;
}
}
using (var stream = new MemoryStream())
{
serializer.WriteObject(stream, obj);
var buffer = stream.ToArray();
return buffer;
}
}

public string ToString(byte[] buffer)
{
using (var stream = new MemoryStream(buffer))
{
stream.Position = 0;
var streamReader = new StreamReader(stream);
return streamReader.ReadToEnd();
}
}
}
public string ToString(byte[] buffer)
{
using (var stream = new MemoryStream(buffer))
{
stream.Position = 0;
var streamReader = new StreamReader(stream);
return streamReader.ReadToEnd();
}
}
}
}
Loading

0 comments on commit dcdbcad

Please sign in to comment.