[英]JSON.NET changes type of a System.Object property automatically to System.String
I have a class named Arg
. 我有一个名为
Arg
的类。 The Property ArgValue
is of the Type System.Object
. 属性
ArgValue
的类型为System.Object
。
public class Arg
{
public string ArgName { get; set; }
public object ArgValue { get; set; }
}
If I want to deserialize a JSON-string like this: 如果我想反序列化JSON字符串,如下所示:
string json = @"{""ArgName"":""arg"",""ArgValue"":""/something/more/than/a/string/""}";
var jsonSerializerSettings = new JsonSerializerSettings() { };
Arg arg = JsonConvert.DeserializeObject<Arg>(json, jsonSerializerSettings);
I get the Arg object with its values like this: 我得到的Arg对象的值如下所示:
ArgName
is of type string
-> arg
ArgName
的类型为string
-> arg
ArgValue
is of type object {string}
-> /something/more/than/a/string/
ArgValue
的类型为object {string}
-> /something/more/than/a/string/
I do not want that JSON.NET is converting it into System.String
. 我不希望JSON.NET将其转换为
System.String
。 If the property is of type System.Object
it should not convert it into a string. 如果属性的类型为
System.Object
,则不应将其转换为字符串。
It would be better it leaves it untouched and the value would be of type: JToken (or JValue ). 最好使其保持不变,并且值的类型为: JToken (或JValue )。
Is there a JsonSerializerSettings option to configure this behaviour? 是否有JsonSerializerSettings选项来配置此行为?
Additional information: 附加信息:
The string value in the property ArgValue
is "more than a string" and I need to convert it later by myself. 属性
ArgValue
的字符串值是“多于一个字符串”,我稍后需要自己进行转换。 JsonConverter
cannot be used here, because i do not know the concrete type of the object at this point. JsonConverter
不能在这里使用,因为此时我不知道对象的具体类型。 later i know and need to use JToken.ToObject<MySpecialType>()
. 稍后我知道并且需要使用
JToken.ToObject<MySpecialType>()
。 (Than it gets correctly converted by a JsonConverter
). (比起
JsonConverter
正确地进行了转换)。
As I stated in the comment, the json string you want to deserialize will be deserialized into an object of a class with two properties, both of type string. 正如我在评论中所述,要反序列化的json字符串将反序列化为具有两个属性的类的对象,这两个属性均为string类型。 This is because when you specify
"ArgName" : "arg"
the two quotes between arg
mark a value of type string. 这是因为当你指定
"ArgName" : "arg"
之间的两个引号arg
标记字符串类型的值。 This is also the case of ArgValue
which is serialized also as a string. ArgValue
也是如此, ArgValue
也被序列化为字符串。
By doing this line of code: 通过执行以下代码行:
Arg arg = JsonConvert.DeserializeObject<Arg>(json, jsonSerializerSettings);
You receive an object ( arg
) of type Arg
which is exactly as a classical object of type Arg
. 收到的对象(
arg
类型的) Arg
,其是完全一样的类型的古典对象Arg
。 It is the equivalent of doing: 这等效于:
Arg arg = new Arg{ ArgName = "arg", ArgValue = "/something/more/than/a/string/" };
The string assignation of property ArgValue
which expects an object is the same in the case of deserialize, it is no conversion (or better said cast ) involved, it is only boxing . 在反序列化的情况下,期望对象的属性
ArgValue
的字符串分配是相同的,它不涉及转换(或更佳地说是cast ),仅是装箱 。
For later conversion you can use 对于以后的转换,您可以使用
public class Arg<T>
{
public string ArgName { get; set; }
public T ArgValue { get; set; }
}
then you can pass target type as follows: 那么您可以按以下方式传递目标类型:
JsonConvert.DeserializeObject<Arg<Qualifiedname>>(somestring);
or 要么
public Arg<T> Deserialize<T>(string somestring){
return JsonConvert.DeserializeObject<Arg<T>>(somestring);
}
JToken
JToken
public class Arg
{
public string ArgName { get; set; }
public JToken ArgValue { get; set; }
}
JsonConvert
JsonConvert
其反序列化 public class Arg
{
public string ArgName { get; set; }
public string ArgValue { get; set; }
}
or simply pass it whole as string until you know what type is going to be parsed.. 或者只是将其整体作为字符串传递,直到您知道将要解析的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.