cloudnet-compose/vote/dotnet/Vote/Messaging/MessageHelper.cs

24 lines
637 B
C#
Raw Permalink Normal View History

2018-09-21 20:23:31 +02:00
using Newtonsoft.Json;
using Vote.Messaging.Messages;
using System.Text;
namespace Vote.Messaging
{
public class MessageHelper
{
public static byte[] ToData<TMessage>(TMessage message)
where TMessage : Message
{
var json = JsonConvert.SerializeObject(message);
return Encoding.Unicode.GetBytes(json);
}
public static TMessage FromData<TMessage>(byte[] data)
where TMessage : Message
{
var json = Encoding.Unicode.GetString(data);
return (TMessage)JsonConvert.DeserializeObject<TMessage>(json);
}
}
}