mikeobrien / WcfRestContrib

The WCF REST Contrib library adds additional functionality to the current WCF REST implementation.
MIT License
49 stars 18 forks source link

Exception when deserializing JSON objects #14

Closed mladenadamovic closed 13 years ago

mladenadamovic commented 13 years ago

From Web.xml


        <behavior name="Rest">
        ...
       <formatter mimeTypes="application/json" type="WcfRestContrib.ServiceModel.Dispatcher.Formatters.DataContractJson, WcfRestContrib"/>

My service returns serialized JSON result without problems, but there is a problem with deserialization from JSON. It's configured with

    [ServiceConfiguration("Rest", true)]
    [WebDispatchFormatterConfiguration("application/json")]
    [WebDispatchFormatterMimeType(
        typeof(WcfRestContrib.ServiceModel.Dispatcher.Formatters.DataContractJson),
        "application/json")]

and the method is

        [WebInvoke(Method = "POST",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "order")]
        [WebDispatchFormatter]
        [OperationContract]
        [OperationAuthentication]
        OrderExternal CreateOrder(OrderCreateRequest createOrderRequest);

I'm testing it using a Java client where I'm sending a

            String message = "{\"CurrencyCode\":\"SEK\", \"OrderLines\": []}";

I tested and this string is serializable into OrderCreateRequest, so it's valid JSON. I'm sending this message as Content-Type "application/json".

However, deserialization fails with DeserializationException: The request body could not be deserialized. Encountered unexpected character '%'.

And this is due to code in

        public object Deserialize(WebFormatterDeserializationContext context, Type type)
        {
            if (context.ContentFormat != WebFormatterDeserializationContext.DeserializationFormat.Xml)
                throw new InvalidDataException("Data must be in xml format.");
            var serializer = new DataContractJsonSerializer(type);
            return serializer.ReadObject(context.XmlReader);
        }

This code looks fishy. XmlReader here is {None} in execution. And ReadObject here raises exception in question.

I suspect this is a WcfRestContrib issue or an application config problem, since I don't know what else could be the reason for this. Sending request encoded as application/json to POST in HTTP seems to be valid and my string is validly encoded and can be deserialized into proper JSON. However, somehow I cannot see that WCF/WcfRestContrib is fetching that string properly and there is no more stack trace here so that I can see any more details.

Help or comment would be appreciated.

mladenadamovic commented 13 years ago

It looks like I found a problem, when sending data from java I did send it mistakenly with

out.write(URLEncoder.encode(message, "UTF8"));

instead of only

out.write(message);

since application/json MIME type is definetely not URLEncoded...

Sorry about this... I'll let you know if I encounter other problems.