3

I most admit that I'm probably forgetting something really simple, but I can't figure it out what

I have a class:

public class UserAgentInfo
{
    public string agent_type { get; set; }
    public string agent_name { get; set; }
    public string agent_version { get; set; }

    public string os_type { get; set; }
    public string os_name { get; set; }
    public string os_versionName { get; set; }
    public string os_versionNumber { get; set; }

    public string linux_distibution { get; set; }
}

then I get the Json string from a Url and try to deserialize it as:

using (System.Net.WebClient wc = new System.Net.WebClient())
{
    string json = wc.DownloadString("http://www.useragentstring.com/?getJSON=agent_type-agent_name-agent_version-os_type-os_name-os_versionName-os_versionNumber-linux_distibution&uas=" + o.Browser);

    agentInfo = ServiceStack.Text.TypeSerializer.DeserializeFromString<UserAgentInfo>(json);
}

the json string is:

"{\"agent_type\":\"Browser\",\"agent_name\":\"Chrome\",\"agent_version\":\"28.0.1500.72\",\"os_type\":\"Windows\",\"os_name\":\"Windows 7\",\"os_versionName\":\"\",\"os_versionNumber\":\"\",\"linux_distibution\":\"Null\"}"

or for the ones that are not used to C#

"{"agent_type":"Browser","agent_name":"Chrome","agent_version":"28.0.1500.72","os_type":"Windows","os_name":"Windows 7","os_versionName":"","os_versionNumber":"","linux_distibution":"Null"}"

and the returned object is:

{...UserAgentInfo}
   agent_name: null
   agent_type: null
   agent_version: null
   linux_distibution: null
   os_name: null
   os_type: null
   os_versionName: null
   os_versionNumber: null

What am I missing?

balexandre
  • 73,608
  • 45
  • 233
  • 342
  • 1
    If the returned string is literally what you write in the question, with an initial and ending double quote and escaped quotes inbetween, then it will be parsed as a single string, not an object. Should look like: `{"agent_type":"Browser","agent_name":"Chrome",...}`. – JimmiTh Jul 17 '13 at 11:32
  • in `C#` a string containing special chars such as `"`, must be escaped, hence the `\"`... – balexandre Jul 17 '13 at 11:41
  • That's beside the point. Your JSON string isn't a string literal written in C#, it's a string returned from a service. If that string starts and ends with `"`, and escapes all the other `"` characters, it will not be parsed as a JSON object, but as a single JSON string. Which is why I ask: Is the above *literally* what you get in return - or is it e.g. just what the Visual Studio debugger shows? – JimmiTh Jul 17 '13 at 11:44
  • ...and when I see it in "Immediate Window" it's always escaped! the string is a normal and valid JSON string, so please stop complaining about my string :P – balexandre Jul 17 '13 at 11:46
  • Right - realized it was an actual service URL, so tried it, and the response is correct. – JimmiTh Jul 17 '13 at 11:51
  • Trying to help, not complaining about anything. No matter what the VS debugger displays in the immediate window, the value of the `json` is not, and never will be, what you write above. If it *were* it *would* explain why it's not parsed properly. "People not used to C#" is a bold statement. "People expecting you to give the actual value of the string, rather than the representation in the debugger" would be more accurate. I'll stop "complaining" now. :-) – JimmiTh Jul 17 '13 at 12:00

1 Answers1

4

I believe you want to use the following to deserialize JSON string:

ServiceStack.Text.JsonSerializer.DeserializeFromString<UserAgentInfo>(json)

The TypeSerializer is for the JSV format (json-csv-jsv-serializers).

AlexD
  • 5,011
  • 2
  • 23
  • 34
  • 2
    someone needs to change the docs in the *Simple API*, that's why I got this issue: https://github.com/ServiceStack/ServiceStack.Text#simple-api :) – balexandre Jul 17 '13 at 14:56
  • Yeah, it's copied verbatim from the original blog post announcing JSV, but in the new context it *appears* to be about all the serializers. Plus "TypeSerializer" always struck me as a confusingly generic choice of name (I didn't spot it in the question code either). And what's wrong with "JsvSerializer"? But after three years, I guess it's too late to change it now. :-) – JimmiTh Jul 17 '13 at 19:58