CustomJson.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. namespace JCUnityLib
  7. {
  8. public class CustomJson
  9. {
  10. public Dictionary<Type, Func<CustomJson, JToken, object>> exporterDict = new Dictionary<Type, Func<CustomJson, JToken, object>>();
  11. public Dictionary<Type, Func<CustomJson, object, JToken>> importerDict = new Dictionary<Type, Func<CustomJson, object, JToken>>();
  12. public CustomJson()
  13. {
  14. //add exporter
  15. exporterDict.Add(typeof(short), (cs, jt) => jt.Value<short>());
  16. exporterDict.Add(typeof(int), (cs, jt) => jt.Value<int>());
  17. exporterDict.Add(typeof(long), (cs, jt) => jt.Value<long>());
  18. exporterDict.Add(typeof(float), (cs, jt) => jt.Value<float>());
  19. exporterDict.Add(typeof(double), (cs, jt) => jt.Value<double>());
  20. exporterDict.Add(typeof(bool), (cs, jt) => jt.Value<bool>());
  21. //add importer
  22. importerDict.Add(typeof(short), (cs, o) => JValue.FromObject(o));
  23. importerDict.Add(typeof(int), (cs, o) => JValue.FromObject(o));
  24. importerDict.Add(typeof(long), (cs, o) => JValue.FromObject(o));
  25. importerDict.Add(typeof(float), (cs, o) => JValue.FromObject(o));
  26. importerDict.Add(typeof(double), (cs, o) => JValue.FromObject(o));
  27. importerDict.Add(typeof(bool), (cs, o) => JValue.FromObject(o));
  28. importerDict.Add(typeof(double[][]), (cs, o) => JArray.FromObject(o));
  29. }
  30. public T Parse<T>(string text)
  31. {
  32. var jt = JToken.Parse(text);
  33. return (T)ParseByExporter(jt, typeof(T));
  34. }
  35. public string Stringify(object o)
  36. {
  37. return JsonConvert.SerializeObject(ToJTokenByImporter(o));
  38. }
  39. public void FetchJsonProperties(JToken target, object fromObject, params string[] propertyNames)
  40. {
  41. Type fromObjectType = fromObject.GetType();
  42. foreach (var propertyName in propertyNames)
  43. {
  44. FieldInfo targetField = fromObjectType.GetField(propertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
  45. if (targetField == null) {
  46. PropertyInfo propertyInfo = fromObjectType.GetProperty(propertyName);
  47. JToken propertyValue = ToJTokenByImporter(propertyInfo.GetValue(fromObject));
  48. target[propertyName] = propertyValue;
  49. } else {
  50. JToken propertyValue = ToJTokenByImporter(targetField.GetValue(fromObject));
  51. target[propertyName] = propertyValue;
  52. }
  53. }
  54. }
  55. public JToken ToJTokenByImporter(object o)
  56. {
  57. Type type = o.GetType();
  58. Func<CustomJson, object, JToken> importer = null;
  59. importerDict.TryGetValue(type, out importer);
  60. if (importer == null) throw new Exception($"缺少Type<{type.Name}>的Importer");
  61. return importer.Invoke(this, o);
  62. }
  63. public T ParseByExporter<T>(JToken jt)
  64. {
  65. return (T)ParseByExporter(jt, typeof(T));
  66. }
  67. public object ParseByExporter(JToken jt, Type type)
  68. {
  69. Func<CustomJson, JToken, object> exporter = null;
  70. exporterDict.TryGetValue(type, out exporter);
  71. if (exporter == null) throw new Exception($"缺少Type<{type.Name}>的Exporter");
  72. return exporter.Invoke(this, jt);
  73. }
  74. public void FetchFieldValue(object target, JToken fromJson, params string[] fieldNames) {
  75. Type targetType = target.GetType();
  76. foreach (var fieldName in fieldNames)
  77. {
  78. FieldInfo targetField = targetType.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
  79. if (targetField == null) {
  80. PropertyInfo propertyInfo = targetType.GetProperty(fieldName);
  81. object fieldValue = ParseByExporter(fromJson[fieldName], propertyInfo.PropertyType);
  82. propertyInfo.SetValue(target, fieldValue);
  83. } else {
  84. object fieldValue = ParseByExporter(fromJson[fieldName], targetField.FieldType);
  85. targetField.SetValue(target, fieldValue);
  86. }
  87. }
  88. }
  89. }
  90. }