How to make JSON post to REST Web Service?

Jan 21, 2010 at 9:36 AM

I use the following code to serialize object:

string result = JsonConvert.SerializeObject(item, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects});    

The string contains object type. However it is not recognize in WCF REST Web Service.

 

If I use the following code:

DataContractJsonSerializer serializer =
            new DataContractJsonSerializer(typeof(DItem));
            MemoryStream stream = new MemoryStream();
            serializer.WriteObject(stream, item);
            string xml = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length);
            stream.Close();
            return xml;

Everything works correctly.

Please help resolve the problem. I need to have object type information on server side. But it is now instantiated correctly. How can I configure WCF to use JSON.NET de-serialization?

 

Thank you in advance

Nov 24, 2011 at 11:43 AM

I have the same issue sending a JSON.Net serialized object to a WCF 4 Rest WebMethod for PUT.    Here's my WebMethod signature:

[WebInvoke(UriTemplate = "MainRenderings/{mainRenderingId}", Method = "PUT")]

public byte[] MainRenderingUpdate(string mainRenderingId, MainRenderingDTO mainRendering)

If I use the DataContractJsonSerializer in the client call, the web service happily deserializes to the object.  If I send the object as JSON.Net serialized string, WCF Tracing reports the error:

Success Activity: Description Automatic format selection set the response format to 'Json' due to the HTTP client's stated preference for the 'application/json; charset=utf-8' media type.
Next Activity: Fails on deserializing my DTO object on a byte[] property that contains the SQL RowVersion

Exception Type: System.Runtime.Serialization.SerializationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Message: There was an error deserializing the object of type MainRenderingDTO. End element 'RowVersion' from namespace '' expected. Found text 'AAAAABfD3B4='.

Thanks in advance!