From ec9a8da1ce8663048b5532f1ce1866dde278c45e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 1 Feb 2019 19:28:38 +0900 Subject: [PATCH] replace expression in FormatterExtensionsSerializer with invoke --- .../Editor/Tests/Json/JsonSerializerTests.cs | 2 + .../Scripts/FormatterExtensionsSerializer.cs | 53 ++++++------------ .../GenericInvokeCallFactory.cs | 18 +++++-- .../GenericInvokeCallFactory.g.cs | 54 ++++++++++++++----- 4 files changed, 74 insertions(+), 53 deletions(-) diff --git a/Assets/VRM/UniJSON/Editor/Tests/Json/JsonSerializerTests.cs b/Assets/VRM/UniJSON/Editor/Tests/Json/JsonSerializerTests.cs index 0f497e9fb..50d4aef9f 100644 --- a/Assets/VRM/UniJSON/Editor/Tests/Json/JsonSerializerTests.cs +++ b/Assets/VRM/UniJSON/Editor/Tests/Json/JsonSerializerTests.cs @@ -83,6 +83,8 @@ namespace UniJSON SerializeValue(HogeFuga.Fuga, "1"); SerializeValue(new EnumTest(), "{\"EnumDefault\":0,\"EnumAsInt\":0,\"EnumAsString\":\"Hoge\",\"EnumAsLowerString\":\"hoge\"}"); + + SerializeValue((object)new Point { X = 1 }, "{\"X\":1,\"Y\":0}"); } [Test] diff --git a/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs b/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs index 94abead16..ca5b873c8 100644 --- a/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs +++ b/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Linq.Expressions; using System.Reflection; + namespace UniJSON { public static class FormatterExtensionsSerializer @@ -67,6 +67,11 @@ namespace UniJSON { GenericSerializer.Set(serializer); } + + public static MethodInfo GetMethod(string name) + { + return typeof(FormatterExtensionsSerializer).GetMethod(name); + } } static class GenericSerializer @@ -78,13 +83,8 @@ namespace UniJSON // object if (typeof(T) == typeof(object) && t.GetType() != typeof(object)) { - var self = Expression.Parameter(typeof(IFormatter), "f"); - var arg = Expression.Parameter(t, "value"); - var call = Expression.Call(typeof(FormatterExtensionsSerializer), "SerializeObject", - new Type[] { }, - self, arg); - var lambda = Expression.Lambda(call, self, arg); - return (Action)lambda.Compile(); + var mi = FormatterExtensionsSerializer.GetMethod("SerializeObject"); + return GenericInvokeCallFactory.Create(mi); } try @@ -93,13 +93,7 @@ namespace UniJSON var mi = typeof(IFormatter).GetMethod("Value", new Type[] { t }); if (mi != null) { - // primitives - var self = Expression.Parameter(typeof(IFormatter), "f"); - var arg = Expression.Parameter(t, "value"); - var call = Expression.Call(self, mi, arg); - - var lambda = Expression.Lambda(call, self, arg); - return (Action)lambda.Compile(); + return GenericInvokeCallFactory.Create(mi); } } catch (AmbiguousMatchException) @@ -116,14 +110,8 @@ namespace UniJSON ); if (idictionary != null) { - //var mi = typeof(IFormatter).GetMethod("SerializeDictionary", new Type[] { t }); - var self = Expression.Parameter(typeof(IFormatter), "f"); - var arg = Expression.Parameter(t, "value"); - var call = Expression.Call(typeof(FormatterExtensionsSerializer), "SerializeDictionary", - new Type[] { }, - self, arg); - var lambda = Expression.Lambda(call, self, arg); - return (Action)lambda.Compile(); + var mi = FormatterExtensionsSerializer.GetMethod("SerializeDictionary"); + return GenericInvokeCallFactory.Create(mi); } } @@ -131,13 +119,8 @@ namespace UniJSON // object[] if (t == typeof(object[])) { - var self = Expression.Parameter(typeof(IFormatter), "f"); - var arg = Expression.Parameter(t, "value"); - var call = Expression.Call(typeof(FormatterExtensionsSerializer), "SerializeObjectArray", - new Type[] { }, - self, arg); - var lambda = Expression.Lambda(call, self, arg); - return (Action)lambda.Compile(); + var mi = FormatterExtensionsSerializer.GetMethod("SerializeObjectArray"); + return GenericInvokeCallFactory.Create(mi); } } @@ -149,13 +132,9 @@ namespace UniJSON ); if (ienumerable != null) { - var self = Expression.Parameter(typeof(IFormatter), "f"); - var arg = Expression.Parameter(t, "value"); - var call = Expression.Call(typeof(FormatterExtensionsSerializer), "SerializeArray", - ienumerable.GetGenericArguments(), - self, arg); - var lambda = Expression.Lambda(call, self, arg); - return (Action)lambda.Compile(); + var g = FormatterExtensionsSerializer.GetMethod("SerializeArray"); + var mi = g.MakeGenericMethod(ienumerable.GetGenericArguments()); + return GenericInvokeCallFactory.Create(mi); } } diff --git a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs index cd97aa66a..76cd2528c 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs @@ -56,11 +56,21 @@ namespace UniJSON var source = @" public static Action Create(MethodInfo m) { - Action callback= - (s, $1) => + Action callback = null; + if (m.IsStatic) { - m.Invoke(s, new object[] { $1 }); - }; + callback = (s, $1) => + { + m.Invoke(null, new object[] { s, $1 }); + }; + } + else + { + callback = (s, $1) => + { + m.Invoke(s, new object[] { $1 }); + }; + } return callback; } ".Replace("$0", g).Replace("$1", a); diff --git a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs index be4fc27f3..ce26ca6d5 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs @@ -12,33 +12,63 @@ namespace UniJSON public static Action Create(MethodInfo m) { - Action callback= - (s, a0) => + Action callback = null; + if (m.IsStatic) { - m.Invoke(s, new object[] { a0 }); - }; + callback = (s, a0) => + { + m.Invoke(null, new object[] { s, a0 }); + }; + } + else + { + callback = (s, a0) => + { + m.Invoke(s, new object[] { a0 }); + }; + } return callback; } public static Action Create(MethodInfo m) { - Action callback= - (s, a0, a1) => + Action callback = null; + if (m.IsStatic) { - m.Invoke(s, new object[] { a0, a1 }); - }; + callback = (s, a0, a1) => + { + m.Invoke(null, new object[] { s, a0, a1 }); + }; + } + else + { + callback = (s, a0, a1) => + { + m.Invoke(s, new object[] { a0, a1 }); + }; + } return callback; } public static Action Create(MethodInfo m) { - Action callback= - (s, a0, a1, a2) => + Action callback = null; + if (m.IsStatic) { - m.Invoke(s, new object[] { a0, a1, a2 }); - }; + callback = (s, a0, a1, a2) => + { + m.Invoke(null, new object[] { s, a0, a1, a2 }); + }; + } + else + { + callback = (s, a0, a1, a2) => + { + m.Invoke(s, new object[] { a0, a1, a2 }); + }; + } return callback; }