如何在 freemarker 中转义 json 字符串

发布于 2024-11-15 18:51:23 字数 1253 浏览 0 评论 0原文

我们正在使用 Spring MVC 和 freemarker 作为模板语言构建一个 Restful api。我们选择在 freemarker 示例 freemarker.ftl 中构建 json 响应

{
"field1" : "${response.value1}",
"field2" : "${response.value2}"
}

当值中的字符串包含引号(或 JSON 语法中的任何其他字符)时,我们会遇到问题。

问题:如何使用 freemarker 转义这些字符串?

我们已经查看了 ?xml?html 但它们并未涵盖所有相关字符(例如 \)。

编辑: ?js_string 将转义字符串以符合 JavaScript。由于 JSON 基于 JavaScript(JavaScript 对象表示法),因此它可以工作。

编辑2:如果弹出单引号,?js_string 将转义它,这再次导致无效的 JSON。它的修补程序是:

${variable?js_string?replace("\\'", "\'")} 

如果你真的想挑剔:

${variable?js_string?replace("\\'", "\'")?replace("\\>",">")}

或者如果你使用 Spring: http://www.springsurf.org/sites/1.0.0.M3/spring-webscripts/spring-webscripts-documentation/reference/html-single/index.html#js-api-index-org.springframework .extensions.webscripts.json.jsonutils

We are building a restful api using Spring MVC and freemarker as the templating language. We have chosen to build json responses in the freemarker

Example freemarker.ftl:

{
"field1" : "${response.value1}",
"field2" : "${response.value2}"
}

We get a problem when the strings in the values contain quotation marks (or any of the other characters in the JSON syntax).

The question: How can I escape these strings using freemarker?

We have looked at ?xml or ?html but they do not cover all relevant characters (such as \).

EDIT: ?js_string will escape the string to comform with JavaScript. And since JSON is based on JavaScript (JavaScript Object Notation), it will work.

EDIT2: In case a single-quote pops up, ?js_string will escape it which again leads to invalid JSON. The hotfix for it is:

${variable?js_string?replace("\\'", "\'")} 

and if you really want to be picky:

${variable?js_string?replace("\\'", "\'")?replace("\\>",">")}

Alternatively if you use Spring: http://www.springsurf.org/sites/1.0.0.M3/spring-webscripts/spring-webscripts-documentation/reference/html-single/index.html#js-api-index-org.springframework.extensions.webscripts.json.jsonutils

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

装纯掩盖桑 2024-11-22 18:51:23

您正在寻找 ?js_string 运算符。

{
"field1" : "${response.value1?js_string}",
"field2" : "${response.value2?js_string}"
}

这将处理转义引号、反斜杠等。 al为了让你的JS开心。

编辑:我刚刚看到他们在 Freemarker 2.3.19 中引入了 ?json_string 运算符。请参阅此处了解其具体工作原理。而且还有很多庆幸的事...

You're looking for the ?js_string operator.

{
"field1" : "${response.value1?js_string}",
"field2" : "${response.value2?js_string}"
}

That will take care of escaping quotes, backslashes, et. al in order to make your JS happy.

Edit: I just saw that they introduced a ?json_string operator in Freemarker 2.3.19. See here for exactly how it works. And there was much rejoicing...

め可乐爱微笑 2024-11-22 18:51:23

使用 FreeMarker 宏 组合上述所有答案,同时使模板更具可读性和可维护:

<#macro json_string string>${string?js_string?replace("\\'", "\'")?replace("\\>", ">")}</#macro>
{
"field1" : "<@json_string "${response.value1}"/>",
"field2" : "<@json_string "${response.value2}"/>"
}

如果您想在多个模板中重用该宏,请将其放入自己的文件中并 包含文件而不是复制宏:

<#include "/path/to/macro.ftl">

Use a FreeMarker macro to combine all of the answers above, while making the template more readable and maintainable:

<#macro json_string string>${string?js_string?replace("\\'", "\'")?replace("\\>", ">")}</#macro>
{
"field1" : "<@json_string "${response.value1}"/>",
"field2" : "<@json_string "${response.value2}"/>"
}

If you want to reuse the macro in multiple templates, put it in its own file and include the file instead of duplicating the macro:

<#include "/path/to/macro.ftl">
¢蛋碎的人ぎ生 2024-11-22 18:51:23

从 FreeMarker 2.3.32 开始,它变得更加容易,因为 ?c 现在引用字符串:

{
"field1" : ${response.value1?c},
"field2" : ${response.value2?c}
}

请注意,${...} 周围没有引号!这就是重点;如果值是字符串(而不是数字或布尔值,它们的格式不添加引号),则 ?c 将它们放在那里。

在 2.3.32 之前,上述内容会因错误而终止,因为 ?c 不允许输入字符串,只能输入数字和布尔值。

从2.3.32开始,您还可以使用?cn代替?c,它代表“?c nullable”。然后,如果值丢失/null,它将输出null,不带引号。因此,您也不必通过有条件地打印引号来使模板复杂化。

另外,最好将 c_format 设置设置为 “JSON”“JavaScript 或 JSON”。 (或者,如果您将 incomplete_improvements 配置设置设置为 2.3.32 或更高版本,则最后一个将是默认值。)

Starting from FreeMarker 2.3.32, it's much easier, as ?c now quotes strings:

{
"field1" : ${response.value1?c},
"field2" : ${response.value2?c}
}

Note that there are no quotes around the ${...}! That's the point; the ?c puts them there if the value is a string (as opposed to a number or a boolean, which are formatted without adding quotation marks).

Before 2.3.32 the above terminates with error, as string input wasn't allowed by ?c, only numbers and booleans.

Starting from 2.3.32, instead of ?c, you can also use ?cn, which stands for "?c nullable". Then if the value is missing/null, it will output null, without the quotes. So you don't have to complicate the template with printing quotes conditionally either.

Also, It's a good idea to set the c_format setting to "JSON", or "JavaScript or JSON". (Or, if you set the incompatible_improvements configuration setting to 2.3.32 or higher anyway, the last will be the default.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文