|
@@ -8,26 +8,49 @@ namespace JCUnityLib
|
|
|
{
|
|
{
|
|
|
public class CustomJson
|
|
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 Dictionary<Type, Func<object, JToken>> importerDict = new Dictionary<Type, Func<object, JToken>>();
|
|
|
|
|
+ public Dictionary<Type, Func<JToken, object>> exporterDict = new Dictionary<Type, Func<JToken, object>>();
|
|
|
|
|
|
|
|
public CustomJson()
|
|
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
|
|
//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));
|
|
|
|
|
|
|
+ importerDict.Add(typeof(short), (o) => JValue.FromObject(o));
|
|
|
|
|
+ importerDict.Add(typeof(int), (o) => JValue.FromObject(o));
|
|
|
|
|
+ importerDict.Add(typeof(long), (o) => JValue.FromObject(o));
|
|
|
|
|
+ importerDict.Add(typeof(float), (o) => JValue.FromObject(o));
|
|
|
|
|
+ importerDict.Add(typeof(double), (o) => JValue.FromObject(o));
|
|
|
|
|
+ importerDict.Add(typeof(bool), (o) => JValue.FromObject(o));
|
|
|
|
|
+ importerDict.Add(typeof(double[][]), (o) => JArray.FromObject(o));
|
|
|
|
|
+ //add exporter
|
|
|
|
|
+ exporterDict.Add(typeof(short), (jt) => jt.ToObject<short>());
|
|
|
|
|
+ exporterDict.Add(typeof(int), (jt) => jt.ToObject<int>());
|
|
|
|
|
+ exporterDict.Add(typeof(long), (jt) => jt.ToObject<long>());
|
|
|
|
|
+ exporterDict.Add(typeof(float), (jt) => jt.ToObject<float>());
|
|
|
|
|
+ exporterDict.Add(typeof(double), (jt) => jt.ToObject<double>());
|
|
|
|
|
+ exporterDict.Add(typeof(bool), (jt) => jt.ToObject<bool>());
|
|
|
|
|
+ exporterDict.Add(typeof(double[][]), (jt) => ((JArray)jt).ToObject<double[][]>());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void InitJsonClass<T>(Func<object, JToken> importer, Func<JToken, object> exporter)
|
|
|
|
|
+ {
|
|
|
|
|
+ Type type = typeof(T);
|
|
|
|
|
+ importerDict.Add(type, importer);
|
|
|
|
|
+ exporterDict.Add(type, exporter);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void InitJsonClass<T>(Func<object> ctor, params string[] props)
|
|
|
|
|
+ {
|
|
|
|
|
+ Type type = typeof(T);
|
|
|
|
|
+ exporterDict.Add(type, (jt) => {
|
|
|
|
|
+ var o = ctor.Invoke();
|
|
|
|
|
+ FetchFieldValues(o, jt, props);
|
|
|
|
|
+ return o;
|
|
|
|
|
+ });
|
|
|
|
|
+ importerDict.Add(type, (o) => {
|
|
|
|
|
+ JToken jt = new JObject();
|
|
|
|
|
+ FetchJsonProperties(jt, o, props);
|
|
|
|
|
+ return jt;
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public T Parse<T>(string text)
|
|
public T Parse<T>(string text)
|
|
@@ -61,10 +84,22 @@ namespace JCUnityLib
|
|
|
public JToken ToJTokenByImporter(object o)
|
|
public JToken ToJTokenByImporter(object o)
|
|
|
{
|
|
{
|
|
|
Type type = o.GetType();
|
|
Type type = o.GetType();
|
|
|
- Func<CustomJson, object, JToken> importer = null;
|
|
|
|
|
|
|
+ Func<object, JToken> importer = null;
|
|
|
importerDict.TryGetValue(type, out importer);
|
|
importerDict.TryGetValue(type, out importer);
|
|
|
- if (importer == null) throw new Exception($"缺少Type<{type.Name}>的Importer");
|
|
|
|
|
- return importer.Invoke(this, o);
|
|
|
|
|
|
|
+ if (importer == null) {
|
|
|
|
|
+ if (IsArray(type) && type.GetArrayRank() == 1) { //是一维数组才会触发
|
|
|
|
|
+ Type elemType = type.GetElementType();
|
|
|
|
|
+ importerDict.TryGetValue(elemType, out importer);
|
|
|
|
|
+ if (importer == null) throw new Exception($"缺少Type<{elemType.Name}>的Importer");
|
|
|
|
|
+ JArray ja = new JArray();
|
|
|
|
|
+ Array arr = (Array) o;
|
|
|
|
|
+ for (int i = 0; i < arr.Length; i++) ja.Add(importer.Invoke(arr.GetValue(i)));
|
|
|
|
|
+ return ja;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new Exception($"缺少Type<{type.Name}>的Importer");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return importer.Invoke(o);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public T ParseByExporter<T>(JToken jt)
|
|
public T ParseByExporter<T>(JToken jt)
|
|
@@ -74,13 +109,25 @@ namespace JCUnityLib
|
|
|
|
|
|
|
|
public object ParseByExporter(JToken jt, Type type)
|
|
public object ParseByExporter(JToken jt, Type type)
|
|
|
{
|
|
{
|
|
|
- Func<CustomJson, JToken, object> exporter = null;
|
|
|
|
|
|
|
+ Func<JToken, object> exporter = null;
|
|
|
exporterDict.TryGetValue(type, out exporter);
|
|
exporterDict.TryGetValue(type, out exporter);
|
|
|
- if (exporter == null) throw new Exception($"缺少Type<{type.Name}>的Exporter");
|
|
|
|
|
- return exporter.Invoke(this, jt);
|
|
|
|
|
|
|
+ if (exporter == null) {
|
|
|
|
|
+ if (IsArray(type) && type.GetArrayRank() == 1) { //是一维数组才会触发
|
|
|
|
|
+ Type elemType = type.GetElementType();
|
|
|
|
|
+ exporterDict.TryGetValue(elemType, out exporter);
|
|
|
|
|
+ if (exporter == null) throw new Exception($"缺少Type<{elemType.Name}>的Exporter");
|
|
|
|
|
+ var ja = (JArray) jt;
|
|
|
|
|
+ var arr = Array.CreateInstance(elemType, ja.Count);
|
|
|
|
|
+ for (int i = 0; i < arr.Length; i++) arr.SetValue(exporter.Invoke(ja[i]), i);
|
|
|
|
|
+ return arr;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new Exception($"缺少Type<{type.Name}>的Exporter");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return exporter.Invoke(jt);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void FetchFieldValue(object target, JToken fromJson, params string[] fieldNames) {
|
|
|
|
|
|
|
+ public void FetchFieldValues(object target, JToken fromJson, params string[] fieldNames) {
|
|
|
Type targetType = target.GetType();
|
|
Type targetType = target.GetType();
|
|
|
foreach (var fieldName in fieldNames)
|
|
foreach (var fieldName in fieldNames)
|
|
|
{
|
|
{
|
|
@@ -95,5 +142,10 @@ namespace JCUnityLib
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public bool IsArray(Type type)
|
|
|
|
|
+ {
|
|
|
|
|
+ return type.BaseType == typeof(Array);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|