多行字符串的JSON和模板文字

san*_*zti 5 json multiline ecmascript-6 template-literals

我想在JSON文件中存储一些sql字符串.这就是JSON文件在我的理想世界中的样子

[
  "select t.index1, t.title, t.description, t.insertDate
  from myTable t
  join anotherTable t2 on t.index1 = t2.fIndex
  where t2.active = 1",
  ...
]
Run Code Online (Sandbox Code Playgroud)

明显的限制是JSON和javascript不支持多行字符串.

我想避免这样做:

[
  "select t.index1, t.title, t.description, t.insertDate\nfrom myTable t\njoin anotherTable t2 on t.index1 = t2.fIndex\nwhere t2.active = 1",
  ...
]    
Run Code Online (Sandbox Code Playgroud)

我看到一个人这样做了,这也不理想:

[
  ["select t.index1, t.title, t.description, t.insertDate",
  "from myTable t",
  "join anotherTable t2 on t.index1 = t2.fIndex",
  "where t2.active = 1"],
  ...
]
Run Code Online (Sandbox Code Playgroud)

然后在他使用的翻译程序中myMultilineStringInArrayForm.join("\n").

我想知道是否有人为此目的有任何关于模板字符串工作(或计划在将来工作)的信息,如下所示:

[
  `select t.index1, t.title, t.description, t.insertDate
  from myTable t
  join anotherTable t2 on t.index1 = t2.fIndex
  where t2.active = 1`,
  ...
]
Run Code Online (Sandbox Code Playgroud)

请注意,我-稠反引号(`)像模板字面从ES6,明显的变量插值不会为存储JSON工作,但这种文字的多特征是什么,我感兴趣的东西.

请注意,这个问题类似于这个6岁的问题:JSON中的多行字符串

我再问一遍,因为我想知道`自那时以来语法是否已计划好或已经有效.

我很感激这方面的帮助

san*_*zti 2

看来我最好的选择是拥有可读的存储格式是 XML。我试图远离 XML,并认为由于`语法原因,JSON 将来会有多行字符串,但没有

  • 看起来 yaml 最适合这个。我会尝试说服我的团队改用 yaml。如果您感兴趣并且不知道 yaml 是什么,请从这里开始:https://www.json2yaml.com/ (2认同)