47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
|
|
|||
|
using Newtonsoft.Json;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace GraphicsStudy
|
|||
|
{
|
|||
|
public static class JsonUtils
|
|||
|
{
|
|||
|
private static readonly List<Newtonsoft.Json.JsonConverter> JsonConverters = new List<Newtonsoft.Json.JsonConverter>()
|
|||
|
{
|
|||
|
|
|||
|
new Json.XYZConverter(),
|
|||
|
new Json.LineConverter(),
|
|||
|
new Json.TransformConvert(),
|
|||
|
new Json.ElementIdConverter()
|
|||
|
};
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// JSON序列化
|
|||
|
/// </summary>
|
|||
|
/// <param name="obj"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string ObjectToJson(object obj)
|
|||
|
{
|
|||
|
JsonSerializerSettings jsSettings = new JsonSerializerSettings();
|
|||
|
jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|||
|
return JsonConvert.SerializeObject(obj/*, JsonConverters.ToArray()*/);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// JSON反序列化
|
|||
|
/// </summary>
|
|||
|
/// <param name="jsonStr"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static T JsonToObject<T>(string jsonStr) where T : class
|
|||
|
{
|
|||
|
return JsonConvert.DeserializeObject<T>(jsonStr/*, JsonConverters.ToArray()*/);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|