JSON generic type deserialization of method parameters
Since JSON.NET supports deserialization of generic .NET types, I rewrote the DirectProvider.Execute method to allow deserialization in this generic sense. The trick is to check the expected types of the invoked method via reflection.
Note that I also removed the "SanitizeDates" method since it is no longer necessary. "ParseAsJson" is also not included anymore, but can be added again if needed.
Code:
internal object Execute(DirectRequest request)
{
DirectAction action = this.actions[request.Action];
if (action == null)
throw new DirectException("Unable to find action, " + request.Action);
DirectMethod method = action.GetMethod(request.Method);
if (method == null)
throw new DirectException("Unable to find action, " + request.Method);
JArray data = (JArray)request.RequestData["data"];
int parameterCount = request.Data != null ? request.Data.Length : data.Count;
if (parameterCount == 0)
{
if (method.Parameters > 0)
throw new DirectException("Parameters length does not match");
}
else
{
if (parameterCount > 1 && method.IsForm)
{
throw new DirectException("Form methods can only have a single parameter.");
}
else if (parameterCount != method.Parameters)
{
throw new DirectException("Parameters length does not match");
}
}
try
{
object[] param = new object[method.Parameters];
if (request.Data != null)
{
// lets use given request data
param = request.Data;
}
else
{
// Deserialize generic JSON to specific .NET method types
for (int t = 0; t < param.Length; t++)
{
Type paramType = method.ParameterInfos[t].ParameterType;
param[t] = JsonConvert.DeserializeObject(data[t].ToString(), paramType);
}
}
Type type = action.Type;
return method.Method.Invoke(type.Assembly.CreateInstance(type.FullName), param);
}
catch (TargetInvocationException ex)
{
// send application exception to browser
if (ex.InnerException != null)
throw new DirectException(ex.InnerException.ToString());
throw new DirectException(ex.Message);
}
catch (Exception ex)
{
throw new DirectException("Error occurred while calling Direct method: " + ex.Message);
}
}
Hope this helps someone
Call a function in C# to get json string
Hi
I have a data grid A and another grid B: When user click on a row of A, B will show a grid detail with the ID of the row clicked in A.
I have a function called GetBlogList(int rowIDofA) on server (written in C#) which will return a json string.
How can I call this function and pass the parameter when user click on a row of A by using Ext Direct Router?
Furthermore, do you have a completed guide to use this library?
Thank you
Sort and Filter params sent from grid
Hi,
does anybody has working copy of this Direct provider with grid/store and remote sorting and filtering?
I see sorters config value is converted to complex object on server side (after json conversion).
I see message about different number of params so my method can't be called...
Is it retired project?
Thanks,
Alex