[英]Debugging when deserializing a Json file with JsonConvert
Is there a way to "debug a Json file" at deserialisation using JsonConvert?有没有办法在使用 JsonConvert 进行反序列化时“调试 Json 文件”?
It would be very useful if the line (or group of lines) of the Json file being processed could be printed as a debug option.如果正在处理的 Json 文件的行(或行组)可以作为调试选项打印,那将非常有用。
Otherwise, when the deserialisation of a Json file into an object fails, it can be very difficult to figure out what is wrong with that file.否则,当 Json 文件反序列化为 object 失败时,很难找出该文件有什么问题。
You can implement a custom TraceWriter
that logs to console as deserialization goes on, like this.您可以实现一个自定义
TraceWriter
,在反序列化进行时记录到控制台,就像这样。 Of course you could also wrap it into a log4net log entry or just do whatever you like.:当然,您也可以将其包装到 log4net 日志条目中,或者做任何您喜欢的事情。:
public class ConsoleTraceWriter : ITraceWriter
{
public TraceLevel LevelFilter
{
// trace all messages
get { return TraceLevel.Verbose; }
}
public void Trace(TraceLevel level, string message, Exception ex)
{
Console.WriteLine(level.ToString() + ": " + message);
if (ex != null) {
Console.WriteLine(level.ToString() + ": " + message + " Ex: " + ex.Message);
}
}
}
And set it at deserialization并将其设置为反序列化
JsonConvert.DeserializeObject(myJson, new JsonSerializerSettings { TraceWriter = new ConsoleTraceWriter() });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.