未使用の Generics 関数を削除。関連するテストの修正

This commit is contained in:
ousttrue 2021-02-09 15:32:27 +09:00
parent c2ffda92dc
commit cdc9d73d36
25 changed files with 74 additions and 1605 deletions

View File

@ -1,63 +0,0 @@
version: 2
jobs:
update_unity_repo:
docker:
- image: circleci/buildpack-deps:latest
working_directory: ~/repo
environment:
UniJSON_unity_REPO: "git@github.com:ousttrue/UniJSON-unity.git"
steps:
- add_ssh_keys:
fingerprints:
- "5b:16:4a:8a:c9:a5:2c:80:37:d4:a5:b8:1b:0d:60:27"
- run:
name: Avoid hosts unknown for github
command: echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
- run:
name: Clone UniJSON-unity (NOT UniJSON)
command: |
git clone ${UniJSON_unity_REPO}
cd UniJSON-unity
git submodule update --init
- run:
name: Update submodule(UniJSON) in UniJSON-unity
command: |
cd UniJSON-unity/Assets/UniJSON
git fetch && git checkout --force ${CIRCLE_SHA1}
- run:
command: |
cd UniJSON-unity
git config user.email "yutopp+unijson-unity@users.noreply.github.com"
git config user.name "unijson-unity job"
- run:
command: |
cd UniJSON-unity
git add Assets/UniJSON
git commit -m "[skip ci] Checkout UniJSON (${CIRCLE_SHA1})"
- run:
command: |
cd UniJSON-unity
git status
when: on_fail
- run:
command: |
cd UniJSON-unity
git push origin master
workflows:
version: 2
build_test_deploy:
jobs:
- update_unity_repo:
filters:
branches:
only: master

View File

@ -66,50 +66,5 @@ namespace UniJSON
var method = typeof(FormatterExtensions).GetMethod("Serialize");
return method.MakeGenericMethod(typeof(T));
}
//
// https://stackoverflow.com/questions/238765/given-a-type-expressiontype-memberaccess-how-do-i-get-the-field-value
//
public static void KeyValue<T>(this IFormatter f, Expression<Func<T>> expression)
{
// lambda body
var lambdaBody = (MemberExpression)expression.Body;
if (lambdaBody.Expression.NodeType == ExpressionType.Constant)
{
//
// KeyValue(() => Field);
//
var constant = (ConstantExpression)lambdaBody.Expression;
var field = (FieldInfo)lambdaBody.Member;
var value = field.GetValue(constant.Value);
if (value != null)
{
f.Key(lambdaBody.Member.Name);
f.Serialize(value);
}
}
else
{
//
// KeyValue(() => p.Field);
//
var capture = (MemberExpression)lambdaBody.Expression;
var captureVariable = (ConstantExpression)capture.Expression;
var captureObj = captureVariable.Value;
var captureField = (FieldInfo)capture.Member;
var captureValue = captureField.GetValue(captureObj);
var field = (FieldInfo)lambdaBody.Member;
var value = field.GetValue(captureValue);
if (value != null)
{
f.Key(field.Name);
f.Serialize(value);
}
}
}
}
}

View File

@ -1,162 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace UniJSON
{
public static class FormatterExtensionsSerializer
{
public static void SerializeDictionary(this IFormatter f, IDictionary<string, object> dictionary)
{
f.BeginMap(dictionary.Count);
foreach (var kv in dictionary)
{
f.Key(kv.Key);
f.SerializeObject(kv.Value);
}
f.EndMap();
}
public static void SerializeArray<T>(this IFormatter f, IEnumerable<T> values)
{
f.BeginList(values.Count());
foreach (var value in values)
{
f.Serialize(value);
}
f.EndList();
}
public static void SerializeObjectArray(this IFormatter f, object[] array)
{
f.BeginList(array.Length);
foreach (var x in array)
{
f.SerializeObject(x);
}
f.EndList();
}
public static void SerializeObject(this IFormatter f, object value)
{
if (value == null)
{
f.Null();
}
else
{
typeof(FormatterExtensionsSerializer).GetMethod("Serialize")
.MakeGenericMethod(value.GetType()).Invoke(null, new object[] { f, value });
}
}
public static void Serialize<T>(this IFormatter f, T arg)
{
if (arg == null)
{
f.Null();
return;
}
GenericSerializer<T>.Serialize(f, arg);
}
public static void SetCustomSerializer<T>(Action<IFormatter, T> serializer)
{
GenericSerializer<T>.Set(serializer);
}
public static MethodInfo GetMethod(string name)
{
return typeof(FormatterExtensionsSerializer).GetMethod(name);
}
}
static class GenericSerializer<T>
{
delegate void Serializer(IFormatter f, T t);
static Action<IFormatter, T> GetSerializer()
{
var t = typeof(T);
// object
if (typeof(T) == typeof(object) && t.GetType() != typeof(object))
{
var mi = FormatterExtensionsSerializer.GetMethod("SerializeObject");
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
try
{
// primitive
var mi = typeof(IFormatter).GetMethod("Value", new Type[] { t });
if (mi != null)
{
return GenericInvokeCallFactory.OpenAction<IFormatter, T>(mi);
}
}
catch (AmbiguousMatchException)
{
// do nothing
}
{
// dictionary
var idictionary = t.GetInterfaces().FirstOrDefault(x =>
x.IsGenericType
&& x.GetGenericTypeDefinition() == typeof(IDictionary<,>)
&& x.GetGenericArguments()[0] == typeof(string)
);
if (idictionary != null)
{
var mi = FormatterExtensionsSerializer.GetMethod("SerializeDictionary");
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
}
{
// object[]
if (t == typeof(object[]))
{
var mi = FormatterExtensionsSerializer.GetMethod("SerializeObjectArray");
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
}
{
// list
var ienumerable = t.GetInterfaces().FirstOrDefault(x =>
x.IsGenericType
&& x.GetGenericTypeDefinition() == typeof(IEnumerable<>)
);
if (ienumerable != null)
{
var g = FormatterExtensionsSerializer.GetMethod("SerializeArray");
var mi = g.MakeGenericMethod(ienumerable.GetGenericArguments());
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
}
throw new NotImplementedException();
}
static Serializer s_serializer;
public static void Set(Action<IFormatter, T> serializer)
{
s_serializer = new Serializer(serializer);
}
public static void Serialize(IFormatter f, T t)
{
if (s_serializer == null)
{
s_serializer = new Serializer(GetSerializer());
}
s_serializer(f, t);
}
}
}

View File

@ -1,12 +0,0 @@
fileFormatVersion: 2
guid: 11342b30ed6802d4ebccbcf85663c6a0
timeCreated: 1543520703
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,9 +0,0 @@
fileFormatVersion: 2
guid: 1a508fceebf4ef64ca956059360b2467
folderAsset: yes
timeCreated: 1548656168
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,119 +0,0 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
#endif
namespace UniJSON
{
public static partial class GenericExpressionCallFactory
{
#if UNITY_EDITOR && VRM_DEVELOP
const int NET35MAX = 4;
const int ARGS = 6;
const string GENERATE_PATH = "UniGLTF/UniJSON/Scripts/GenericCallUtility/GenericExpressionCallFactory.g.cs";
static System.Collections.Generic.IEnumerable<string> GetArgs(string prefix, int n)
{
for (int i = 0; i < n; ++i)
{
yield return prefix + i;
}
}
[MenuItem("UniGLTF/UniJSON/Generate GenericExpressionCallFactory")]
static void Generate()
{
var sb = new StringBuilder();
using (var w = new StringWriter(sb))
{
w.WriteLine(@"
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace UniJSON
{
public static partial class GenericExpressionCallFactory
{
");
// Create
for (int i = 1; i <= ARGS && i<NET35MAX; ++i)
{
var g = String.Join(", ", GetArgs("A", i).ToArray());
var a = String.Join(", ", GetArgs("a", i).ToArray());
var source = @"
public static Action<S, $0> Create<S, $0>(MethodInfo m)
{
var self = Expression.Parameter(m.DeclaringType, m.Name);
var args = m.GetParameters().Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray();
var call = Expression.Call(self, m, args);
return
(Action<S, $0>)Expression.Lambda(call, new[] { self }.Concat(args).ToArray()).Compile();
}
".Replace("$0", g).Replace("$1", a);
w.WriteLine(source);
}
// CreateWithThis
for (int i = 1; i <= ARGS && i<=NET35MAX; ++i)
{
var g = String.Join(", ", GetArgs("A", i).ToArray());
var source = @"
public static Action<$0> CreateWithThis<S, $0>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
if (instance != null)
{
throw new ArgumentException();
}
}
else
{
if (instance == null)
{
throw new ArgumentNullException();
}
}
var self = Expression.Constant(instance, typeof(S)); // thisを定数化
var args = m.GetParameters().Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray();
MethodCallExpression call;
if (m.IsStatic)
{
call = Expression.Call(m, args);
}
else
{
call = Expression.Call(self, m, args);
}
return
(Action<$0>)Expression.Lambda(call, args).Compile();
}
".Replace("$0", g);
w.WriteLine(source);
}
w.WriteLine(@"
}
}
");
}
var path = Path.GetFullPath(Application.dataPath + GENERATE_PATH).Replace("\\", "/");
File.WriteAllText(path, sb.ToString().Replace("\r\n", "\n"));
}
#endif
}
}

View File

@ -1,178 +0,0 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace UniJSON
{
public static partial class GenericExpressionCallFactory
{
public static Action<S, A0> Create<S, A0>(MethodInfo m)
{
var self = Expression.Parameter(m.DeclaringType, m.Name);
var args = m.GetParameters().Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray();
var call = Expression.Call(self, m, args);
return
(Action<S, A0>)Expression.Lambda(call, new[] { self }.Concat(args).ToArray()).Compile();
}
public static Action<S, A0, A1> Create<S, A0, A1>(MethodInfo m)
{
var self = Expression.Parameter(m.DeclaringType, m.Name);
var args = m.GetParameters().Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray();
var call = Expression.Call(self, m, args);
return
(Action<S, A0, A1>)Expression.Lambda(call, new[] { self }.Concat(args).ToArray()).Compile();
}
public static Action<S, A0, A1, A2> Create<S, A0, A1, A2>(MethodInfo m)
{
var self = Expression.Parameter(m.DeclaringType, m.Name);
var args = m.GetParameters().Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray();
var call = Expression.Call(self, m, args);
return
(Action<S, A0, A1, A2>)Expression.Lambda(call, new[] { self }.Concat(args).ToArray()).Compile();
}
public static Action<A0> CreateWithThis<S, A0>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
if (instance != null)
{
throw new ArgumentException();
}
}
else
{
if (instance == null)
{
throw new ArgumentNullException();
}
}
var self = Expression.Constant(instance, typeof(S)); // thisを定数化
var args = m.GetParameters().Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray();
MethodCallExpression call;
if (m.IsStatic)
{
call = Expression.Call(m, args);
}
else
{
call = Expression.Call(self, m, args);
}
return
(Action<A0>)Expression.Lambda(call, args).Compile();
}
public static Action<A0, A1> CreateWithThis<S, A0, A1>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
if (instance != null)
{
throw new ArgumentException();
}
}
else
{
if (instance == null)
{
throw new ArgumentNullException();
}
}
var self = Expression.Constant(instance, typeof(S)); // thisを定数化
var args = m.GetParameters().Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray();
MethodCallExpression call;
if (m.IsStatic)
{
call = Expression.Call(m, args);
}
else
{
call = Expression.Call(self, m, args);
}
return
(Action<A0, A1>)Expression.Lambda(call, args).Compile();
}
public static Action<A0, A1, A2> CreateWithThis<S, A0, A1, A2>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
if (instance != null)
{
throw new ArgumentException();
}
}
else
{
if (instance == null)
{
throw new ArgumentNullException();
}
}
var self = Expression.Constant(instance, typeof(S)); // thisを定数化
var args = m.GetParameters().Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray();
MethodCallExpression call;
if (m.IsStatic)
{
call = Expression.Call(m, args);
}
else
{
call = Expression.Call(self, m, args);
}
return
(Action<A0, A1, A2>)Expression.Lambda(call, args).Compile();
}
public static Action<A0, A1, A2, A3> CreateWithThis<S, A0, A1, A2, A3>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
if (instance != null)
{
throw new ArgumentException();
}
}
else
{
if (instance == null)
{
throw new ArgumentNullException();
}
}
var self = Expression.Constant(instance, typeof(S)); // thisを定数化
var args = m.GetParameters().Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray();
MethodCallExpression call;
if (m.IsStatic)
{
call = Expression.Call(m, args);
}
else
{
call = Expression.Call(self, m, args);
}
return
(Action<A0, A1, A2, A3>)Expression.Lambda(call, args).Compile();
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: cf88d89ec3acdbf4eb44842ed15aaff4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,274 +0,0 @@
using System;
using System.Reflection;
#if UNITY_EDITOR && VRM_DEVELOP
using UnityEngine;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
#endif
namespace UniJSON
{
/// <summary>
/// MethodInfoからDelegateを作成する
///
/// * StaticAction/Func StaticMethod呼び出し
/// * OpenAction/Func 第1引数にthisを受けるメソッド呼び出し
/// * BindAction/Func thisを内部に保持したメソッド呼び出し
///
/// </summary>
public static partial class GenericInvokeCallFactory
{
#if UNITY_EDITOR && VRM_DEVELOP
const int NET35MAX = 4;
const int ARGS = 6;
const string GENERATE_PATH = "/UniGLTF/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs";
static System.Collections.Generic.IEnumerable<string> GetArgs(string prefix, int n)
{
for (int i = 0; i < n; ++i)
{
yield return prefix + i;
}
}
[MenuItem("UniGLTF/UniJSON/Generate GenericInvokeCallFactory")]
static void Generate()
{
var sb = new StringBuilder();
using (var w = new StringWriter(sb))
{
w.WriteLine(@"
using System;
using System.Reflection;
namespace UniJSON
{
public static partial class GenericInvokeCallFactory
{
");
// StaticAction
w.WriteLine("//////////// StaticAction");
for (int i = 1; i <= ARGS && i <= NET35MAX; ++i)
{
var g = String.Join(", ", GetArgs("A", i).ToArray());
var a = String.Join(", ", GetArgs("a", i).ToArray());
var source = @"
public static Action<$0> StaticAction<$0>(MethodInfo m)
{
if (!m.IsStatic)
{
throw new ArgumentException(string.Format(""{0} is not static"", m));
}
return (Action<$0>)Delegate.CreateDelegate(typeof(Action<$0>), null, m);
}
".Replace("$0", g).Replace("$1", a);
w.WriteLine(source);
}
// OpenAction
w.WriteLine("//////////// OpenAction");
for (int i = 1; i <= ARGS && i < NET35MAX; ++i)
{
var g = String.Join(", ", GetArgs("A", i).ToArray());
var a = String.Join(", ", GetArgs("a", i).ToArray());
var source = @"
public static Action<S, $0> OpenAction<S, $0>(MethodInfo m)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format(""{0} is static"", m));
}
return (Action<S, $0>)Delegate.CreateDelegate(typeof(Action<S, $0>), m);
}
".Replace("$0", g).Replace("$1", a);
w.WriteLine(source);
}
// BindAction
w.WriteLine("//////////// BindAction");
for (int i = 1; i <= ARGS && i <= NET35MAX; ++i)
{
var g = String.Join(", ", GetArgs("A", i).ToArray());
var a = String.Join(", ", GetArgs("a", i).ToArray());
var source = @"
public static Action<$0> BindAction<S, $0>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format(""{0} is static"", m));
}
return (Action<$0>)Delegate.CreateDelegate(typeof(Action<$0>), instance, m);
}
".Replace("$0", g).Replace("$1", a);
w.WriteLine(source);
}
// StaticFunc
w.WriteLine("//////////// StaticFunc");
for (int i = 1; i <= ARGS && i <= NET35MAX; ++i)
{
var g = String.Join(", ", GetArgs("A", i).ToArray());
var a = String.Join(", ", GetArgs("a", i).ToArray());
var source = @"
public static Func<$0, T> StaticFunc<$0, T>(MethodInfo m)
{
if (!m.IsStatic)
{
throw new ArgumentException(string.Format(""{0} is not static"", m));
}
return (Func<$0, T>)Delegate.CreateDelegate(typeof(Func<$0, T>), null, m);
}
".Replace("$0", g).Replace("$1", a);
w.WriteLine(source);
}
// OpenFunc
w.WriteLine("//////////// OpenFunc");
for (int i = 1; i <= ARGS && i < NET35MAX; ++i)
{
var g = String.Join(", ", GetArgs("A", i).ToArray());
var a = String.Join(", ", GetArgs("a", i).ToArray());
var source = @"
public static Func<S, $0, T> OpenFunc<S, $0, T>(MethodInfo m)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format(""{0} is static"", m));
}
return (Func<S, $0, T>)Delegate.CreateDelegate(typeof(Func<S, $0, T>), m);
}
".Replace("$0", g).Replace("$1", a);
w.WriteLine(source);
}
// BindFunc
w.WriteLine("//////////// BindFunc");
for (int i = 1; i <= ARGS && i <= NET35MAX; ++i)
{
var g = String.Join(", ", GetArgs("A", i).ToArray());
var a = String.Join(", ", GetArgs("a", i).ToArray());
var source = @"
public static Func<$0, T> BindFunc<S, $0, T>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format(""{0} is static"", m));
}
return (Func<$0, T>)Delegate.CreateDelegate(typeof(Func<$0, T>), instance, m);
}
".Replace("$0", g).Replace("$1", a);
w.WriteLine(source);
}
w.WriteLine(@"
}
}
");
}
var path = Path.GetFullPath(Application.dataPath + GENERATE_PATH).Replace("\\", "/");
File.WriteAllText(path, sb.ToString().Replace("\r\n", "\n"));
}
#endif
#region Action without arguments
public static Action StaticAction(MethodInfo m)
{
if (!m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is not static", m));
}
return (Action)Delegate.CreateDelegate(typeof(Action), null, m);
}
public static Action<S> OpenAction<S>(MethodInfo m)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (s) =>
{
m.Invoke(s, new object[] { });
};
}
public static Action BindAction<S>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return () =>
{
m.Invoke(instance, new object[] { });
};
}
#endregion
#region Func without arguments
public static Func<T> StaticFunc<T>(MethodInfo m)
{
if (!m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is not static", m));
}
return () => (T)m.Invoke(null, new object[] { });
}
public static Func<S, T> OpenFunc<S, T>(MethodInfo m)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (s) => (T)m.Invoke(s, new object[] { });
}
public static Func<T> BindFunc<S, T>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return () => (T)m.Invoke(instance, new object[] { });
}
#endregion
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: b4bdb47bf29e1214a9e3da9b4ae9f31e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,262 +0,0 @@
using System;
using System.Reflection;
namespace UniJSON
{
public static partial class GenericInvokeCallFactory
{
//////////// StaticAction
public static Action<A0> StaticAction<A0>(MethodInfo m)
{
if (!m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is not static", m));
}
return (Action<A0>)Delegate.CreateDelegate(typeof(Action<A0>), null, m);
}
public static Action<A0, A1> StaticAction<A0, A1>(MethodInfo m)
{
if (!m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is not static", m));
}
return (Action<A0, A1>)Delegate.CreateDelegate(typeof(Action<A0, A1>), null, m);
}
public static Action<A0, A1, A2> StaticAction<A0, A1, A2>(MethodInfo m)
{
if (!m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is not static", m));
}
return (Action<A0, A1, A2>)Delegate.CreateDelegate(typeof(Action<A0, A1, A2>), null, m);
}
public static Action<A0, A1, A2, A3> StaticAction<A0, A1, A2, A3>(MethodInfo m)
{
if (!m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is not static", m));
}
return (Action<A0, A1, A2, A3>)Delegate.CreateDelegate(typeof(Action<A0, A1, A2, A3>), null, m);
}
//////////// OpenAction
public static Action<S, A0> OpenAction<S, A0>(MethodInfo m)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (Action<S, A0>)Delegate.CreateDelegate(typeof(Action<S, A0>), m);
}
public static Action<S, A0, A1> OpenAction<S, A0, A1>(MethodInfo m)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (Action<S, A0, A1>)Delegate.CreateDelegate(typeof(Action<S, A0, A1>), m);
}
public static Action<S, A0, A1, A2> OpenAction<S, A0, A1, A2>(MethodInfo m)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (Action<S, A0, A1, A2>)Delegate.CreateDelegate(typeof(Action<S, A0, A1, A2>), m);
}
//////////// BindAction
public static Action<A0> BindAction<S, A0>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (Action<A0>)Delegate.CreateDelegate(typeof(Action<A0>), instance, m);
}
public static Action<A0, A1> BindAction<S, A0, A1>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (Action<A0, A1>)Delegate.CreateDelegate(typeof(Action<A0, A1>), instance, m);
}
public static Action<A0, A1, A2> BindAction<S, A0, A1, A2>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (Action<A0, A1, A2>)Delegate.CreateDelegate(typeof(Action<A0, A1, A2>), instance, m);
}
public static Action<A0, A1, A2, A3> BindAction<S, A0, A1, A2, A3>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (Action<A0, A1, A2, A3>)Delegate.CreateDelegate(typeof(Action<A0, A1, A2, A3>), instance, m);
}
//////////// StaticFunc
public static Func<A0, T> StaticFunc<A0, T>(MethodInfo m)
{
if (!m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is not static", m));
}
return (a0) => (T)m.Invoke(null, new object[] { a0 });
}
public static Func<A0, A1, T> StaticFunc<A0, A1, T>(MethodInfo m)
{
if (!m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is not static", m));
}
return (a0, a1) => (T)m.Invoke(null, new object[] { a0, a1 });
}
public static Func<A0, A1, A2, T> StaticFunc<A0, A1, A2, T>(MethodInfo m)
{
if (!m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is not static", m));
}
return (a0, a1, a2) => (T)m.Invoke(null, new object[] { a0, a1, a2 });
}
public static Func<A0, A1, A2, A3, T> StaticFunc<A0, A1, A2, A3, T>(MethodInfo m)
{
if (!m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is not static", m));
}
return (a0, a1, a2, a3) => (T)m.Invoke(null, new object[] { a0, a1, a2, a3 });
}
//////////// OpenFunc
public static Func<S, A0, T> OpenFunc<S, A0, T>(MethodInfo m)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (s, a0) => (T)m.Invoke(s, new object[] { a0 });
}
public static Func<S, A0, A1, T> OpenFunc<S, A0, A1, T>(MethodInfo m)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (s, a0, a1) => (T)m.Invoke(s, new object[] { a0, a1 });
}
public static Func<S, A0, A1, A2, T> OpenFunc<S, A0, A1, A2, T>(MethodInfo m)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (s, a0, a1, a2) => (T)m.Invoke(s, new object[] { a0, a1, a2 });
}
//////////// BindFunc
public static Func<A0, T> BindFunc<S, A0, T>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (a0) => (T)m.Invoke(instance, new object[] { a0 });
}
public static Func<A0, A1, T> BindFunc<S, A0, A1, T>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (a0, a1) => (T)m.Invoke(instance, new object[] { a0, a1 });
}
public static Func<A0, A1, A2, T> BindFunc<S, A0, A1, A2, T>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (a0, a1, a2) => (T)m.Invoke(instance, new object[] { a0, a1, a2 });
}
public static Func<A0, A1, A2, A3, T> BindFunc<S, A0, A1, A2, A3, T>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (a0, a1, a2, a3) => (T)m.Invoke(instance, new object[] { a0, a1, a2, a3 });
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 56dfdc21f4d594143ab023e6bd171f91
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,72 +0,0 @@
using System;
namespace UniJSON
{
struct GenericCast<S, T>
{
public static T Null()
{
if (typeof(T).IsClass)
{
return default(T);
}
else
{
throw new Exception("can not null");
}
}
delegate T CastFunc(S value);
static CastFunc s_cast;
delegate Func<T> ConstFuncCreator(S value);
static ConstFuncCreator s_const;
public static Func<T> Const(S value)
{
if (s_const == null)
{
s_const = new ConstFuncCreator(GenericCast.CreateConst<S, T>());
}
return s_const(value);
}
public static T Cast(S value)
{
if (s_cast == null)
{
s_cast = new CastFunc(GenericCast.CreateCast<S, T>());
}
return s_cast(value);
}
}
static partial class GenericCast
{
public static Func<S, T> CreateCast<S, T>()
{
var mi = ConcreteCast.GetMethod(typeof(S), typeof(T));
if (mi == null)
{
return (Func<S, T>)((S s) =>
{
return (T)(object)s;
});
}
else
{
return GenericInvokeCallFactory.StaticFunc<S, T>(mi);
}
}
public static Func<S, Func<T>> CreateConst<S, T>()
{
var cast = CreateCast<S, T>();
return (Func<S, Func<T>>)((S s) =>
{
return (Func<T>)(() => cast(s));
});
}
}
}

View File

@ -1,12 +0,0 @@
fileFormatVersion: 2
guid: 5e1cb5b0419b4a04da69e90f4890c349
timeCreated: 1545134074
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,12 +0,0 @@
fileFormatVersion: 2
guid: 4737055a37e47074e92e6fc03a96a9a5
timeCreated: 1545735557
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -283,270 +283,5 @@ namespace UniJSON
CommaCheck();
m_w.Write(node.Value.Bytes);
}
#region IRpc
int m_nextRequestId = 1;
static Utf8String s_jsonrpc = Utf8String.From("jsonrpc");
static Utf8String s_20 = Utf8String.From("2.0");
static Utf8String s_method = Utf8String.From("method");
static Utf8String s_params = Utf8String.From("params");
public void Notify(Utf8String method)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
}
EndList();
EndMap();
}
public void Notify<A0>(Utf8String method, A0 a0)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
this.Serialize(a0);
}
EndList();
EndMap();
}
public void Notify<A0, A1>(Utf8String method, A0 a0, A1 a1)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
this.Serialize(a0);
this.Serialize(a1);
}
EndList();
EndMap();
}
public void Notify<A0, A1, A2>(Utf8String method, A0 a0, A1 a1, A2 a2)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
this.Serialize(a0);
this.Serialize(a1);
this.Serialize(a2);
}
EndList();
EndMap();
}
public void Notify<A0, A1, A2, A3>(Utf8String method, A0 a0, A1 a1, A2 a2, A3 a3)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
this.Serialize(a0);
this.Serialize(a1);
this.Serialize(a2);
this.Serialize(a3);
}
EndList();
EndMap();
}
public void Notify<A0, A1, A2, A3, A4>(Utf8String method, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
this.Serialize(a0);
this.Serialize(a1);
this.Serialize(a2);
this.Serialize(a3);
this.Serialize(a4);
}
EndList();
EndMap();
}
public void Notify<A0, A1, A2, A3, A4, A5>(Utf8String method, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
this.Serialize(a0);
this.Serialize(a1);
this.Serialize(a2);
this.Serialize(a3);
this.Serialize(a4);
this.Serialize(a5);
}
EndList();
EndMap();
}
static Utf8String s_id = Utf8String.From("id");
public void Request(Utf8String method)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_id); Value(m_nextRequestId++);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
}
EndList();
EndMap();
}
public void Request<A0>(Utf8String method,
A0 a0)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_id); Value(m_nextRequestId++);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
this.Serialize(a0);
}
EndList();
EndMap();
}
public void Request<A0, A1>(Utf8String method,
A0 a0, A1 a1)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_id); Value(m_nextRequestId++);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
this.Serialize(a0);
this.Serialize(a1);
}
EndList();
EndMap();
}
public void Request<A0, A1, A2>(Utf8String method,
A0 a0, A1 a1, A2 a2)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_id); Value(m_nextRequestId++);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
this.Serialize(a0);
this.Serialize(a1);
this.Serialize(a2);
}
EndList();
EndMap();
}
public void Request<A0, A1, A2, A3>(Utf8String method,
A0 a0, A1 a1, A2 a2, A3 a3)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_id); Value(m_nextRequestId++);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
this.Serialize(a0);
this.Serialize(a1);
this.Serialize(a2);
this.Serialize(a3);
}
EndList();
EndMap();
}
public void Request<A0, A1, A2, A3, A4>(Utf8String method,
A0 a0, A1 a1, A2 a2, A3 a3, A4 a4)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_id); Value(m_nextRequestId++);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
this.Serialize(a0);
this.Serialize(a1);
this.Serialize(a2);
this.Serialize(a3);
this.Serialize(a4);
}
EndList();
EndMap();
}
public void Request<A0, A1, A2, A3, A4, A5>(Utf8String method,
A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_id); Value(m_nextRequestId++);
Key(s_method); Value(method);
Key(s_params); BeginList();
{
this.Serialize(a0);
this.Serialize(a1);
this.Serialize(a2);
this.Serialize(a3);
this.Serialize(a4);
this.Serialize(a5);
}
EndList();
EndMap();
}
static Utf8String s_error = Utf8String.From("error");
public void ResponseError(int id, Exception error)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_id); Value(id);
Key(s_error); this.Serialize(error);
EndMap();
}
static Utf8String s_result = Utf8String.From("result");
public void ResponseSuccess(int id)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_id); Value(id);
Key(s_result); Null();
EndMap();
}
public void ResponseSuccess<T>(int id, T result)
{
BeginMap();
Key(s_jsonrpc); Value(s_20);
Key(s_id); Value(id);
Key(s_result); this.Serialize(result);
EndMap();
}
#endregion
}
}

View File

@ -103,23 +103,5 @@ namespace UniJSON
public Double GetDouble() { return Segment.ToDouble(); }
public String GetString() { return JsonString.Unquote(Segment.ToString()); }
public Utf8String GetUtf8String() { return JsonString.Unquote(Segment); }
public T GetValue<T>()
{
switch (ValueType)
{
case ValueNodeType.Null: return GenericCast<object, T>.Null();
case ValueNodeType.Boolean: return GenericCast<bool, T>.Cast(GetBoolean());
case ValueNodeType.Integer: return GenericCast<int, T>.Cast(GetInt32());
case ValueNodeType.Number:
case ValueNodeType.NaN:
case ValueNodeType.Infinity:
case ValueNodeType.MinusInfinity:
return GenericCast<double, T>.Cast(GetDouble());
case ValueNodeType.String: return GenericCast<string, T>.Cast(GetString());
}
throw new NotImplementedException();
}
}
}

View File

@ -57,15 +57,6 @@ namespace UniJSON
public static ulong GetUInt64(this JsonNode self) { return self.Value.GetUInt64(); }
public static float GetSingle(this JsonNode self) { return self.Value.GetSingle(); }
public static double GetDouble(this JsonNode self) { return self.Value.GetDouble(); }
/// <summary>
/// for UnitTest. Use explicit GetT() or Deserialize(ref T)
/// </summary>
/// <returns></returns>
public static object GetValue(this JsonNode self)
{
return self.Value.GetValue<object>();
}
#endregion
public static IEnumerable<JsonNode> Traverse(this JsonNode self)

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 93b8e65929169944e823bc9a95c8a1ed
guid: 66a8eb0c7cffd8849999e6e19ff45b3a
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -25,35 +25,5 @@ namespace UniJSON
return Value;
}
}
[Test]
public void GenericCallUtilityTestsSimplePasses()
{
var s = new Sample();
{
var mi = s.GetType().GetMethod("Set");
var action = GenericInvokeCallFactory.OpenAction<Sample, int>(mi);
action(s, 1);
Assert.AreEqual(1, s.Value);
}
{
var mi = s.GetType().GetMethod("Get");
var func = GenericInvokeCallFactory.OpenFunc<Sample, int, int>(mi);
/*var value =*/ func(s, 1);
Assert.AreEqual(1, s.Value);
}
{
var mi = s.GetType().GetMethod("Set");
var action = GenericExpressionCallFactory.Create<Sample, int>(mi);
action(s, 2);
Assert.AreEqual(2, s.Value);
}
}
}
}

View File

@ -33,7 +33,7 @@ namespace UniJSON
var it = root.Traverse().GetEnumerator();
var f = new JsonFormatter();
f.Serialize("JsonPath");
f.Value("JsonPath");
root.SetValue(Utf8String.From("/a"), f.GetStoreBytes());
it.MoveNext(); Assert.AreEqual("/", it.Current.Pointer().ToString());

View File

@ -32,7 +32,7 @@ namespace UniJSON
{
public HogeFuga EnumDefault;
[JsonSchema(EnumSerializationType =EnumSerializationType.AsInt)]
[JsonSchema(EnumSerializationType = EnumSerializationType.AsInt)]
public HogeFuga EnumAsInt;
[JsonSchema(EnumSerializationType = EnumSerializationType.AsString)]
@ -43,12 +43,50 @@ namespace UniJSON
}
#region Serializer
static void SerializeValue<T>(T value, string json)
static void SerializeValue(float value, string json)
{
var b = new BytesStore();
var f = new JsonFormatter(b);
f.Serialize(value);
f.Value(value);
Assert.AreEqual(json, new Utf8String(b.Bytes).ToString());
}
static void SerializeValue(double value, string json)
{
var b = new BytesStore();
var f = new JsonFormatter(b);
f.Value(value);
Assert.AreEqual(json, new Utf8String(b.Bytes).ToString());
}
static void SerializeValue(bool value, string json)
{
var b = new BytesStore();
var f = new JsonFormatter(b);
f.Value(value);
Assert.AreEqual(json, new Utf8String(b.Bytes).ToString());
}
static void SerializeValue(string value, string json)
{
var b = new BytesStore();
var f = new JsonFormatter(b);
f.Value(value);
Assert.AreEqual(json, new Utf8String(b.Bytes).ToString());
}
static void SerializeList<T>(IEnumerable<T> values, string json, Action<JsonFormatter, T> seri)
{
var b = new BytesStore();
var f = new JsonFormatter(b);
f.BeginList();
foreach (var value in values)
{
seri(f, value);
}
f.EndList();
Assert.AreEqual(json, new Utf8String(b.Bytes).ToString());
}
@ -65,19 +103,19 @@ namespace UniJSON
SerializeValue(false, "false");
SerializeValue("ascii", "\"ascii\"");
SerializeValue(new[] { 1 }, "[1]");
SerializeValue(new[] { 1.1f }, "[1.1]");
SerializeValue(new[] { 1.2 }, "[1.2]");
SerializeValue(new[] { true, false }, "[true,false]");
SerializeValue(new[] { "ascii" }, "[\"ascii\"]");
SerializeValue(new List<int> { 1 }, "[1]");
SerializeList(new[] { 1 }, "[1]", (f, x) => f.Value(x));
SerializeList(new[] { 1.1f }, "[1.1]", (f, x) => f.Value(x));
SerializeList(new[] { 1.2 }, "[1.2]", (f, x) => f.Value(x));
SerializeList(new[] { true, false }, "[true,false]", (f, x) => f.Value(x));
SerializeList(new[] { "ascii" }, "[\"ascii\"]", (f, x) => f.Value(x));
SerializeList(new List<int> { 1 }, "[1]", (f, x) => f.Value(x));
//SerializeValue(new object[] { null, 1, "a" }, "[null,1,\"a\"]");
SerializeValue(new Dictionary<string, object> { }, "{}");
SerializeValue(new Dictionary<string, object> { { "a", 1 } }, "{\"a\":1}");
SerializeValue(new Dictionary<string, object> { { "a",
new Dictionary<string, object>{
} } }, "{\"a\":{}}");
// SerializeValue(new Dictionary<string, object> { }, "{}");
// SerializeValue(new Dictionary<string, object> { { "a", 1 } }, "{\"a\":1}");
// SerializeValue(new Dictionary<string, object> { { "a",
// new Dictionary<string, object>{
// } } }, "{\"a\":{}}");
// SerializeValue(new Point { X = 1 }, "{\"X\":1,\"Y\":0}");
@ -99,7 +137,12 @@ namespace UniJSON
var f = new JsonFormatter();
f.BeginMap();
f.KeyValue(() => p.Vector);
f.Key(nameof(p.Vector));
f.BeginList();
f.Value(p.Vector[0]);
f.Value(p.Vector[1]);
f.Value(p.Vector[2]);
f.EndList();
f.EndMap();
var json = JsonParser.Parse(new Utf8String(f.GetStoreBytes()));
@ -146,7 +189,7 @@ namespace UniJSON
// DeserializeValue(new EnumTest(), "{\"EnumDefault\":0,\"EnumAsInt\":0,\"EnumAsString\":\"Hoge\",\"EnumAsLowerString\":\"hoge\"}");
// }
class DictionaryValue: IEquatable<DictionaryValue>
class DictionaryValue : IEquatable<DictionaryValue>
{
public Dictionary<string, object> Dict = new Dictionary<string, object>();
@ -170,11 +213,11 @@ namespace UniJSON
public bool Equals(DictionaryValue other)
{
if(Dict==null && other.Dict == null)
if (Dict == null && other.Dict == null)
{
return true;
}
else if(Dict==null || other.Dict==null)
else if (Dict == null || other.Dict == null)
{
return false;
}

View File

@ -14,7 +14,7 @@ namespace UniJSON
{
var v = new JsonValue(Utf8String.From("NaN"), ValueNodeType.NaN, -1);
Assert.AreEqual("NaN", v.ToString());
Assert.AreEqual(Double.NaN, v.GetValue<double>());
Assert.AreEqual(Double.NaN, v.GetDouble());
}
}
@ -24,7 +24,7 @@ namespace UniJSON
{
var v = new JsonValue(Utf8String.From("Infinity"), ValueNodeType.Infinity, -1);
Assert.AreEqual("Infinity", v.ToString());
Assert.AreEqual(Double.PositiveInfinity, v.GetValue<double>());
Assert.AreEqual(Double.PositiveInfinity, v.GetDouble());
}
}
@ -34,7 +34,7 @@ namespace UniJSON
{
var v = new JsonValue(Utf8String.From("-Infinity"), ValueNodeType.MinusInfinity, -1);
Assert.AreEqual("-Infinity", v.ToString());
Assert.AreEqual(Double.NegativeInfinity, v.GetValue<double>());
Assert.AreEqual(Double.NegativeInfinity, v.GetDouble());
}
}
}

View File

@ -4,15 +4,16 @@ using UniGLTF;
using UniJSON;
using UnityEngine;
using MeshUtility;
using System;
namespace VRM.Samples
{
public static class JsonExtensions
{
public static void SetValue<T>(this JsonNode node, string key, T value)
public static void SetValue<T>(this JsonNode node, string key, T value, Action<JsonFormatter, T> seri)
{
var f = new JsonFormatter();
f.Serialize(value);
seri(f, value);
var p = Utf8String.From(key);
var bytes = f.GetStoreBytes();
node.SetValue(p, bytes);
@ -50,13 +51,13 @@ namespace VRM.Samples
using (new ActionDisposer(() => { GameObject.DestroyImmediate(context.Root); }))
{
var importedJson = JsonParser.Parse(context.Json);
importedJson.SetValue("/extensions/VRM/exporterVersion", VRMVersion.VRM_VERSION);
importedJson.SetValue("/asset/generator", UniGLTF.UniGLTFVersion.UNIGLTF_VERSION);
importedJson.SetValue("/scene", 0);
importedJson.SetValue("/materials/*/doubleSided", false);
importedJson.SetValue("/extensions/VRM/exporterVersion", VRMVersion.VRM_VERSION, (f, x) => f.Value(x));
importedJson.SetValue("/asset/generator", UniGLTF.UniGLTFVersion.UNIGLTF_VERSION, (f, x) => f.Value(x));
importedJson.SetValue("/scene", 0, (f, x) => f.Value(x));
importedJson.SetValue("/materials/*/doubleSided", false, (f, x) => f.Value(x));
//importJson.SetValue("/materials/*/pbrMetallicRoughness/roughnessFactor", 0);
//importJson.SetValue("/materials/*/pbrMetallicRoughness/baseColorFactor", new float[] { 1, 1, 1, 1 });
importedJson.SetValue("/accessors/*/normalized", false);
importedJson.SetValue("/accessors/*/normalized", false, (f, x) => f.Value(x));
importedJson.RemoveValue(Utf8String.From("/nodes/*/extras"));
/*
importJson.SetValue("/bufferViews/12/byteStride", 4);