Returning XML or JSON Is Easy with RESTful Services

    Someone asked me recently a quick question on how to set up a RESTful service to return JSON instead of XML. Th person already had a RESTful service they had created in WCF that returned XML, so most of the hard work was done already. This was a quick answer (nice to have a quick question/answer once in a while!)  since the WebGetAttribute class expose a property called ResponseFormat which accepts a WebMessageFormat enumeration value. The values are Xml and Json. So to configure a method to return a Product class serialized as JSON, simply do this:

    C#

    [OperationContract]
    [WebGet(UriTemplate = "Product/{productIdString}", 
        ResponseFormat = WebMessageFormat.Json)]
    public Product FindProduct(string productIdString)
    {
        int productId = int.Parse(productIdString);
        return GetProduct(productId);
    } 

    VB

    <OperationContract, WebGet(UriTemplate := "Product/{productIdString}", _
        ResponseFormat := WebMessageFormat.Json)> _
    Public Function FindProduct(ByVal productIdString As String) As Product
        Dim productId As Integer = Integer.Parse(productIdString)
        Return GetProduct(productId)
    End Function

    To Flip this to return XML instead, modify the example to set the ResponseFormat to WebMessageFormat.Xml

    And that’s it!

    DotNetKicks Image
    #1 John Papa on 8.27.2008 at 3:06 PM

    Thanks to Roger Jennings for catching a typo in my post where I transposed XML and JSON. All fixed now!



    #2 Dew Drop - August 28, 2008 | Alvin Ashcraft's Morning Dew on 8.28.2008 at 9:00 AM

    Pingback from Dew Drop - August 28, 2008 | Alvin Ashcraft's Morning Dew



    #3 Jay Kimble on 8.28.2008 at 10:50 AM

    John,

    Can the WCF RESTFul stuff obey the accept header attribute and send JSON or XML depending on the value of this header variable (aka ServerVariable)?

    I know I have been mentioning the RESTFul libary that is part of MVCContrib (and the MS MVC Framework), but I was able to fairly easily wire this up with that framework (It wasn't as clean as I would like, but it wasn't bad).



    #4 Rinsing the SOAP from WCF (or, RESTful WCF Hyperlink Acupuncture) | The Freak Parade on 8.28.2008 at 6:31 PM

    Pingback from Rinsing the SOAP from WCF (or, RESTful WCF Hyperlink Acupuncture) | The Freak Parade



    #5 John Papa on 8.29.2008 at 12:31 PM

    Jay ... Creating REST services with WCF allows you to specify the response format for each method and unique URI at design time. This means that a single method will return XML or JSON. I disucssed this with Rob Bagby and he recommends putting the type of response in the querystring and mapping that to the method. I'll post an example of this shortly.



    #6 Chris on 10.14.2008 at 8:34 PM

    Hey Do you know of a way to modify the JSON output to support JSONP.

    ajaxian.com/.../jsonp-json-with



    #7 Damian Mehers on 10.31.2008 at 3:05 AM

    I've put together an example where the response type is set dynamically based on the requested content type (via the "Accepts" header): damianblog.com/.../wcf-rest-dynami

    You just need to specify an additional attribute on your operation.

    I implement my own IDispatchMessageFormatter which is hooked up via my own ServiceHostFactory. Full source etc.

    /Damian



    Leave a Comment