Controlling serialization/deserialization http://forums.asp.net/t/1800106.aspx/1?Controlling+serialization+deserialization+Thu, 24 May 2012 08:27:36 -040018001064965516http://forums.asp.net/p/1800106/4965516.aspx/1?Controlling+serialization+deserialization+Controlling serialization/deserialization <p>Hi,</p> <p>I'm writing a simple REST service based on the ApiController class. All is well and i can consume data from JQuery.</p> <p>I would like to modify the json slightly, specifically i want to change some &nbsp;names and exclude some properties of my model.</p> <p>The model is very simple:</p> <pre class="prettyprint">public class Comment { public string Id {get;set;} public string Caption { get; set; } public string InternalInformation { get; set; } }</pre> <p>The controller is</p> <pre class="prettyprint">public IEnumerable&lt;Comment&gt; Get() { return myVals; &nbsp;// &lt;- this is just a List&lt;&gt;<br /> } </pre> <p>Now, i have tried to decorate my Model using [DataContract] and [DataMember] attributes setting the Name property. This works well for GET.</p> <p>When i try to POST the same data back to my controller, the fields are not mapped correctly and i get nulls. My method for POST on the controller looks like this:</p> <pre class="prettyprint"> public Comment Post(Comment comment) { //comment.Caption is null here... } </pre> <p>If i rename the properties on my model, instead of using [DataMember(name=...)] to match the json all works well.</p> <p>How can i specify the serilazation so it works for all http verbs?</p> <p>Thanks,</p> <p>Frederik</p> <p><br> <br> </p> 2012-05-04T11:32:28-04:004965567http://forums.asp.net/p/1800106/4965567.aspx/1?Re+Controlling+serialization+deserialization+Re: Controlling serialization/deserialization <p>Can you please post the <strong>exact</strong> JSON that you are posting and it gets null? Also all your added decorations, since they are missing in the question.</p> 2012-05-04T12:01:21-04:004965696http://forums.asp.net/p/1800106/4965696.aspx/1?Re+Controlling+serialization+deserialization+Re: Controlling serialization/deserialization <p>Yes, sorry i should have included that:</p> <p>First, the data contract</p> <pre class="prettyprint">[DataContract] public class Comment { [DataMember(Name=&quot;id&quot;)] public string Id {get;set;} [DataMember(Name = &quot;caption&quot;)] public string MyInternalName { get; set; } [DataMember(Name = &quot;description&quot;)] public string Description { get; set; } //information i do not want to be part of the data contract public string InternalInformation { get; set; } }</pre> <p></p> <p>Here's the http GET and the reply from the controller:</p> <pre class="prettyprint">GET http://localhost:50359/api/famouspersons HTTP/1.1 Host: localhost:50359 Connection: keep-alive X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.9 (KHTML, like Gecko) Chrome/20.0.1115.1 Safari/536.9 Accept: application/json, text/javascript, */*; q=0.01 Referer: http://localhost:50359/ Accept-Encoding: gzip,deflate,sdch Accept-Language: da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 HTTP/1.1 200 OK Server: ASP.NET Development Server/10.0.0.0 Date: Fri, 04 May 2012 12:53:56 GMT X-AspNet-Version: 4.0.30319 Transfer-Encoding: chunked Cache-Control: no-cache Pragma: no-cache Expires: -1 Content-Type: application/json; charset=utf-8 Connection: Close 79 [{"caption":"Name1","description":"Description1","id":"id1"},{"caption":"Name2","description":"Description2","id":"id2"}] 0 </pre> <p><br />The fields are "caption", "description" and "id" as expected.</p> <p>Now i POST some data:</p> <pre class="prettyprint">POST http://localhost:50359/api/famouspersons HTTP/1.1 Host: localhost:50359 Connection: keep-alive Content-Length: 58 Origin: http://localhost:50359 X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.9 (KHTML, like Gecko) Chrome/20.0.1115.1 Safari/536.9 Content-Type: application/json Accept: */* Referer: http://localhost:50359/ Accept-Encoding: gzip,deflate,sdch Accept-Language: da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 {"id":"xx","caption":"Name3","description":"Description3"} </pre> <p><br />(Just a single record here, the POST is set up to receive one object)</p> <p>POST simply returns the same object:</p> <pre class="prettyprint">public Comment Post(Comment comment) { return comment; } </pre> <p></p> <p>Resulting in this capture in fiddler:</p> <pre class="prettyprint">HTTP/1.1 200 OK Server: ASP.NET Development Server/10.0.0.0 Date: Fri, 04 May 2012 13:01:51 GMT X-AspNet-Version: 4.0.30319 Transfer-Encoding: chunked Cache-Control: no-cache Pragma: no-cache Expires: -1 Content-Type: application/json Connection: Close 37 {"caption":null,"description":"Description3","id":"xx"} 0 </pre> <p><br> Unfortunately &quot;caption&quot; is null when i receive the object in the POST handler. The other fields are populated correctly.</p> <p>Thanks!! :)</p> <p></p> <p></p> 2012-05-04T13:03:04-04:004988257http://forums.asp.net/p/1800106/4988257.aspx/1?Re+Controlling+serialization+deserialization+Re: Controlling serialization/deserialization <p>You can create a custom formatter that bind your parameters correctly.</p> 2012-05-19T18:00:47-04:004991523http://forums.asp.net/p/1800106/4991523.aspx/1?Re+Controlling+serialization+deserialization+Re: Controlling serialization/deserialization <p>Fred, It looks like you are using Beta bits. In beta, the formatter's were used while replying where as MVC kind of model binding is used for processing the request. Model binding did not used to work well with DataContract attributes. Thus the same response returned by formatter in your scenario is not &nbsp;well processed as request with model binding.</p> <p>This has been fixed a little while ago. From now on, Formatter is always used to process the body both for request and response.&nbsp;You could use the latest bits as per instructions in Henrik's blog -&nbsp;<a href="http://blogs.msdn.com/b/henrikn/archive/2012/04/29/using-nightly-nuget-packages-with-asp-net-web-stack.aspx">http://blogs.msdn.com/b/henrikn/archive/2012/04/29/using-nightly-nuget-packages-with-asp-net-web-stack.aspx</a></p> <p>Thanks.</p> <p></p> 2012-05-22T07:47:50-04:004995211http://forums.asp.net/p/1800106/4995211.aspx/1?Re+Controlling+serialization+deserialization+Re: Controlling serialization/deserialization <p>Thanks, i'll give it a spin.</p> 2012-05-24T08:27:36-04:00