java Jackson:在将对象序列化为 json 时自定义转义双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27403903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:46:00  来源:igfitidea点击:

Hymanson: customization escaping of double quotes while serialize an object to json

javajsonserializationHymanson

提问by kali

By default Hymanson escape double quotes by backslash: \". So the json object looks like

默认情况下,Hymanson 通过反斜杠转义双引号:\"。所以 json 对象看起来像

{"title": "Testing \"double quotes\""}

I want to escape double quotes by two backslashes: \\". Like this

我想用两个反斜杠转义双引号:\\"。像这样

{"title": "Testing \"double quotes\""}

How to customize Hymanson serialization strategy for double quotes?

如何自定义双引号的Hymanson序列化策略?

回答by Aaryn Tonita

This will produce invalid JSON, so Hymanson isn't going to help you do that. Assuming that you have no reverse solidus in the member names, you can just do it after outputting the actual string value:

这将产生无效的 JSON,因此 Hymanson 不会帮助您做到这一点。假设成员名称中没有反向实线,则可以在输出实际字符串值后执行此操作:

String invalid = mapper.writeValueAsString(object).replace("\", "\\");