using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace Ant.ORM
{
public class JsonToMod
{
///
/// 实体转为Json格式
///
/// 实体
///
public static string ToJson(object entity)
{
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(entity.GetType());
using (var ms = new MemoryStream())
{
serializer.WriteObject(ms, entity);
var sb = new StringBuilder();
sb.Append(Encoding.UTF8.GetString(ms.ToArray()));
return sb.ToString();
}
}
///
/// Json格式转成实体
///
///
///
///
public static object ToObject(string jsonstr) where T : new()
{
T obj = new T();
var json = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonstr)))
{
T mod = (T)json.ReadObject(stream);
}
return obj;
}
}
}