重新抛出 JsonConverter 异常的正确方法
Proper way to rethrow a JsonConverter exception
我有以下反序列化设置 json:
parsedResponse = JsonConvert.DeserializeObject<T>(
json,
new JsonSerializerSettings
{
Error = (object sender, ErrorEventArgs args) =>
{
throw new MyParseException($"Parse error: {args.ErrorContext.Error.Message}");
},
Converters =
{
new MyItemConverter(),
new BoolConverter(),
new UnixDateTimeConverter(),
new NullableIntConverter(),
new UriConverter()
}
}
);
在一种情况下,json
有一堆空值(如 "title" : null,
等),这会导致我的一个转换器出现 NullReferenceException。但是在错误处理程序中抛出 MyParseException
会导致
System.InvalidOperationException: Current error context error is different to requested error.
我想我可以这样做:
try
{
parsedResponse = JsonConvert.DeserializeObject<T>(
json,
new JsonSerializerSettings
{
Converters =
{
new MyItemConverter(),
new BoolConverter(),
new UnixDateTimeConverter(),
new NullableIntConverter(),
new UriConverter()
}
}
);
}
catch (Exception ex)
{
throw new MyParseException($"Parse error: {ex.Message}");
}
但是有更好的方法吗? (也许与我的原始解决方案更相似但不会导致错误上下文问题?)
基于 Newtonsoft 示例 here。我最终做了以下事情:
List<MyParseException> errors = new List<MyParseException>();
T parsedResponse = JsonConvert.DeserializeObject<T>(
json,
new JsonSerializerSettings
{
Error = (object sender, ErrorEventArgs args) =>
{
errors.Add(new MyParseException(String.Format("Parse error: {0}", args.ErrorContext.Error.Message), args.ErrorContext.Error));
args.ErrorContext.Handled = true;
},
Converters =
{
new MyItemConverter(),
new BoolConverter(),
new UnixDateTimeConverter(),
new NullableIntConverter(),
new UriConverter()
}
}
);
if (errors.Count == 1)
{
MyParseException firstException = errors[0];
firstException.Data["json"] = json;
throw firstException;
}
else if (errors.Count > 1)
{
AggregateException ex = new AggregateException("Unable to parse json. See innner exceptions and exception.Data[\"json\"] for details", errors);
ex.Data["json"] = json;
throw ex;
}
我有以下反序列化设置 json:
parsedResponse = JsonConvert.DeserializeObject<T>(
json,
new JsonSerializerSettings
{
Error = (object sender, ErrorEventArgs args) =>
{
throw new MyParseException($"Parse error: {args.ErrorContext.Error.Message}");
},
Converters =
{
new MyItemConverter(),
new BoolConverter(),
new UnixDateTimeConverter(),
new NullableIntConverter(),
new UriConverter()
}
}
);
在一种情况下,json
有一堆空值(如 "title" : null,
等),这会导致我的一个转换器出现 NullReferenceException。但是在错误处理程序中抛出 MyParseException
会导致
System.InvalidOperationException: Current error context error is different to requested error.
我想我可以这样做:
try
{
parsedResponse = JsonConvert.DeserializeObject<T>(
json,
new JsonSerializerSettings
{
Converters =
{
new MyItemConverter(),
new BoolConverter(),
new UnixDateTimeConverter(),
new NullableIntConverter(),
new UriConverter()
}
}
);
}
catch (Exception ex)
{
throw new MyParseException($"Parse error: {ex.Message}");
}
但是有更好的方法吗? (也许与我的原始解决方案更相似但不会导致错误上下文问题?)
基于 Newtonsoft 示例 here。我最终做了以下事情:
List<MyParseException> errors = new List<MyParseException>();
T parsedResponse = JsonConvert.DeserializeObject<T>(
json,
new JsonSerializerSettings
{
Error = (object sender, ErrorEventArgs args) =>
{
errors.Add(new MyParseException(String.Format("Parse error: {0}", args.ErrorContext.Error.Message), args.ErrorContext.Error));
args.ErrorContext.Handled = true;
},
Converters =
{
new MyItemConverter(),
new BoolConverter(),
new UnixDateTimeConverter(),
new NullableIntConverter(),
new UriConverter()
}
}
);
if (errors.Count == 1)
{
MyParseException firstException = errors[0];
firstException.Data["json"] = json;
throw firstException;
}
else if (errors.Count > 1)
{
AggregateException ex = new AggregateException("Unable to parse json. See innner exceptions and exception.Data[\"json\"] for details", errors);
ex.Data["json"] = json;
throw ex;
}