无效的 JSON 模式(JSON001)

财产 价值
规则 ID JSON001
标题 无效的 JSON 模式
类别 风格
子类别 非适用
适用的语言 C#、Visual Basic
选项 请参阅 “选项” 部分。

概述

此规则标志将放置 JSON 文本字符串不符合所需语法的位置。 该规则适用于标识的 JSON,该 JSON 由包含 JSON 字符串前面的内联注释或注释决定,例如 //lang=json//lang=json,strict。 后一个注释使用严格模式进行 JSON 解释。

此规则仅适用于 IDE,不适用于命令行方案。

选项

选项指定希望规则强制实施的行为。

对于识别为 JSON 的代码,可以通过选择 “工具>选项>文本编辑器>C# | Visual Basic>高级”在 Visual Studio 中设置选项。 JSON 字符串 部分下提供了以下选项:

财产 DESCRIPTION
着色 JSON 字符串 指定是否对 JSON 字符串进行着色。
报告无效 JSON 指定是否报告无效的 JSON。
突出显示光标下的相关组件 指定 快速作
是否使用突出显示。

示例:

以下代码片段演示了一个 JSON 无效的示例。

// Code with violations
// lang=json,strict
var v = """{ "pie": true, "cherry": [1, 2, 3 }""";

// Fixed code
// lang=json,strict
var v = """{ "pie": true, "cherry": [1, 2, 3] }""";

// Code with violations
//lang=json,strict
var v = """
{
   "pie": true, 
   // Comments not allowed in strict mode
   "cherry": [1, 2, 3]
}
""";

// Fixed code
//lang=json
var v = """
{
   "pie": true, 
   // Comments allowed if not using strict mode
   "cherry": [1, 2, 3]
}
""";

抑制警告

如果只想取消单个冲突,请将预处理器指令添加到源文件以禁用,然后重新启用规则。

#pragma warning disable JSON001
// The code that's violating the rule is on this line.
#pragma warning restore JSON001

若要对文件、文件夹或项目禁用该规则,请在none中将其严重性设置为

[*.{cs,vb}]
dotnet_diagnostic.JSON001.severity = none

有关详细信息,请参阅如何禁止显示代码分析警告

另请参阅