cat
cat

Reputation: 31

Regex to remove certain characters not working on json string obtained from pojo using gson

I am using gson to convert pojo to json string. On that string I want to remove certain characters and then want to convert back to pojo. My goal is to remove those characters from pojo. I want this function to be generic as this will be used on different type of pojos.

I have tried using jackson and gson both to convert pojo to string but none of them worked

public <T> T sanitise(T object, Class<T> class){
    return gson.fromJson(gson.toJson(object).replaceAll("[\\s]", ""), class);
}

I am getting this : {"field1":"entry","field2":"\tentry\t","field3":"entry\tentry","field4":["\tentry\t","\tentry\tentry\t","entry"]} output from gson.toJson(pojo, pojoClass).

I want this : {"field1":"entry","field2":"entry","field3":"entryentry","field4":["entry","entryentry","entry"]} .

pojo is object of some pojoClass.

.replaceAll("[\\s]", "") works when I pass string "\t\r\ndummy" but not working with output of gson.toJson()

Upvotes: 0

Views: 378

Answers (1)

Andreas
Andreas

Reputation: 159106

You say it works when you pass Java string literal "\t\r\ndummy", but that's because the \t, \r, and \n have been converted to TAB, CR, and LF characters by the Java compiler, resulting in string <TAB><CR><LF>dummy.

You say it doesn't work when using regex "\\s" with input that contains "field2":"\tentry\t", but that's because the \t there is the two characters \ and t, not the TAB character.

If you want to eliminate the text \t, then you need regex "\\\\t", which is a Java string literal for the regex \\t, which means the \ character followed by the t character.

Use replaceAll("\\\\t", "") instead of replaceAll("[\\s]", "").

Upvotes: 4

Related Questions