added MapMethodParameters(..) method family to JSONMapper
ln.build - build0.waldrennach.l--n.de build job pending Details

master 1.0.7
Harald Wolff 2020-12-25 20:31:19 +01:00
parent 02adec8cb8
commit fd9e11a182
5 changed files with 115 additions and 1 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Reflection;
using ln.json.mapping;
using NUnit.Framework;
@ -135,6 +136,55 @@ namespace ln.json.tests
Assert.AreEqual("C", texts[2]);
}
[Test]
public void TestParameterMapping()
{
MethodInfo testMethodInfo = GetType().GetMethod("MappingTestMethod");
object[] methodArguments = null;
JSONArray jsonArguments = new JSONArray()
.Add(new JSONString("text parameter value"))
.Add(new JSONNumber(10))
.Add(new JSONNumber(-2))
.Add(new JSONNumber(-3))
;
Assert.IsTrue(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonArguments, out object[] arguments));
Assert.AreEqual("text parameter value", arguments[0]);
Assert.AreEqual(10, arguments[1]);
Assert.AreEqual(-2, arguments[2]);
Assert.AreEqual(-3, arguments[3]);
jsonArguments.Remove(3);
Assert.IsFalse(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonArguments, out arguments));
JSONObject jsonNamedArguments = new JSONObject()
.Add("n", new JSONNumber(10))
.Add("m", new JSONNumber(-2))
.Add("a", new JSONNumber(-3))
.Add("t", new JSONString("text parameter value"))
;
Assert.IsTrue(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonNamedArguments, out arguments));
Assert.AreEqual("text parameter value", arguments[0]);
Assert.AreEqual(10, arguments[1]);
Assert.AreEqual(-2, arguments[2]);
Assert.AreEqual(-3, arguments[3]);
jsonNamedArguments.Remove("m");
Assert.IsTrue(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonNamedArguments, out arguments));
jsonNamedArguments.Remove("a");
Assert.IsTrue(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonNamedArguments, out arguments));
jsonNamedArguments.Remove("n");
Assert.IsFalse(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonNamedArguments, out arguments));
}
public void MappingTestMethod(string t, int n, short m = 2, long a = 3)
{}
void TestPrimitiveMapping(object primitiveValue)
{
Type primitiveType = primitiveValue.GetType();

View File

@ -45,6 +45,7 @@ namespace ln.json
public JSONNumber(double doubleValue)
: base(JSONValueType.NUMBER)
{
decValue = doubleValue.ToDecimal();
}
public JSONNumber(decimal decValue)

View File

@ -40,6 +40,8 @@ namespace ln.json
return this;
}
public void Remove(string propertyName) => values.Remove(propertyName);
public bool ContainsKey(string key)
{
return values.ContainsKey(key);

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>1.0.6</Version>
<Version>1.0.7</Version>
<Authors>Harald Wolff-Thobaben</Authors>
<Company>l--n.de</Company>
<Product>ln.json</Product>

View File

@ -267,6 +267,67 @@ namespace ln.json.mapping
}
public bool MapMethodParameters(MethodInfo methodInfo, JSONValue jsonArguments, out object[] methodArguments)
{
if (jsonArguments is JSONArray jsonArrayArguments)
return MapMethodParameters(methodInfo.GetParameters(), jsonArrayArguments, out methodArguments);
if (jsonArguments is JSONObject jsonNamedArguments)
return MapMethodParameters(methodInfo.GetParameters(), jsonNamedArguments, out methodArguments);
throw new ArgumentException(nameof(jsonArguments));
}
public bool MapMethodParameters(ParameterInfo[] parameterInfos, JSONValue jsonArguments, out object[] methodArguments)
{
if (jsonArguments is JSONArray jsonArrayArguments)
return MapMethodParameters(parameterInfos, jsonArrayArguments, out methodArguments);
if (jsonArguments is JSONObject jsonNamedArguments)
return MapMethodParameters(parameterInfos, jsonNamedArguments, out methodArguments);
throw new ArgumentException(nameof(jsonArguments));
}
public bool MapMethodParameters(MethodInfo methodInfo, JSONObject namedArguments, out object[] methodArguments) => MapMethodParameters(methodInfo.GetParameters(), namedArguments, out methodArguments);
public bool MapMethodParameters(ParameterInfo[] parameterInfos, JSONObject namedArguments, out object[] methodArguments)
{
methodArguments = new object[parameterInfos.Length];
for (int n=0; n < methodArguments.Length; n++)
{
if (namedArguments.ContainsKey(parameterInfos[n].Name))
{
if (!JSONMapper.DefaultMapper.Deserialize(namedArguments[parameterInfos[n].Name], parameterInfos[n].ParameterType, out methodArguments[n]))
return false;
} else if (parameterInfos[n].IsOptional)
{
methodArguments[n] = parameterInfos[n].DefaultValue;
} else
return false;
}
return true;
}
public bool MapMethodParameters(MethodInfo methodInfo, JSONArray jsonArguments, out object[] methodArguments) => MapMethodParameters(methodInfo.GetParameters(), jsonArguments, out methodArguments);
public bool MapMethodParameters(ParameterInfo[] parameterInfos, JSONArray jsonArguments, out object[] methodArguments)
{
methodArguments = new object[parameterInfos.Length];
if (jsonArguments.Count != methodArguments.Length)
return false;
for (int n=0; n < methodArguments.Length; n++)
{
if (!JSONMapper.DefaultMapper.Deserialize(jsonArguments[n], parameterInfos[n].ParameterType, out methodArguments[n]))
return false;
}
return true;
}
static JSONMapper()
{
DefaultMapper.Add(new JSONMapping(