lvjincheng пре 3 година
родитељ
комит
80cd7a892b

+ 58 - 148
Assets/BowArrow/Scripts/Bluetooth/New/AttitudeJson.cs

@@ -8,154 +8,64 @@ public class AttitudeJson : JCUnityLib.CustomJson
 {
     public AttitudeJson() : base()
     {
-        InitAttitudeImporter();
-        InitAttitudeExporter();
-    }
-
-    void InitAttitudeImporter()
-    {
-        importerDict.Add(typeof(_9AxisPreProcessor), (cs, o) => {
-            JToken jt = new JObject();
-            cs.FetchJsonProperties(jt, o,
-                "AccByteIndex", 
-                "GyrByteIndex", 
-                "MagByteIndex",
-                "ByteToAcc", 
-                "ByteToGyr", 
-                "ByteToMag",
-                "GyrCalibrater", 
-                "MagCalibrater"
-            );
-            return jt; 
-        });
-        importerDict.Add(typeof(MagnetometerAutoCalibrater), (cs, o) => {
-            JToken jt = new JObject();
-            cs.FetchJsonProperties(jt, o, 
-                "CountPerLength", 
-                "EllipsoidFitting", 
-                "FitCountLeft",
-                "FitThreshold", 
-                "LastTimestamp", 
-                "Lock", 
-                "MaxCount",
-                "NewBlock", 
-                "NewBlockAccumulation", 
-                "Variance",
-                "VectorByBlock"
-            );
-            return jt;
-        });
-        importerDict.Add(typeof(EllipsoidFitting), (cs, o) => {
-            JToken jt = new JObject();
-            cs.FetchJsonProperties(jt, o, "Center", "CorrectMatrixArray", "Radius");
-            return jt;
-        });
-        importerDict.Add(typeof(Dictionary<Vector<int>, Vector<double>>), (cs, o) => JToken.FromObject(o));
-        importerDict.Add(typeof(MeanMaintainer<Vector<double>>), (cs, o) => {
-            JToken jt = new JObject();
-            cs.FetchJsonProperties(jt, o, "Count", "Mean");
-            return jt; 
-        });
-        importerDict.Add(typeof(ByteToShortByVariance[]), (cs, o) => {
-            var oArr = (ByteToShortByVariance[]) o;
-            var ja = new JArray();
-            for (int i = 0; i < oArr.Length; i++) 
-            {
-                var jo = new JObject();
-                cs.FetchJsonProperties(jo, oArr[i], "Count", "Reverse", "Short", "ThresholdRate");
-                ja.Add(jo);
-            }
-            return ja;
-        });
-        importerDict.Add(typeof(ByteToShorts), (cs, o) => {
-            JToken jt = new JObject();
-            cs.FetchJsonProperties(jt, o, "ByteToShort");
-            return jt; 
-        });
-        importerDict.Add(typeof(Vector<int>), (cs, o) => JToken.FromObject(o));
-        importerDict.Add(typeof(Vector<double>), (cs, o) => JToken.FromObject(o));
-    }
-
-    void InitAttitudeExporter()
-    {
-        exporterDict.Add(typeof(_9AxisPreProcessor), (cs, jt) => {
-            var o = new _9AxisPreProcessor();
-            FetchFieldValue(o, jt, 
-                "AccByteIndex", 
-                "GyrByteIndex", 
-                "MagByteIndex", 
-                "ByteToAcc", 
-                "ByteToGyr", 
-                "ByteToMag", 
-                "GyrCalibrater", 
-                "MagCalibrater"
-            );
-            return o; 
-        });
-        exporterDict.Add(typeof(MagnetometerAutoCalibrater), (cs, jt) => {
-            var o = new MagnetometerAutoCalibrater();
-            cs.FetchFieldValue(o, jt, 
-                "CountPerLength", 
-                "EllipsoidFitting",
-                "FitCountLeft",
-                "FitThreshold",
-                "LastTimestamp",
-                "Lock",
-                "MaxCount",
-                "NewBlock",
-                "NewBlockAccumulation",
-                "Variance",
-                "VectorByBlock"
-            );
-            return o;
-        });
-        exporterDict.Add(typeof(EllipsoidFitting), (cs, jt) => {
-            var Center = cs.ParseByExporter<Vector<double>>(jt["Center"]);
-            var CorrectMatrix = cs.ParseByExporter
-                <MathNet.Numerics.LinearAlgebra.Matrix<double>>(jt["CorrectMatrixArray"]);
-            var Radius = cs.ParseByExporter<Vector<double>>(jt["Radius"]);
-            return new EllipsoidFitting(Center, CorrectMatrix, Radius);
-        });
-        exporterDict.Add(typeof(Dictionary<Vector<int>, Vector<double>>), (cs, jt) => {
-            var o = new Dictionary<Vector<int>, Vector<double>>();
-            foreach (JProperty item in jt)
-                o[cs.ParseByExporter<Vector<int>>(JArray.Parse(item.Name))] 
-                    = cs.ParseByExporter<Vector<double>>(item.Value);
-            return o;
-        });
-        exporterDict.Add(typeof(MeanMaintainer<Vector<double>>), (cs, jt) => {
-            var o = new MeanMaintainer<Vector<double>>();
-            cs.FetchFieldValue(o, jt, "Count", "Mean");
-            return o; 
-        });
-        exporterDict.Add(typeof(ByteToShortByVariance[]), (cs, jt) => {
-            var o = new ByteToShortByVariance[3];
-            var arr = (JArray) jt;
-            for (int i = 0; i < o.Length; i++) 
-            {
-                var item = arr[i];
-                var byteToShortByVariance = new ByteToShortByVariance();
-                cs.FetchFieldValue(byteToShortByVariance, item, "Count", "Reverse", "Short", "ThresholdRate");
-                o[i] = byteToShortByVariance;
+        InitJsonClass<_9AxisPreProcessor>(() => new _9AxisPreProcessor(),
+            "AccByteIndex", 
+            "GyrByteIndex", 
+            "MagByteIndex",
+            "ByteToAcc", 
+            "ByteToGyr", 
+            "ByteToMag",
+            "GyrCalibrater", 
+            "MagCalibrater"
+        );
+        InitJsonClass<MagnetometerAutoCalibrater>(() => new MagnetometerAutoCalibrater(),
+            "CountPerLength", 
+            "EllipsoidFitting", 
+            "FitCountLeft",
+            "FitThreshold", 
+            "LastTimestamp", 
+            "Lock", 
+            "MaxCount",
+            "NewBlock", 
+            "NewBlockAccumulation", 
+            "Variance",
+            "VectorByBlock"
+        );
+        InitJsonClass<EllipsoidFitting>(() => new EllipsoidFitting(default, default, default),
+            "Center", 
+            "CorrectMatrixArray", 
+            "Radius"
+        );
+        InitJsonClass<ByteToShorts>(() => new ByteToShorts(3),
+            "ByteToShort"
+        );
+        InitJsonClass<MeanMaintainer<Vector<double>>>(() => new MeanMaintainer<Vector<double>>(),
+            "Count",
+            "Mean"
+        );
+        InitJsonClass<ByteToShortByVariance>(() => new ByteToShortByVariance(),
+            "Count", 
+            "Reverse", 
+            "Short", 
+            "ThresholdRate"
+        );
+        InitJsonClass<Dictionary<Vector<int>, Vector<double>>>(
+            o => JToken.FromObject(o),
+            jt => {
+                var o = new Dictionary<Vector<int>, Vector<double>>();
+                foreach (JProperty item in jt)
+                    o[ParseByExporter<Vector<int>>(JArray.Parse(item.Name))] 
+                        = ParseByExporter<Vector<double>>(item.Value);
+                return o;
             }
-            return o;
-        });
-        exporterDict.Add(typeof(ByteToShorts), (cs, jt) => {
-            ByteToShorts o = new ByteToShorts(3);
-            cs.FetchFieldValue(o, jt, "ByteToShort");
-            return o; 
-        });
-        exporterDict.Add(typeof(MathNet.Numerics.LinearAlgebra.Matrix<double>), (cs, jt) => {
-            JArray arr = (JArray) jt;
-            int rcCount = arr.Count;
-            MathNet.Numerics.LinearAlgebra.Matrix<double> matrix 
-                = MathNet.Numerics.LinearAlgebra.CreateMatrix.Dense<double>(rcCount, rcCount);
-            for (var i = 0; i < rcCount; ++i)
-                for (var j = 0; j < rcCount; ++j)
-                    matrix[i, j] = arr[i][j].Value<double>();
-            return matrix;
-        });
-        exporterDict.Add(typeof(Vector<int>), (cs, jt) => new Vector<int>(jt.ToObject<int[]>()));
-        exporterDict.Add(typeof(Vector<double>), (cs, jt) => new Vector<double>(jt.ToObject<double[]>()));
+        );
+        InitJsonClass<Vector<int>>(
+            o => JToken.FromObject(o),
+            jt => new Vector<int>(jt.ToObject<int[]>())
+        );
+        InitJsonClass<Vector<double>>(
+            o => JToken.FromObject(o),
+            jt => new Vector<double>(jt.ToObject<double[]>())
+        );
     }
 }

+ 75 - 23
Assets/ThirdAssets/JCUnityLib/CustomJson.cs

@@ -8,26 +8,49 @@ 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 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()
         {
-            //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));
+            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)
@@ -61,10 +84,22 @@ namespace JCUnityLib
         public JToken ToJTokenByImporter(object o)
         {
             Type type = o.GetType();
-            Func<CustomJson, object, JToken> importer = null;
+            Func<object, JToken> importer = null;
             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)
@@ -74,13 +109,25 @@ namespace JCUnityLib
 
         public object ParseByExporter(JToken jt, Type type)
         {
-            Func<CustomJson, JToken, object> exporter = null;
+            Func<JToken, object> exporter = null;
             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();
             foreach (var fieldName in fieldNames)
             {
@@ -95,5 +142,10 @@ namespace JCUnityLib
                 }
             }
         }
+
+        public bool IsArray(Type type)
+        {
+            return type.BaseType == typeof(Array);
+        }
     }
 }