CSharp · Console
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Polenter.Serialization;
public class Program
{
public static void Main()
{
var dict = new Dictionary<string, string[]>
{
{ "nikhil", new string[] { "[email protected]", "[email protected]" } }
};
var serialized = Serialize(dict);
Console.WriteLine(serialized);
var deserialized = Deserialize<Dictionary<string, string[]>>(serialized);
Console.WriteLine(string.Join(",", deserialized["nikhil"]) == string.Join(",", deserialized["nikhil"])); // True
}
private static string Serialize<T>(T obj) where T : class
{
using (var stream = new MemoryStream())
{
var serializer = new SharpSerializer();
serializer.Serialize(obj, stream);
stream.Position = 0;
return new StreamReader(stream).ReadToEnd();
}
}
private static T Deserialize<T>(string val) where T : class
{
if (string.IsNullOrWhiteSpace(val))
{
return default(T);
}
var bytes = Encoding.UTF8.GetBytes(val);
using (var stream = new MemoryStream(bytes))
{
var serializer = new SharpSerializer();
return serializer.Deserialize(stream) as T;
}
}
}