Skip to content

Instantly share code, notes, and snippets.

@takekazuomi
Created April 10, 2014 00:10
Show Gist options
  • Save takekazuomi/10332165 to your computer and use it in GitHub Desktop.
Save takekazuomi/10332165 to your computer and use it in GitHub Desktop.
Message Pack CLI issue #27 reproducible code
using System;
using System.IO;
using System.Linq;
using MsgPack.Serialization;
namespace TestMsgpackCli001
{
using System.Collections.Generic;
namespace TestMsgpackCli001
{
public class SomeOne
{
public string Target;
public IDictionary<string, string> Data;
}
internal class Program
{
private static void Main(string[] args)
{
var h = new SomeOne
{
Target = "boo",
Data = new Dictionary<string, string>()
};
h.Data.Add(new KeyValuePair<string, string>("foo", "woo"));
var b = h.Pack();
var h2 = b.UnPack<SomeOne>();
Console.WriteLine("{0},{1}", h2.Target, string.Join(",", h2.Data.Select(pair => string.Format("{0}=>{1}", pair.Key, pair.Value))));
}
}
public static class Extentions
{
public static byte[] Pack<T>(this T data)
{
var serializer = MessagePackSerializer.Create<T>();
var stream = new MemoryStream();
serializer.Pack(stream, data);
return stream.ToArray();
}
public static T UnPack<T>(this byte[] data)
{
var serializer = MessagePackSerializer.Create<T>();
var stream = new MemoryStream(data);
return serializer.Unpack(stream);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment