ServiceStack.Text Deserialize json to object always converts to string and behaves strangely with quotes

最后都变了- 提交于 2019-12-07 16:23:10

问题


What I'm trying to do:

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.

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.

Here's an example:

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<object>(json1);
object o2 = JsonSerializer.DeserializeFromString<object>(json2);
object o3 = JsonSerializer.DeserializeFromString<object>(json3);
object o4 = JsonSerializer.DeserializeFromString<object>(json4);
object o5 = JsonSerializer.DeserializeFromString<object>(json5);
object o6 = JsonSerializer.DeserializeFromString<object>(json6);

Expected Underlying object:

object    type           value
o1        List           [1]
o2        List           [1,2]
o3        List           ['hello']
o4        List           ['hello','world']
o5        List           ["hello"]
o6        List           ["hello","world"]

Actual Underlying object:

object    type           value
o1        String         "[1]"
o2        String         "[1,2]"
o3        String         "['hello']"
o4        String         "['hello','world']"
o5        String         "["
o6        String         "["

For reference, the corresponding code block using Newtonsoft.Json interprets the underlying objects as Netwonsoft.Json.Link.JArray.

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.

Is there something I can do to have this behave the way I'd like using ServiceStack.Text?


回答1:


ServiceStack's text serializers work by converting the JSON into the specified schema, when you use object it's not possible to infer the type ahead of time, so in order to do this at runtime ServiceStack's JSON Serializer needs to emit a proprietary __type metadata property that tells the deserializer what to deserialize it into. This only gets emitted for JSON Object literals and not arrays which is why it doesn't work here.

Here are some ways to deserialize an array:

string json6 = "[\"hello\",\"world\"]";

var list = json6.FromJson<List<string>>();
list.PrintDump();

var array = json6.FromJson<string[]>();
array.PrintDump();

var arrayObj = json6.FromJson<object[]>();
arrayObj.PrintDump();


来源:https://stackoverflow.com/questions/15345422/servicestack-text-deserialize-json-to-object-always-converts-to-string-and-behav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!