none
How to create a service reference using the DataContractSerializer and/or de/serialize proxy classes from/to JSON? RRS feed

  • Question

  • Hi everyone,

    our application consumes a bunch of (more or less) straightforward XML services. Now we will have to bind to a JSON service. So we have to seraialize our requests to and deserialize the responses from JSON. At least our affiliate provided a WSDL, so we can generate the proxies using a service reference (just like for XML services).

    Unfortunately the service reference is generated using the XmlSerializer instead of the DataContractSerializer. So nullable types are generated as a Property/PropertySpecified combination in order to simulate e.g. a nullable DateTime:

    [XmlAttributeAttribute] public DateTime TimeStamp { get; set; }
    [XmlIgnoreAttribute] public bool TimeStampSpecified { get; set; }

    Serializing these objects to XML works absolutely fine as the XmlSerializer knows how to handle the specified-flags. When these objects are serialized to JSON however, the specified-properties are serialized as well and the actual property is not treated as nullable. I've tried three different serializers:

    • DataContractJsonSerializer: Terrible result. Only the fields and types not marked with Xml-Attributes are serialized.
    • JavaScriptSerializer: Not bad, but the specified-properties are serialized.
    • Newtonsoft JsonSerializer: Similar result as the JavaScriptSerializer, also here the specified-properties are serialized.

    I'm quite sure, the DataContractJsonSerializer would work, if only the service reference was generated using the DataContractSerializer (DCS). However when I force VS to use the DCS (within Reference.svcmap), no code is generated at all (Reference.cs is empty). I've already found out that the WSDL/XSD must comply with some very strict schema rules, otherwise the XmlSerializer is used.

    Is there any way (maybe a program or an XSLT script) that can transform my WSDL/XSD to a DataContract-serializable form?

    And/Or: Is there a way to influence one of the JSON-serializers to process Property/PropertySpecified combinations as nullable types?

    Thanks a lot for your support.

    Ciao,
    Dante

    Sunday, July 7, 2013 12:20 PM

All replies