DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Hello I'm yunus emre.I'm a 24 year old.I have been living in istanbul,Turkey.I'm incredibly passionate about what I do and enjoy working with other open minded, enthusiastic people. I also like to share my experiences with others and blog regularly here. Buzz Words : .NET 2.0 and upwards C# ASP.NET (Web Forms, MVC, Web API) HTML/CSS MSSQL RavenDB Various Data Access libraries - from Disconnected and connected for SqlClient, NHibernate and Entity Framework I currently have been working as a software engineer in intertech.I am developing software to functional requirements and fixing bugs on banking platform using microsoft technologies in here. I'm interested in backend software development and enterprise software organizations.I'm keen on mostly about software architectures , design patterns , object oriented programming and enterprise software development.I have wrote articles in my own blog and communities about these topics.Also i like drawing software diagrams To become a good software engineer requires a good theoretical background as well as practical experiences.Therefore I will try to improve myself daily.New technologies and experiences raises new ideas and so we can change the world with these ideas.I believe that. I want to develop software on international projects in a international atmosphere Yunus Emre has posted 10 posts at DZone. You can read more from them at their website. View Full User Profile

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

01.08.2014
| 2312 views |
  • submit to reddit
// 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();
 
    }
}

Structure of our data objects:

Console output :


gist link ; https://gist.github.com/yemrekeskin/5764530#file-dataformates-cs