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

时间:2021-03-07 18:14:59

I want to store some sql strings in a JSON file. This is how the JSON file looks in my ideal world

我想在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",
  ...
]

The obvious limitation is that JSON and javascript don't support multiline strings.

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

I want to avoid doing this:

我想避免这样做:

[
  "select t.index1, t.title, t.description, t.insertDate\nfrom myTable t\njoin anotherTable t2 on t.index1 = t2.fIndex\nwhere t2.active = 1",
  ...
]    

I saw a guy that did this, which is not ideal either:

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

[
  ["select t.index1, t.title, t.description, t.insertDate",
  "from myTable t",
  "join anotherTable t2 on t.index1 = t2.fIndex",
  "where t2.active = 1"],
  ...
]

And then in his interpreter program he used myMultilineStringInArrayForm.join("\n").

然后在他的解释程序中,他使用了myMultilineStringInArrayForm.join(“\ n”)。

I was wondering if anyone has any information about Template strings working (or planned to work in the future) for such purpose, like this:

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

[
  `select t.index1, t.title, t.description, t.insertDate
  from myTable t
  join anotherTable t2 on t.index1 = t2.fIndex
  where t2.active = 1`,
  ...
]

Notice I iused backticks (`) like in Template Literals from ES6, obviously the interpolation of variables would not work for stored JSON, but the multiline feature of this literals is what I am interested in.

请注意我在ES6中的模板文字中使用了反引号(`),显然变量的插值对于存储的JSON不起作用,但是这个文字的多行特征是我感兴趣的。

Notice that this question is similar to this 6 year old question: Multiline strings in JSON

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

I asked again because I wanted to know if the ` syntax was planned or already worked since that time.

我又问了一遍,因为我想知道`语法是否已经计划好或者已经有效了。

I appreciate the help with this

我很感激这方面的帮助

2 个解决方案

#1


0  

Looks like my best choice for having a readable storage format is XML. I was trying to stay away from XML and thought that JSON had multiline strings in its future due to the ` syntax, but no

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

#2


0  

You may use JSON6 which supports backticks.

您可以使用支持反引号的JSON6。

It seems to be a small one-person-project, so maybe the better supported JSON5 with support for Strings spanning multiple lines by escaping new line characters may be a better choice.

它似乎是一个小的单人项目,所以也许更好的支持JSON5支持通过转换新行字符跨越多行的字符串可能是一个更好的选择。

#1


0  

Looks like my best choice for having a readable storage format is XML. I was trying to stay away from XML and thought that JSON had multiline strings in its future due to the ` syntax, but no

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

#2


0  

You may use JSON6 which supports backticks.

您可以使用支持反引号的JSON6。

It seems to be a small one-person-project, so maybe the better supported JSON5 with support for Strings spanning multiple lines by escaping new line characters may be a better choice.

它似乎是一个小的单人项目,所以也许更好的支持JSON5支持通过转换新行字符跨越多行的字符串可能是一个更好的选择。