ASMX WebService/Serialization: How to force " __type" property to be included in javascript
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[] 开发者_C百科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:
[WebMethod(EnableSession = true)]
public void ThisMethodSucks(AccountingUser u1, PowerUser u2, AdminUser u3, DatabaseUser u4, ADAdministratorUser u5, PSUser u6, NUTUser u7, TechUser u8, ...[all classes here])
{
//this method never needs to get called, just sits here.
}
精彩评论