kali kali -5 years ago 581
JSON Question

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

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

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

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

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

How to customize jackson serialization strategy for double quotes?

Answer Source

This will produce invalid JSON, so Jackson 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:

String invalid = mapper.writeValueAsString(object).replace("\\", "\\\\");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download