Hello again, @Ibrohan!
The issue that happens if you have
JSON.GlobalSettings.TypeNameHandling = TypeNameHandling.None
is,
ProcessGlobalData(ParentClass[] data)
has already been deserialized with the
GlobalSetting
before you set the request-specific's
TypeNameHandling behavior.
So all that you need to do is to re-submit the raw value to be deserialized into the parameter variable
after the
JSON.RequestSettings is overridden.
Then, if you want to keep the default global (and you should, as it can potentially break any internal Ext.NET client-server communication),
TypeNameHandling.None
, then you can ensure the two methods that require
TypeNameHandling.All
works changing them to something like this:
[DirectMethod]
public static ParentClass[] LoadGlobalData()
{
JSON.RequestSettings = JSON.CopyCurrentSettings;
JSON.RequestSettings.TypeNameHandling = TypeNameHandling.All;
return new ParentClass[]
{
new ParentClass
{
Id = 1
},
new ChildClass
{
Id = 2,
DataForClient = "xxx"
}
};
}
[DirectMethod]
public static string ProcessGlobalData(ParentClass[] data)
{
JSON.RequestSettings = JSON.CopyCurrentSettings;
JSON.RequestSettings.TypeNameHandling = TypeNameHandling.All;
var request = HttpContext.Current.Request;
string rawData = request.RequestType == "POST" ? request.Form["data"] : request["data"];
data = (ParentClass[])JSON.Deserialize(rawData, data.GetType());
return string.Join(", ", data.Select(item => item.Test()));
}
This is probably not very optimal, as
data
will just get deserialized twice in
ProcessGlobalData
. It should be okay if the methods you need to work like that are not time-critical in your application.
The parameter like this may still be interesting as you are able to use Ext.NET facilities to address the direct method and encapsulate the parameter.
In the case you don't want the double deserializations, you should then change the
data
argument to
string
, then
JSON.stringify()
it from client-side before passing to the direct method. Then the method would become something like:
[DirectMethod]
public static string ProcessGlobalData(string rawData)
{
JSON.RequestSettings = JSON.CopyCurrentSettings;
JSON.RequestSettings.TypeNameHandling = TypeNameHandling.All;
ParentClass[] data = (ParentClass[])JSON.Deserialize(rawData, data.GetType());
return string.Join(", ", data.Select(item => item.Test()));
}
This is unfortunately a limitation, as there's no telling one wants to change the
JSON.RequestSettings
within the direct method until it is called and, in order for it to be called, it needs the parameter already done. So in this case, one way or another, you have to re-deserialize the parameter after the desired setting is determined.
Hope this helps!