[英]Handling deserialization error
I have class to deserialize from json 我有要从JSON反序列化的类
class SeeSharp
{
public int Number;
}
Good json is smth like 好的json就像
{Number:1} {1号}
Wrong json which comes from PHP is 来自PHP的错误json是
{Number:false} {Number:false}
I use following way to handle deserialization error http://blog.mrlacey.co.uk/2012/03/debugging-deserialization-errors-in.html 我使用以下方法来处理反序列化错误http://blog.mrlacey.co.uk/2012/03/debugging-deserialization-errors-in.html
When fails reading member of type string/int/double/bool/etc, I would like to set it's value to default of this type and mark that error as handled. 当无法读取string / int / double / bool / etc类型的成员时,我想将其值设置为此类型的默认值,并将该错误标记为已处理。
Currently, JsonSerializationSettings.Error delegate doesn't allow to set value to failed member and doesn't show any information about type of member failed. 当前, JsonSerializationSettings.Error委托不允许为失败的成员设置值,并且不显示有关失败成员类型的任何信息。
If there is an option to do that in another way, would be great to know it. 如果有其他选择可以做,那真是太好了。
Can you use this class? 你可以使用这个课程吗?
public class SeeSharp
{
public string Number
{
get
{
return _number.ToString();
}
set
{
if (!int.TryParse(value, out _number))
_number = default(int);
}
}
public int _Number { get; set; }
}
JsonConvert
will use the string property Number
to set data, which handles the parsing itself. JsonConvert
将使用字符串属性Number
来设置数据,该数据将自行处理解析。 And then use the property _Number
in your code as int
然后在代码中将属性_Number
用作int
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.