C# Json Serialzer

These two methods are tool methods, they are very useful. Below code's demo is about object array serialisation. So the GetknowTypes method is used in the constructor of the class named DataContractJsonSerializer

The project demo the serialisation of object.

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;


namespace ExampleSerialize
{
    class Program
    {
        static void Main(string[] args)
        {
            object[] obj1 = new[] { "1", "2", "3" };

            string value = SerializeObject<object[]>(obj1);

            Console.WriteLine("Object string: "+value);

            object[] obj2 = DeserializeString<object[]>(value);

            Console.WriteLine("Object: "+obj2);

            object[] obj3 = new[] { new A() };

            ((A)DeserializeString<object[]>(SerializeObject<object[]>(obj3))[0]).Speak();

            Console.ReadLine();
        }

        private static string SerializeObject<T>(T data)
        {
            string serializeString = null;

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(object), GetknowTypes(null));

            MemoryStream str = new MemoryStream();

            serializer.WriteObject(str,data);

            byte[] buffer = new byte[str.Length];

            str.Seek(0,SeekOrigin.Begin);

            str.Read(buffer,0,(int)str.Length);

            serializeString = Encoding.UTF8.GetString(buffer, 0, buffer.Length);

            return serializeString;
        }

        private static T DeserializeString<T>(string dataString)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(object),GetknowTypes(null));

            MemoryStream str = new MemoryStream();

            byte[] buffer = Encoding.UTF8.GetBytes(dataString);

            str.Write(buffer,0,buffer.Length);

            str.Seek(0,SeekOrigin.Begin);

            T result = (T)serializer.ReadObject(str);

            return result;
        }

        private static IEnumerable<Type> GetknowTypes(object provider)
        {
            yield return typeof(object[]);
            yield return typeof(string[]);
            yield return typeof(A);
            yield return typeof(A[]);
        }
    }

    public class A
    {
        public string Name { get; set; }
        public string Words { get; set; }

        public A()
        {
            this.Name = "A";
            this.Words = "I am A";
        }

        public A(string name, string words)
        {
            this.Name = name;
            this.Words = words;
        }

        public void Speak()
        {
            Console.WriteLine(this.Name+": "+this.Words);
        }
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值