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

namespace DotNet.Common.Util
{
    ///<summary>
    /// Json操作帮助类
    ///</summary>
publicstaticclass JsonHelper
    {
        #region

        ///<summary>
        /// 对象转换成json
        ///</summary>
        ///<typeparam name="T"></typeparam>
        ///<param name="jsonObject">需要格式化的对象</param>
        ///<returns>Json字符串</returns>
publicstaticstring<T>(T jsonObject)
        {
            DataContractJsonSerializer serializer =newtypeof(T));
            string=null;
            using=new//定义一个stream用来存发序列化之后的内容
            {
                serializer.WriteObject(ms, jsonObject);
                json =//将stream读取成一个字符串形式的数据,并且返回
                ms.Close();
            }
            return json;
        }

        ///<summary>
        /// json字符串转换成对象
        ///</summary>
        ///<typeparam name="T"></typeparam>
        ///<param name="json">要转换成对象的json字符串</param>
        ///<returns></returns>
publicstatic<T>(string json)
        {
            DataContractJsonSerializer serializer =newtypeof(T));
            T obj =default(T);
            using=new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                obj = (T)serializer.ReadObject(ms);
                ms.Close();
            }
            return obj;
        }

        #endregion

    }
}