| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using System;
- using System.Reflection;
- using System.Collections.Generic;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- namespace JCUnityLib
- {
- public class CustomJson
- {
- public Dictionary<Type, Func<CustomJson, JToken, object>> exporterDict = new Dictionary<Type, Func<CustomJson, JToken, object>>();
- public Dictionary<Type, Func<CustomJson, object, JToken>> importerDict = new Dictionary<Type, Func<CustomJson, object, JToken>>();
- public CustomJson()
- {
- //add exporter
- exporterDict.Add(typeof(short), (cs, jt) => jt.Value<short>());
- exporterDict.Add(typeof(int), (cs, jt) => jt.Value<int>());
- exporterDict.Add(typeof(long), (cs, jt) => jt.Value<long>());
- exporterDict.Add(typeof(float), (cs, jt) => jt.Value<float>());
- exporterDict.Add(typeof(double), (cs, jt) => jt.Value<double>());
- exporterDict.Add(typeof(bool), (cs, jt) => jt.Value<bool>());
- //add importer
- importerDict.Add(typeof(short), (cs, o) => JValue.FromObject(o));
- importerDict.Add(typeof(int), (cs, o) => JValue.FromObject(o));
- importerDict.Add(typeof(long), (cs, o) => JValue.FromObject(o));
- importerDict.Add(typeof(float), (cs, o) => JValue.FromObject(o));
- importerDict.Add(typeof(double), (cs, o) => JValue.FromObject(o));
- importerDict.Add(typeof(bool), (cs, o) => JValue.FromObject(o));
- importerDict.Add(typeof(double[][]), (cs, o) => JArray.FromObject(o));
- }
- public T Parse<T>(string text)
- {
- var jt = JToken.Parse(text);
- return (T)ParseByExporter(jt, typeof(T));
- }
- public string Stringify(object o)
- {
- return JsonConvert.SerializeObject(ToJTokenByImporter(o));
- }
- public void FetchJsonProperties(JToken target, object fromObject, params string[] propertyNames)
- {
- Type fromObjectType = fromObject.GetType();
- foreach (var propertyName in propertyNames)
- {
- FieldInfo targetField = fromObjectType.GetField(propertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
- if (targetField == null) {
- PropertyInfo propertyInfo = fromObjectType.GetProperty(propertyName);
- JToken propertyValue = ToJTokenByImporter(propertyInfo.GetValue(fromObject));
- target[propertyName] = propertyValue;
- } else {
- JToken propertyValue = ToJTokenByImporter(targetField.GetValue(fromObject));
- target[propertyName] = propertyValue;
- }
- }
- }
- public JToken ToJTokenByImporter(object o)
- {
- Type type = o.GetType();
- Func<CustomJson, object, JToken> importer = null;
- importerDict.TryGetValue(type, out importer);
- if (importer == null) throw new Exception($"缺少Type<{type.Name}>的Importer");
- return importer.Invoke(this, o);
- }
- public T ParseByExporter<T>(JToken jt)
- {
- return (T)ParseByExporter(jt, typeof(T));
- }
- public object ParseByExporter(JToken jt, Type type)
- {
- Func<CustomJson, JToken, object> exporter = null;
- exporterDict.TryGetValue(type, out exporter);
- if (exporter == null) throw new Exception($"缺少Type<{type.Name}>的Exporter");
- return exporter.Invoke(this, jt);
- }
- public void FetchFieldValue(object target, JToken fromJson, params string[] fieldNames) {
- Type targetType = target.GetType();
- foreach (var fieldName in fieldNames)
- {
- FieldInfo targetField = targetType.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
- if (targetField == null) {
- PropertyInfo propertyInfo = targetType.GetProperty(fieldName);
- object fieldValue = ParseByExporter(fromJson[fieldName], propertyInfo.PropertyType);
- propertyInfo.SetValue(target, fieldValue);
- } else {
- object fieldValue = ParseByExporter(fromJson[fieldName], targetField.FieldType);
- targetField.SetValue(target, fieldValue);
- }
- }
- }
- }
- }
|