kali kali - 5 months ago 162
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

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("\\", "\\\\");
Comments