JSON-java icon indicating copy to clipboard operation
JSON-java copied to clipboard

DEL control character (0x007F) is not escaped

Open reto-hoehener opened this issue 3 years ago • 5 comments

https://github.com/stleary/JSON-java/blame/master/src/main/java/org/json/JSONObject.java#L2080

    System.out.println(java.lang.Character.isISOControl(0x007f));
    System.out.println(new JSONObject("{\"a\":\"\u007f\"}").toString(0));

reto-hoehener avatar Feb 21 '22 15:02 reto-hoehener

Thanks, this does look like a bug. Please feel free to post a PR.

johnjaylward avatar Feb 21 '22 15:02 johnjaylward

adding an additional || java.lang.Character.isISOControl(c) to the if should be sufficient. The function was added in 1.5 so it should be available for all our supported platforms.

johnjaylward avatar Feb 21 '22 15:02 johnjaylward

or better may be to just replace the first 2 check with that function, so the new if would look like:

if (Character.isISOControl(c) || (c >= '\u2000' && c < '\u2100')) {

johnjaylward avatar Feb 21 '22 16:02 johnjaylward

  • or copy the (optimized) implementation of Character.isISOControl(int)
  • or use Character.isISOControl((int) c) in order to save one method call

reto-hoehener avatar Feb 21 '22 16:02 reto-hoehener

I would prefer the Character.isISOControl((int) c) call over the copy/paste so any future changes to the call would be available on a JVM update.

johnjaylward avatar Feb 21 '22 17:02 johnjaylward

Closed due to lack of activity.

stleary avatar Nov 18 '22 01:11 stleary