XML error when trying to deserialize JSON

6,520 views
Skip to first unread message

IRDC

unread,
Feb 16, 2011, 3:24:52 AM2/16/11
to RestSharp
Hi,
I'm trying to use RestSharp in a WP7 app. The response to my request
contains the desired JSON in its Content property but the Data is
empty and the ErrorMessage reads:
ErrorException {"Data at the root level is invalid. Line 1, position
1."} System.Exception {System.Xml.XmlException}

I don't understand why an XML error occurs when the content is JSON.

David Poulin

unread,
Feb 16, 2011, 3:35:48 AM2/16/11
to rest...@googlegroups.com
Hi,

Did you try to specify explicitely Request DataFormat ? 

Can you have a look at this Json online viewer to check you json output : http://jsonviewer.stack.hu/




2011/2/16 IRDC <jensk...@googlemail.com>

IRDC

unread,
Feb 16, 2011, 3:46:00 AM2/16/11
to RestSharp
Hi,
from this thread (http://groups.google.com/group/restsharp/
browse_thread/thread/f48341bba7f92dd/) I understood that the
Request.DataFormat only works for the request I send not for
deserializing the response I receive (I tried it anyway and it doesn't
change the outcome).

John Sheehan

unread,
Feb 16, 2011, 3:58:34 AM2/16/11
to rest...@googlegroups.com
Sounds like the content type coming back from the server isn't application/json or text/json or text/x-json so the right deserializer isn't being called up and its defaulting to the Xml one. What's the value of RestResponse.ContentType? 

IRDC

unread,
Feb 16, 2011, 4:07:11 AM2/16/11
to RestSharp
text/plain;charset=UTF-8

IRDC

unread,
Feb 16, 2011, 4:31:35 AM2/16/11
to RestSharp
Is there a way to set this to a json type?

David Poulin

unread,
Feb 16, 2011, 4:38:41 AM2/16/11
to rest...@googlegroups.com
Maybe Trying to set header on Request

request.AddHeader("Content-type", "application/json")

Have a try and let us know.

2011/2/16 IRDC <jensk...@googlemail.com>

IRDC

unread,
Feb 16, 2011, 4:41:04 AM2/16/11
to RestSharp
That results in a ProtocolViolationException: A request with this
method cannot have a request body.

David Poulin

unread,
Feb 16, 2011, 4:45:14 AM2/16/11
to rest...@googlegroups.com
Ok. ContentType Property on response object can be set (but it does not really sense to change it), but, try to set 

response.ContentType = "application/json";

(before deserialization).

2011/2/16 IRDC <jensk...@googlemail.com>

IRDC

unread,
Feb 16, 2011, 4:50:14 AM2/16/11
to RestSharp
Can I use request.OnBeforeDeserialization for that? If so, how?

IRDC

unread,
Feb 16, 2011, 4:53:19 AM2/16/11
to RestSharp
Thank you for your help.
request.OnBeforeDeserialization = resp =>
{ resp.ContentType = "application/json"; };
did it.

David Poulin

unread,
Feb 16, 2011, 4:53:41 AM2/16/11
to rest...@googlegroups.com
Yes you can !

request.OnBeforeDeserialization = reponse => { response.ContentType = "application/json"; }



2011/2/16 IRDC <jensk...@googlemail.com>

John Sheehan

unread,
Feb 16, 2011, 12:52:16 PM2/16/11
to rest...@googlegroups.com
While that works, the easier (and recommended way) is to call RestClient.AddHandler("text/plain", new JsonDeserializer()); to associate that content type with the desired deserializer.
Reply all
Reply to author
Forward
0 new messages