如何说服VS代码在JSON文件中接受#作为注释? [英] How to convince VS code to accept # as comment in JSON files?

查看:569
本文介绍了如何说服VS代码在JSON文件中接受#作为注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些特殊文件,其中包含与# comments混合的JSON数据.

We have special files that contain JSON data mixed with # comments.

我认为我需要使用以下方法增强Code的json.settings文件:

I figured I need to enhance Code's json.settings file with:

"files.associations": {
    "*.ourextension": "jsonc"
}

,但随后我发现 jsconc.

but then I discovered that jsconc is about JSON data with // comments.

是否有一种方便的方法来获取VS代码以接受JSON数据中的# comments?

Is there a convenient way to get VS code to accept # comments in JSON data?

VS代码可以识别jsconc语言,它会向我显示此错误消息:

VS code recognizes the jsconc language, it gives me this error message:

它也接受//注释:

添加////给我第一行 green ,现在第二行得到第一个错误(因为以#开头).

adding the // got me a green first line, and now the second line gets the first error (because starting with #).

推荐答案

一个不同的答案:可能可以添加这样的新语言定义,但这需要大量工作.我还可以快速查看一下是否可以简单地为VS代码附带的jsonc更改相应的json配置文件,但是该文件相当复杂,并且可能在下一次VS代码更新中被覆盖.

A distinct non-answer: it might be possible to add a such a new language definition, but it would require quite a bit of work. I also had a quick look if I could simply change the corresponding config json file for jsonc that ships with VS code, but that file is rather complex, and would probably be overridden with the next VS code update.

因此,一种直接的解决方法.两个脚本将一种命令样式替换为另一种:

Thus a straight forward workaround. Two scripts to replace one command style with the other:

#!/bin/sh

# a helper script that turns all # into //
# with the syntax that works for sed on MAC OS
for file in "$@"
do
    sed -i '' -e 's,#,//,g' $file
done

考虑到我们的特定要求,并不完全方便,但快速而强大.

Not exactly convenient, but fast and robust, given our specific requirements.

这篇关于如何说服VS代码在JSON文件中接受#作为注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆