Note that there are some explanatory texts on larger screens.

plurals
  1. POServiceStack.Text Deserialize json to object always converts to string and behaves strangely with quotes
    text
    copied!<p>What I'm trying to do:</p> <p>I have json objects that have values which could be string, ints, doubles, or lists of any of these. I'm trying to deserialize these json strings into C# objects, but because they could have multiple types, I'm stuck using the generic object, rather than a strongly typed alternative.</p> <p>My issue: It appears as though the ServiceStack.Text.JsonSerializer.DeserializeFromString(jsonString) function behaves oddly in cases where T = object. It will always treat things as a string, and does not work with quotes.</p> <p>Here's an example:</p> <pre><code>string json1 = "[1]"; string json2 = "[1,2]"; string json3 = "['hello']"; string json4 = "['hello','world']"; string json5 = "[\"hello\"]"; string json6 = "[\"hello\",\"world\"]"; object o1 = JsonSerializer.DeserializeFromString&lt;object&gt;(json1); object o2 = JsonSerializer.DeserializeFromString&lt;object&gt;(json2); object o3 = JsonSerializer.DeserializeFromString&lt;object&gt;(json3); object o4 = JsonSerializer.DeserializeFromString&lt;object&gt;(json4); object o5 = JsonSerializer.DeserializeFromString&lt;object&gt;(json5); object o6 = JsonSerializer.DeserializeFromString&lt;object&gt;(json6); </code></pre> <p>Expected Underlying object:</p> <pre><code>object type value o1 List [1] o2 List [1,2] o3 List ['hello'] o4 List ['hello','world'] o5 List ["hello"] o6 List ["hello","world"] </code></pre> <p>Actual Underlying object:</p> <pre><code>object type value o1 String "[1]" o2 String "[1,2]" o3 String "['hello']" o4 String "['hello','world']" o5 String "[" o6 String "[" </code></pre> <p>For reference, the corresponding code block using Newtonsoft.Json interprets the underlying objects as Netwonsoft.Json.Link.JArray.</p> <p>As it currently stands, I would have to ensure single quotes are used in the json, and then deserialize any string that was extracted recursively until everything has been properly extracted.</p> <p>Is there something I can do to have this behave the way I'd like using ServiceStack.Text?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload