C# System.Text.Json.JsonReaderException不是';在命名空间中找不到
我正在将.NETFramework4.5项目转换为.NETCore3.1。我的测试过去使用Newtonsoft.Json检查Json是否有效,现在我想用内置的System.Text.Json实现同样的功能。看来C# System.Text.Json.JsonReaderException不是';在命名空间中找不到,c#,json,.net-core,nunit,system.text.json,C#,Json,.net Core,Nunit,System.text.json,我正在将.NETFramework4.5项目转换为.NETCore3.1。我的测试过去使用Newtonsoft.Json检查Json是否有效,现在我想用内置的System.Text.Json实现同样的功能。看来 JsonElement values = JsonDocument.Parse(json).RootElement; 抛出System.Text.Json.JsonReaderException,但我无法捕捉到这一点,因为指向此异常会导致错误 命名空间“System.Text.Json
JsonElement values = JsonDocument.Parse(json).RootElement;
抛出System.Text.Json.JsonReaderException
,但我无法捕捉到这一点,因为指向此异常会导致错误
命名空间“System.Text.Json”中不存在类型或命名空间名称“JsonReaderException”(是否缺少程序集引用?)
我只想知道,怎么可能会抛出一些看似不存在的东西
更新#1:
堆栈跟踪:
at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
at System.Text.Json.Utf8JsonReader.Read()
at System.Text.Json.JsonDocument.Parse(ReadOnlySpan`1 utf8JsonSpan, Utf8JsonReader reader, MetadataDb& database, StackRowStack& stack)
at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 utf8Json, JsonReaderOptions readerOptions, Byte[] extraRentedBytes)
at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 json, JsonDocumentOptions options)
at System.Text.Json.JsonDocument.Parse(String json, JsonDocumentOptions options)
at Anonymized..ctor(String json) in Anonymized.cs:line 182
at Anonymized.<>c__DisplayClass12_0.<TestCreateLogEntryFromJson_IllegalValues>b__0() in Anonymized.cs:line 206
at NUnit.Framework.Assert.Throws(IResolveConstraint expression, TestDelegate code, String message, Object[] args)
但这并不能直接解决问题,如何在Assert.Throws中捕捉到这一点?您可以将
Newtonsoft.Json.dll
添加到您的项目引用中,这将解决您的问题。当前是内部
这一事实表明Microsoft可以随时修改或删除此类型,而System.Text.Json
的用户不应该依赖这个类作为公共类的子类继续存在。事实上,美国只知道这一点
当Utf8JsonReader
遇到无效的JSON时,它抛出一个JsonException
,其中包含行号和行上的字节位置等基本错误信息
以及
JsonReaderException
状态的代码注释:
//此类的存在是因为序列化程序需要捕获读取器引发的异常,以便抛出具有路径信息的JsonException。
相反,通过使用
或者您可以使用NUnit并引入FullTypeNameConstraint
,如下所示:
using NUnit.Framework;
using NUnit.Framework.Constraints;
public class FullTypeNameConstraint : Constraint
{
readonly string expectedFullTypeName;
public FullTypeNameConstraint(string expectedFullTypeName) : base(expectedFullTypeName) => this.expectedFullTypeName = expectedFullTypeName;
public override string DisplayName => "FullTypeNameOf";
public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
var actualTypeName = actual?.GetType().FullName;
return new ConstraintResult(this, actualTypeName, actualTypeName == expectedFullTypeName);
}
}
public class Is : NUnit.Framework.Is
{
public static FullTypeNameConstraint FullTypeNameOf(string expectedFullTypeName) => new FullTypeNameConstraint(expectedFullTypeName);
}
public static class CustomConstraintExtensions
{
public static FullTypeNameConstraint FullTypeNameOf(this ConstraintExpression expression, string expectedFullTypeName)
{
var constraint = new FullTypeNameConstraint(expectedFullTypeName);
expression.Append(constraint);
return constraint;
}
}
但老实说,我不推荐它
另一方面,它是一次性的,事实上需要进行处理以释放池内存以供重用
这里演示小提琴:。对于xUnit,可以使用
断言。通过任何(操作)
您可以使用堆栈跟踪和json示例吗?是否删除了与Newtonsoft.Json
相关的所有内容?是否确定JsonReaderException
位于命名空间System.Text.Json
下?我想它应该在Newtonsoft.Json
名称空间下,请参考这个。JsonReaderException
是一个Newtonsoft.Json
特定的异常。正如@PavelAnikhouski所提到的,您一定仍然对它有一些依赖关系。@PavelAnikhouski您是否仍然认为它在stacktrace之后可能与Newtonsoft有关?无效json的示例是一个空字符串。谢谢!回答得好!
Assert.Throws(Is.InstanceOf<JsonException>(), () => JsonDocument.Parse(json).Dispose());
Assert.AreEqual("System.Text.Json.JsonReaderException",
Assert.Throws(Is.InstanceOf<JsonException>(), () => JsonDocument.Parse(json).Dispose()).GetType().FullName);
using NUnit.Framework;
using NUnit.Framework.Constraints;
public class FullTypeNameConstraint : Constraint
{
readonly string expectedFullTypeName;
public FullTypeNameConstraint(string expectedFullTypeName) : base(expectedFullTypeName) => this.expectedFullTypeName = expectedFullTypeName;
public override string DisplayName => "FullTypeNameOf";
public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
var actualTypeName = actual?.GetType().FullName;
return new ConstraintResult(this, actualTypeName, actualTypeName == expectedFullTypeName);
}
}
public class Is : NUnit.Framework.Is
{
public static FullTypeNameConstraint FullTypeNameOf(string expectedFullTypeName) => new FullTypeNameConstraint(expectedFullTypeName);
}
public static class CustomConstraintExtensions
{
public static FullTypeNameConstraint FullTypeNameOf(this ConstraintExpression expression, string expectedFullTypeName)
{
var constraint = new FullTypeNameConstraint(expectedFullTypeName);
expression.Append(constraint);
return constraint;
}
}
Assert.Throws(Is.FullTypeNameOf("System.Text.Json.JsonReaderException"), () => JsonDocument.Parse(json).Dispose());