Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when deserializing JSON objects #14

Closed
mladenadamovic opened this issue Jun 8, 2011 · 1 comment
Closed

Exception when deserializing JSON objects #14

mladenadamovic opened this issue Jun 8, 2011 · 1 comment

Comments

@mladenadamovic
Copy link

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant