/*******************************************************************************
* Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
* 类的信息:
* 1.程序名称:JsonHelperForPC.cs
* 2.功能描述:Json转换处理,PC端使用。
* 编辑履历:
* 作者 日期 版本 修改内容
* 付斌 2018/08/08 1.00 新建
*******************************************************************************/
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Dongke.IBOSS.PRD.Basics.Library
{
public static class JsonHelperForPC
{
private static JsonSerializerSettings _jsonSettings;
static JsonHelperForPC()
{
IsoDateTimeConverter datetimeConverter = new IsoDateTimeConverter();
datetimeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
_jsonSettings = new JsonSerializerSettings();
_jsonSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
//_jsonSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
_jsonSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
_jsonSettings.Converters.Add(datetimeConverter);
}
///
/// 将指定的对象序列化成 JSON 数据。
///
/// 要序列化的对象。
///
public static string ToJson(object obj)
{
try
{
if (null == obj)
return null;
return JsonConvert.SerializeObject(obj, Formatting.None, _jsonSettings);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 将指定的 JSON 数据反序列化成指定对象。
///
/// 对象类型。
/// JSON 数据。
///
public static T FromJson(string json)
{
try
{
return JsonConvert.DeserializeObject(json, _jsonSettings);
}
catch (Exception ex)
{
throw ex;
}
}
}
}