I’m working with asmx webservices and serializing/deserializing a lot of data through them for a JSON admin panel I’m working on.
When information is loaded, i call a webservice that loads a User[] array into javascript. However, I have around 25 subclasses of User with their own unique properties that are actually getting loaded by this webservice call. It seems to work fine to load them this way, but saving has some issues.
The save method takes an array of User[] to save. On most of the subclasses, there is no __type hint in the javascript, and the deserialization fails. It seems to work fine on the classes that do have a __type property.
So my question is this. How can i force the __type to be included in the javascript objects that get serialized?
Here’s my C# call to load the objects (simplified a bit) just in case you need it:
[WebMethod(EnableSession = true)]
public User[] GetUsersInGroup(int UserGroupID)
{
List<User> UsersInGroup = User.GetUsersInGroup(UserGroupID);
return UsersInGroup.ToArray();
}
And here’s the Save Method:
[WebMethod(EnableSession = true)]
public void SaveUsers(User[] Users)
{
foreach (User CurUser in Users)
{
CurUser.Save();
}
}
And as requested, the Service Header:
[ScriptService]
public class FormFields : System.Web.Services.WebService
{
And the JSON (simplified) – comment shows what isn’t there:
{
"d": [
{
"__type": "Tools.User.AccountingUser", /* This is missing */
"UserID": 3934,
"HasQBAccess": true
},
{
"__type": "Tools.User.PowerUser",
"UserID": 3937,
"AccessDB": true,
"AccessFTP": true
}
]
}
In the interest of having some sort of answer that works, even if it isn’t a good one.
So far, the best answer I can find is to add a really long ugly function definition to my webservice. I never have to call it, just letting it sit there makes C# recognize the types: