简体   繁体   English

使用 JsonConvert 反序列化 Json 文件时进行调试

[英]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.

相关问题 为什么反序列化到字典时JsonConvert会引发异常 - Why is JsonConvert throwing exception when deserializing to a dictionary 反序列化为数据集时,JsonConvert.DeserializeObject 不起作用 - JsonConvert.DeserializeObject is not working when deserializing to a DataSet 使用 JsonConvert.DeserializeObject (Newtonsoft.Json) 反序列化时丢失不可打印的 ascii 字符(组分隔符) - Losing non printable ascii character (group separator) when deserializing with JsonConvert.DeserializeObject (Newtonsoft.Json) 将具有十六进制值的JSON反序列化为sbyte属性时,JsonConvert.DeserializeObject引发异常 - JsonConvert.DeserializeObject throws an exception when deserializing JSON with a hex value into an sbyte property 解析 Json 文件 - JsonConvert - Parse a Json File - JsonConvert 在 C# 中反序列化 JSON 文件时出现 StackOverflowException - StackOverflowException when deserializing a JSON file in C# 反序列化为double时,JsonConvert会抛出一个“非有效整数”异常 - JsonConvert throws a 'not a valid integer' exception when deserializing to a double 反序列化双精度类型变量时,JsonConvert引发“无效整数”异常 - JsonConvert throws a 'not a valid integer' exception when deserializing double type variable 当成员使用 JsonConvert 反序列化两次时如何引发异常 - How to throw an exception when member comes twice on Deserializing with JsonConvert 在JsonConvert.DeserializeObject中反序列化对象时出现意外的标记 - Unexpected token when deserializing object in JsonConvert.DeserializeObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM