Json string handling with back slash

3.6k Views Asked by At

I know this question was asked several times before on this forum. But none of it addresses my problem. I am using Jmeter to post json string with content-type "application/json" My Spring mvc app is happy consuming the same and converting it to Java Object. However, when i send a string with a backslash everything seems to fall apart. Here is my Json Input string

    {"signup":{"firstName":"joe", "lastName":"beedu","email":"[email protected]",
"password":"O;\l-wslD6RQ5@M)","confirmPassword":"O;\l-wslD6RQ5@M)", 
"picImage":[81,109,81,109,109,81,109,109,89]}}

Note: password fields have a back slash. More on this later.

Here is my java class for the same.

@JsonRootName(value = "signup")
@XmlRootElement(name = "signup")
public class Signup {

    private String firstName;

    private String lastName;

    private String email;

    private String password;

    private String confirmPassword;

    private byte[] picImage;

}

Removed getters/setters and also validation annotations.

Issue number 1: When i manually escape the password field values with an extra slash like "O;\l-wslD6RQ5@M)" My Spring mvc web app is happy, but doesn't automatically unescape the extra back slash for me. I was expecting my spring converter MappingJackson2HttpMessageConverter to read the json payload and do the magic unescape thing for me, but it doesn't. It will be very ugly if i manually unescape every field in json json objects. Any help here to address this?

Issue number 2: On the client side, i tried several different ways to see if any library automatically escapes the strings in Json object. Given a raw json string like above, is there a way to escape the string that goes into json object? I tried, StringEscapeUtils from apache escapeJson, it didn't work. I tried json-lib too. None of them were able to handle string "O;\l-wslD6RQ5@M)". I had to manually add extra back slash before i add the json string to Http Body.

I would like to use a client side library that escapes json string fields and also on the server side to unescape. I deeply appreciate you guys for reading a lengthy question. I wasted a day on this one. I hope you guys will help me out with an answer or at least a direction.

Thanks Sri

0

There are 0 best solutions below