Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Object Convert To Other Data Format ( JSON, XML, Binary )

View DataFormates.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
// Object Structure
 
[Serializable]
public class BasePerson
{
public string SerialNo { get; set; }
public Guid IdNo { get; set; }
}
 
[Serializable]
public class Person
:BasePerson
{
public string Name { get; set; }
public string Surname { get; set; }
public short Age { get; set; }
 
public AddressInfo AddressInfo { get; set; }
}
 
[Serializable]
public class AddressInfo
{
public string Street { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
 
// Helper class for other data formats
// for binary serilization
public static class BinarySerilizationHelper
{
public static TObject Deserialize<TObject>(string binarydata)
{
byte[] b = Convert.FromBase64String(binarydata);
using (var stream = new MemoryStream(b))
{
var formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
return (TObject)formatter.Deserialize(stream);
}
}
 
public static string Serialize<TObject>(TObject obj)
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
stream.Flush();
stream.Position = 0;
return Convert.ToBase64String(stream.ToArray());
}
}
}
// for XML Serilization
public static class XmlSerilizationHelper
{
public static string ToXML(Object obj)
{
var stringwriter = new System.IO.StringWriter();
var serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(stringwriter, obj);
return stringwriter.ToString();
}
 
public static BasePerson ToObject(string xml)
{
var stringReader = new System.IO.StringReader(xml);
var serializer = new XmlSerializer(typeof(Person));
return serializer.Deserialize(stringReader) as Person;
}
}
 
// Let's use our helper classes in console application :D
class Program
{
static void Main(string[] args)
{
// JSON
// Binary
// XML
 
AddressInfo address = new AddressInfo
{
Street = "Bülbül str.",
City = "İstanbul",
Country = "Turkey"
};
 
Person obj = new Person
{
AddressInfo = address,
IdNo = System.Guid.NewGuid(),
SerialNo = "SED23SU98",
Age = 13,
Name = "Yunus Emre",
Surname = "KESKIN"
};
 
 
// JSON
var json = new JavaScriptSerializer().Serialize(obj);
Console.WriteLine(json);
 
Console.WriteLine("---");
 
// XML
string xml = XmlSerilizationHelper.ToXML(obj);
Console.WriteLine(xml);
 
Console.WriteLine("---");
 
Person person = XmlSerilizationHelper.ToObject(xml) as Person;
// Binary Serilization
string binary = BinarySerilizationHelper.Serialize(obj);
Console.WriteLine(binary);
Person dummy = BinarySerilizationHelper.Deserialize<Person>(binary);
Console.WriteLine(dummy.IdNo);
 
Console.ReadLine();
 
}
}
 
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.