0

Does VSCode provides any means to parse json files with comments without using external library?

I'm trying get list of keybindings for the extension and need to parse keybindings.json file, which contains comments

I'm currently using JSON5 package, but would like to get rid of it:

//get keybindings
const keybindings = new Promise(resolve => {
  //default keybindings
  const data = require("../package.json").contributes.keybindings.reduce((a,b)=>(a[b.command]=b.key,a),{});
  const parse = list => {
    for(let i = 0; i < list.length; i++) {
      if (list[i].command in data)
        data[list[i].command] = list[i].key;
    }
    for(let i in data) {
      //capitalize first letter
      data[i] = data[i].replace(/\w+/g, w => (w.substring(0,1).toUpperCase()) + w.substring(1));
      //add spaces around "+"
      // data[i] = data[i].replace(/\+/g, " $& ");
    }

    Object.assign(keybindings, data);
    resolve(data);
  };
  const path = {
    windows: process.env.APPDATA + "/Code",
    macos: process.env.HOME + "/Library/Application Support/Code",
    linux: process.env.HOME + "/config/Code"
  }[{
    aix: "linux",
    darwin: "macos",
    freebsd: "linux",
    linux: "linux",
    openbsd: "linux",
    sunos: "linux",
    win32: "windows"
  }[process.platform]||"windows"];
  const file = ((process.env.VSCODE_PORTABLE ? process.env.VSCODE_PORTABLE + "/user-data/User/" : path) + "/User/keybindings.json")
    .replace(/\//g, process.platform == "win32" ? "\\" : "/");

  //read file
  workspace.openTextDocument(file).then(doc => {
    //we can't use JSON.parse() because file may contain comments
    const JSON5 = require("json5").default;
    parse(JSON5.parse(doc.getText()));
  }).catch(er => {
    parse([]);
  });
});
Inigo
  • 12,186
  • 5
  • 41
  • 70
vanowm
  • 9,466
  • 2
  • 21
  • 37
  • JSON doesn't support comments. However, YAML does, and is mostly a superset of JSON, so there's a good chance if you can parse YAML, it will work on a JSON file. YAML comments start with "#" instead of "//". – John Bayko Sep 30 '22 at 04:48
  • Sounds like an external library... – vanowm Sep 30 '22 at 05:13
  • 1
    I think you are stuck with an external library. There is also the `jsonc-parser` written by a vscode team member which works well on keybindings (I use it in a completionProvider to provide available keys and args). – Mark Sep 30 '22 at 20:10
  • @Mark, yes, that's the library VSCode is using. https://github.com/microsoft/vscode/issues/162434#issuecomment-1263546283 – vanowm Sep 30 '22 at 21:10
  • Is there a reason you don't want to use the JSON5 library? As json5.org's home page points out: `"JSON5 was started in 2012, and as of 2022, now gets >65M downloads/week, ranks in the top 0.1% of the most depended-upon packages on npm, and has been adopted by major projects like Chromium, Next.js, Babel, Retool, WebStorm, and more. It's also natively supported on Apple platforms like MacOS and iOS."` – Inigo Dec 16 '22 at 05:33
  • @Inigo it's not just JSON5 I don't want to use, but external packages all together if I could help it. But since there is no other way but to use external library, then I'd pick whichever is the smallest/fastest that does one job: read text file and convert its content into an object. – vanowm Dec 16 '22 at 15:22
  • Sorry, i missed the fact above that you found a way to use the library VSCode uses. Otherwise I was gonna say rolling your own isn't gonna be any smaller unless you know the precise subset of JSON5 VSCode assumes. – Inigo Dec 16 '22 at 16:23

0 Answers0