How to implement custom JSON serialization from ASP.NET web service?

后端 未结 5 647
灰色年华
灰色年华 2020-12-23 23:23

What options are there for serialization when returning instances of custom classes from a WebService?

We have some classes with a number of child collection class p

相关标签:
5条回答
  • 2020-12-23 23:36

    Don't quote me on this working for certain, but I believe this is what you are looking for.

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public XmlDocument GetXmlDocument()
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(_xmlString);
        return xmlDoc;
    }
    
    0 讨论(0)
  • 2020-12-23 23:46

    If you're using .NET 3.x (or can), a WCF service is going to be your best bet.

    You can selectively control which properties are serialized to the client with the [DataMember] attribute. WCF also allows more fine-grained control over the JSON serialization and deserialization, if you desire it.

    This is a good example to get started: http://blogs.msdn.com/kaevans/archive/2007/09/04/using-wcf-json-linq-and-ajax-passing-complex-types-to-wcf-services-with-json-encoding.aspx

    0 讨论(0)
  • 2020-12-23 23:49

    You can use the System.Runtime.Serialization.Json.DataContractJsonSerializer class in the System.ServiceModel.Web.dll assembly.

    0 讨论(0)
  • 2020-12-23 23:57

    If you don't use code-generated classes, you can decorate your properties with the ScriptIgnoreAttribute to tell the serializer to ignore certain properties. Xml serialization has a similar attribute.

    Of course, you cannot use this approach if you want to return some properties of a class on one service method call and different properties of the same class on a different service method call. If you want to do that, return an anonymous type in the service method.

    [WebMethod]
    [ScriptMethod]
    public object GimmieData()
    {
        var dalEntity = dal.GimmieEntity(); //However yours works...
    
        return new
           {
               id = dalEntity.Id,
               description = dalEntity.Desc
           };
    
    }
    

    The serializer could care less about the type of the object you send to it, since it just turns it into text anyway.

    I also believe that you could implement ISerializable on your data entity (as a partial class if you have code-gen'd data entities) to gain fine-grained control over the serialization process, but I haven't tried it.

    0 讨论(0)
  • 2020-12-23 23:57

    I know this thread has been quiet for a while, but I thought I'd offer that if you override the SupportedTypes property of JavaScriptConverter in you custom converter, you can add the types that should use the converter. This could go into a config file if necessary. That way you wouldn't need a custom converter for each class.

    I tried to create a generic converter but couldn't figure out how to identify it in the web.config. Would love to find out if anyone else has managed it.

    I got the idea when trying to solve the above issue and stumbled on Nick Berardi's "Creating a more accurate JSON .NET Serializer" (google it).

    Worked for me:)

    Thanks to all.

    0 讨论(0)
提交回复
热议问题