Jackson JSON 是否进行特殊的字符转义?

发布于 2024-10-11 20:44:27 字数 370 浏览 4 评论 0原文

我假设杰克逊会在序列化过程中自动转义特殊字符,即将“/path/”序列化为“\/path\/”。情况似乎并非如此 - 至少在 1.6 中是开箱即用的:

@Test
public void testJacksonSerialize() throws Exception
{
    ObjectMapper om = new ObjectMapper();
    assertEquals("\\/path\\/", om.writeValueAsString("/path/"));
}

...失败 - 生成的输出是“/path/”。我是否必须编写自己的序列化程序,或者有没有办法在 Jackson 中启用特殊字符转义?

谢谢, -尼基塔

I was assuming that Jackson would automatically escape special characters during serialization i.e. serialize "/path/" as "\/path\/". It appears not to be the case - at least out of the box with 1.6:

@Test
public void testJacksonSerialize() throws Exception
{
    ObjectMapper om = new ObjectMapper();
    assertEquals("\\/path\\/", om.writeValueAsString("/path/"));
}

...fails - the output produced is "/path/". Do I have to write my own serializer or is there a way to enable special char escaping in Jackson?

thanks,
-nikita

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

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

发布评论

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

评论(3

情话墙 2024-10-18 20:44:27

杰克逊只逃避强制性的事情。 “/”不是你必须转义的东西,因此它不是。这是根据 JSON 规范。

现在:如果您绝对想要转义,您可以使用方法来写入“原始”内容或值(在这种情况下,Jackson 不会进行任何处理并在输出中转储 String)。

但你真的需要这样的逃避吗?我知道有些生成器确实会转义它(出于我未知的原因),但没有解析器期望它,所以保留斜杠不转义应该没问题。这与显然必须转义的反斜杠不同。

Jackson only escapes mandatory things. "/" is not something you must escape, hence it is not. This as per JSON specification.

Now: if you absolutely want escaping, you can use methods to write "raw" content or values (in which case Jackson does no processing whatsoever and dumps String in output).

But do you really need such escaping? I know some generators do escape it (for reasons unknown to me), but no parser expects it so it should be just fine to leave slashes unescaped. This is different from backslashes that obviously must be escaped.

月亮邮递员 2024-10-18 20:44:27

斜杠“/”在 JSON 中不需要转义,因为它没有特殊含义。然而,由于以下原因,JSON允许转义斜线。

如果将 JSON 文本直接转储到

话虽这么说,我不知道你的问题的直接答案(如何绝对地在杰克逊逃脱)。

The slash "/" doesn't need to be escaped in JSON because it has no special meaning. Yet JSON allows the slash to be escaped for the following reason.

If you dump a JSON text right into a <SCRIPT> element of an HTML text, you have to make sure that the two-character-sequence "</" does not occur in the text. That sequence would end the script element immediately according to HTML rules. But if the JSON text reads "<\/", this has the same meaning to JSON while not interferring with HTML rules. Consequently, some JSON generators escape the slash if and only if it's preceded by a less-than-sign.

That being said, I don't know the direct answer to your question (how to absolutely do the escaping in Jackson).

千鲤 2024-10-18 20:44:27

对于某些浏览器的工具提示,换行符将不起作用。
不工作 \r\n 不工作或 \n 不工作

使用双反斜杠跳过字符

解决方案:使用 '\\r\\n' 代替 '\r\n' ,

它会解决你的问题。

New line character will not work in case of Tooltip with some browsers.
Not working \r\n Not working or \n Not working

Use double backslash to skip characters

Solution : use '\\r\\n' in place of '\r\n' ,

it will solve your problem.

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