1

com.google.gwt.json.client.JSONParser.parseStrict(jsonStr) is throwing a syntax error when the json string contains non-printable/recognized Unicode characters. Hence, I am trying to remove non-printable Unicode characters in client side. Following How can I replace non-printable Unicode characters in Java?, I'm trying to implement this code in client side, but Character.getType(codePoint) Isn't client compatible.

Is there any way to overcome this issue? Any other way to get the character type in client side? Any other suggestion on how to solve the main problem?

Many Thanks! David.

By the way, I've tried to use my_string.replaceAll("\\p{C}", "?") code and it worked on server side but not on client side.

Community
  • 1
  • 1

3 Answers3

0

You can add a native JS method and use a regex inside it to replace every non-printable non-ASCII character, like this:

private native String replaceUnprintableChars(String text, String replacement) /*-{
    return text.replace(/[^\u0020-\u007E]/g, replacement);
}-*/;


// Somewhere else....
String replacedText = replaceUnprintableChars(originalString, "?");

The regex shown will replace every non-printable or non-ASCII character with the replacement string (e.g. "?"). If you want to include non-ASCII printable characters (latin, for example) then you can tweak the expression to broaden the range.

Of course, you can do this with Java regexes too:

text.replaceAll("[^\\u0020-\\u007E]", "?");

But I came up with the JS solution first, don't know why!

walen
  • 7,103
  • 2
  • 37
  • 58
0

com.google.common.base.CharMatcher in Guava looks promissing. I have not tested this, but the class is annotated @GwtCompatible.

There is a guide to using it in the StringsExplained article in the Guava docs.

bclarkreston
  • 638
  • 5
  • 18
  • I don't think that this code is client complaint. My problem was removing those characters in GWT client side. – David Yanay May 14 '17 at 08:55
  • That's surprising, when Guava classes are annotated with @GwtCompatible, that is supposed to mean they are emulated. Did you try this and see it not work? – bclarkreston May 15 '17 at 14:50
0

I've changed the use of com.google.gwt.json.client.JSONParser.parseStrict(jsonStr) with com.google.gwt.json.client.JSONParser.parseLenient(jsonStr) and the parser was able to handle those non-printable/recognized Unicode characters like in server side.

I feel comfortable using JSONParser.parseLenient since the jsonStrdoesn't come from user input.