VSCode支持IntelliSense,可以方便开发者获得提醒,快速编写代码。JSON常常被用作配置文件。那么如何针对特定的开发环境来定制需要的JSON IntelliSense呢?
JSON Schema
VSCode允许用户配置JSON Schema。JSON schema用于描述和验证JSON数据,本身也是JSON。
这里是一个简单的例子。
{
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName"]
}
我给Dynamsoft Barcode Reader的模板文件写了一个简单的JSON schema。
{
"title": "JSON schema for DBR configuration files",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "A representation of Dynamsoft Barcode Reader template.",
"type": "object",
"required": ["Version", "ImageParameters"],
"properties": {
"Version": {
"description": "The template version number.",
"type": "string",
"enum": [
"1.0"
]
},
"ImageParameters": {
"description": "Parameters for barcode detection",
"type": "object",
"required": [
"Name"
],
"properties": {
"Name": {
"description": "The name of the ImageParameters object",
"type": "string",
"maxLength": 50,
"minLength": 1 },
"Description": {
"description": "The description of the ImageParameters object",
"type": "string" },
"BarcodeFormatIds": {
"description": "Sets which types of barcode to be read. Barcode types can be combined",
"type": "array",
"items": { "type": "string", "enum": [ "All", "OneD", "CODE_39", "CODE_128", "CODE_93", "CODABAR", "ITF", "EAN_13", "EAN_8", "UPC_A", "UPC_E", "INDUSTRIAL_25", "PDF417", "QR_CODE", "DATAMATRIX" ] } },
"MaxBarcodesCount": {
"description": "Sets the maximum number of barcodes to read",
"type": "number",
"maximum": 2147483647,
"minimum": 1,
"default": 2147483647 },
"Timeout": {
"description": "Sets the maximum amount of time (in milliseconds) it should spend searching for a barcode per page",
"type": "number",
"maximum": 2147483647,
"minimum": 0,
"default": 2147483647 },
"ScaleDownThreshold": {
"description": "Sets the threshold value of the image shrinking",
"type": "number",
"maximum": 2147483647,
"minimum": 512,
"default": 2048 },
"DeblurLevel": {
"description": "The blurriness of the barcode",
"type": "number",
"maximum": 9,
"minimum": 0,
"default": 5 }
}
}
}
}
接下来在VSCode中设置一下。
"json.schemas": [
{
"fileMatch": [
"/tpl_*.json"
],
"url": "./dbr.json"
}]
把这个dbr.json(JSON schema)文件放到工程根目录即可。
有了这个,不用担心不知道用什么key和value了。
本文介绍了如何在Visual Studio Code (VSCode) 中利用JSON Schema定制JSON的IntelliSense功能,以提升开发效率。通过创建和应用JSON Schema,开发者可以为特定的配置文件提供自动补全和错误检查,确保键值对的正确性。文中给出了为Dynamsoft Barcode Reader模板文件创建JSON Schema的示例,并说明了将schema文件放入工程根目录的方法。
353

被折叠的 条评论
为什么被折叠?



