12345678910111213141516171819202122232425262728293031 |
- using Newtonsoft.Json;
- using System.ComponentModel;
- namespace KYFramework
- {
- public static class JsonHelper
- {
- public static string ToJson(object obj, Formatting formatting = Formatting.Indented)
- {
- return JsonConvert.SerializeObject(obj,formatting);
- }
- public static T FromJson<T>(string str)
- {
- T t = JsonConvert.DeserializeObject<T>(str);
-
- ISupportInitialize iSupportInitialize = t as ISupportInitialize;
- if (iSupportInitialize == null)
- {
- return t;
- }
- iSupportInitialize.EndInit();
- return t;
- }
- public static T Clone<T>(T t)
- {
- return FromJson<T>(ToJson(t));
- }
- }
- }
|