Quantcast
Channel: Windows – delog
Viewing all articles
Browse latest Browse all 44

Customize how objects are serialized to JSON in a WCF service

$
0
0

This post presents a slightly sophisticated version of the service presented in Cleaner JSON from a WCF service with webHttp behavior. I demonstrate how to customize serialization of a custom Object to JSON.

Additional assembly references

You’ll need to add System.Runtime.Serialization to your project and use it in the code.

using System.Runtime.Serialization;

The custom object

We define a new Person class, annotated using the DataContract attribute. The DataMember attribute applied to each attribute of the class allows us to specify an alternative name for the attribute, whether it is required to be present, the order in which it is serialized, and so on.

    [DataContract()]
    class Person : IExtensibleDataObject
    {
        [DataMember(Name="id", IsRequired=false)]
        public string ID { get; set; }

        [DataMember(Name="name", IsRequired=true)]
        public string Name { get; set; }

        public ExtensionDataObject ExtensionData {get; set;}
    }

Method signatures in the service interface

The IMyService interface is modified to add additional method signatures that consume and return Person objects.

        [OperationContract]
        string Person(Person p);

        [OperationContract]
        Person GetPerson(string id);

The service implementation

The service method Person receives an instance of Person using HTTP POST to URI /Person. WCF handles conversion of JSON to the Person object, and vice-versa.

        private int nextID = 1;
        Dictionary<string, Person> people = new Dictionary<string, Person>();

        [WebInvoke(Method = "POST",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "/Person")]
        public string Person(Person p)
        {
            p.ID = p.ID == null? (nextID++).ToString(): p.ID;
            people[p.ID] = p;
            return p.ID;
        }

        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "/Person/{id}")]
        public Person GetPerson(string id)
        {
            Person p;
            p = people.TryGetValue(id, out p) ? p: null;
            return p;
        }

The application configuration

The app.config doesn’t need to be changed. It states for instance that our service endpoint is http://localhost:8003/myservice.

Testing with Fiddler

Fiddler is a powerful Web debugger written in .NET. It can be used to send HTTP requests and observe HTTP responses. The following figure shows the raw request and response when creating a new Person called John. It is important to set the Content-Type header to application/json. Try and send the JSON shown in the request pane using the Composer tab. The service responds with the identifier of the Person added or updated.
fiddler_json_post

The following figure shows a query to read Person with id 1. Try the GET request by using the Composer tab.
fiddler_json_get

The working project source can be obtained from GitHub. It builds with Visual Studio 2012 Express.


Filed under: .NET, Windows

Viewing all articles
Browse latest Browse all 44

Trending Articles