如何让 JSON 模式在 Visual Studio 2015 中工作(智能感知和验证)

How to get JSON Schema to work in Visual Studio 2015 (Intellisense and validation)

我在获取 json 文件时遇到问题,我正在尝试创建该文件以使用我创建的 json 架构,以便我可以同时使用智能感知和验证来确保 json 我必须手卡纸是好的。我不确定是我的模式有问题,还是我试图让 VS2015 使用它的方式。

目前,我已将所有架构内容放入一个文件中(在尝试将其拆分时遇到问题)。

{
  "$schema": "http://json-schema.org/schema#",
  "id": "http://savatronix.com/jsonschemas/losthaven1/MainSchemaV1.json",
  "title": "Lost Haven Common JSON",
  "description": "A schema for json types that will be common across many different object types. V1",
  "definitions": {
    "gameDateTime": {
      "type": "object",
      "properties": {
        "year": {
          "type": "integer",
          "minimum": 0
        },
        "month": {
          "type": "string",
          "enum": [ "Nil", "Spring", "Summer", "Autumn", "Winter" ]
        },
        "weekday": {
          "type": "string",
          "enum": [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]
        },
        "day": {
          "type": "integer",
          "minimum": 0,
          "maximum": 30
        },
        "hour": {
          "type": "integer",
          "minimum": 0,
          "maximum": 23
        },
        "minute": {
          "type": "integer",
          "minimum": 0,
          "maximum": 59
        },
        "second": {
          "type": "integer",
          "minimum": 0,
          "maximum": 59
        }
      }
    },
    "characterAttributes": {
      "type": "object",
      "properties": {
        "strength": {
          "type": "integer",
          "minimum": 0
        },
        "agility": {
          "type": "integer",
          "minimum": 0
        },
        "dexterity": {
          "type": "integer",
          "minimum": 0
        },
        "intelligence": {
          "type": "integer",
          "minimum": 0
        },
        "endurance": {
          "type": "integer",
          "minimum": 0
        },
        "charisma": {
          "type": "integer",
          "minimum": 0
        },
        "luck": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "characterElements": {
      "type": "object",
      "properties": {
        "earth": { "type": "integer" },
        "wind": { "type": "integer" },
        "fire": { "type": "integer" },
        "water": { "type": "integer" },
        "lightning": { "type": "integer" },
        "light": { "type": "integer" },
        "dark": { "type": "integer" }
      }
    },
    "characterStatus": {
      "type": "object",
      "properties": {
        "poison": { "type": "integer" },
        "paralyze": { "type": "integer" },
        "sleep": { "type": "integer" },
        "fatigue": { "type": "integer" },
        "charm": { "type": "integer" },
        "confusion": { "type": "integer" }
      }
    },
    "characterStats": {
      "type": "object",
      "properties": {
        "attackPower": {
          "type": "integer",
          "minimum": 0
        },
        "block": {
          "type": "integer",
          "minimum": 0
        },
        "health": {
          "type": "integer",
          "minimum": 0
        },
        "defense": {
          "type": "integer",
          "minimum": 0
        },
        "evade": {
          "type": "integer",
          "minimum": 0
        },
        "attackSpeed": {
          "type": "integer",
          "minimum": 0
        },
        "parry": {
          "type": "integer",
          "minimum": 0
        },
        "stamina": {
          "type": "integer",
          "minimum": 0
        },
        "movementSpeedModifier": { "type": "integer" },
        "elementalPower": { "$ref": "#/definitions/characterElements" },
        "statusPower": { "$ref": "#/definitions/characterStatus" }
      }
    },
    "attributesAndStatsContainer": {
      "type": "object",
      "properties": {
        "attributes": { "$ref": "#/definitions/characterAttributes" },
        "stats": { "$ref": "#/definitions/characterStats" }
      }
    },
    "characterEquipmentState": {
      "type": "object",
      "properties": {
        "weaponId": { "type": "string" },
        "headgearId": { "type": "string" },
        "chestId": { "type": "string" },
        "legsId": { "type": "string" },
        "bootsId": { "type": "string" },
        "glovesId": { "type": "string" },
        "firstAccessoryId": { "type": "string" },
        "secondAccessoryId": { "type": "string" }
      }
    },
    "baseObjectState": {
      "type": "object",
      "properties": {
        "baseId": { "type": "string" },
        "referenceId": { "type": "string" }
      }
    },
    "characterState": {
      "type": "object",
      "allOf": [
        { "$ref": "#/definitions/baseObjectState" },
        {
          "properties": {
            "characterName": { "type": "string" },
            "characterSex": {
              "type": "string",
              "enum": [ "Unknown", "Male", "Female" ]
            },
            "race": {
              "type": "string",
              "enum": [ "Unknown", "Human", "Yokai" ]
            },
            "baseStats": { "$ref": "#/definitions/attributesAndStatsContainer" },
            "currentHealth": {
              "type": "integer",
              "minimum": 0
            },
            "birthday": { "$ref": "#/definitions/gameDateTime" },
            "equippedItems": { "$ref": "#/definitions/characterEquipmentState" }
          }
        }
      ]
    },
    "enemyStateObject": {
      "type": "object",
      "allOf": [
        { "$ref": "#/definitions/characterState" },
        {
          "properties": {
            "isBoss": { "type": "boolean" }
          }
        }
      ]
    },
    "enemyState": {
      "type": "object",
      "properties": {
        "enemies": {
          "type": "array",
          "items": { "$ref": "#/definitions/enemyStateObject" },
          "minItems": 1
        }
      }
    },
    "NpcState": {
      "type": "object",
      "allOf": [
        { "$ref": "#/definitions/characterState" },
        {
          "properties": {
            "backgroundInfo": { "type": "string" },
            "history": { "type": "string" },
            "personalityDescription": { "type": "string" },
            "job": {
              "type": "string",
              "enum": [ "None" ]
            },
            "isRecruitable": { "type": "boolean" },
            "isEligableSignificantOther": { "type": "boolean" },
            "significantOtherId": { "type": "string" },
            "wallet": {
              "type": "integer",
              "minimum": 0
            },
            "inventory": {
              "type": "array",
              "items": { "type": "string" }
            }
          }
        }
      ]
    }
  }
}

我正在尝试使用 enemyState 定义来创建一个包含敌人数组的对象(这样做是因为 MSDN 博客建议使用 $schema,以便 VS 将自动检测所需的模式)。

"enemyState": {
      "type": "object",
      "properties": {
        "enemies": {
          "type": "array",
          "items": { "$ref": "#/definitions/enemyStateObject" },
          "minItems": 1
        }
      }
    },

在我的 Enemies.json 中,我尝试过:

{
  "$schema": "../../../JsonSchemas/MainSchema.json#/definitions/enemyState"
}

这是 "msdn blog post" 推荐的方法:Intellisense for JSON Schema in the JSON Editor

我也试过省略它,然后将 link 直接复制到编辑器中的架构位置栏中。

键入 $Schema 部分时,它是正确的位置,因为 #/defintions/enemyState 部分开始运行智能感知。

不过也仅此而已。尝试创建实际的 json 时,我没有得到智能感知或验证。我试过关闭并重新打开文件,以及重新启动 visual studio.

我想要的预期效果是获得智能感知和验证,这样我就可以创建一个敌人状态对象数组(这是我正在制作的游戏中各种敌人的定义,并被读取和加载到 Unity 中)并确保所需的属性存在(尚未在模式中创建,图我至少在我让它工作后添加所需的属性),并确保一切都在范围内,并且它验证所以我知道没有错误。

如有任何帮助,我们将不胜感激。

非常感谢!

好的,我终于让它工作了,方法如下。

我使用 Solution Explorer -> json 架构创建了一个测试 json 文件,并将其拖到 JSON 文件中,它成功了。

然后我将其修改为看起来与我的模式的相关部分完全一样(调整 $ref's 以便它们在需要时指向主模式文件)。

有效。

因此,无论出于何种原因,手动创建 json 文件(新建 -> 文本文件 -> 重命名)似乎会抛出 Visual Studio 并使其认为它不是 json 文件直接设置为 json 文件中的架构时(但 VS 能够很好地读取它,正如它如何通过我的 "test" [= 从我的主架构文件中提取信息所证明的那样24=] 模式文件,然后进入使用该模式的实际 json 文件)。

去计算...