You need to build your own version of the org.json.JSONObject class if you want it to escape all non-ASCII characters.
The signature of the method you need to modify is
public static Writer quote(String string, Writer w) throws IOException
it is declared inside JSONObject.java. It is the method responsible of formatting all string values inside the produced json strings. it loops over all the characters of the source string and emits the corresponding output characters.
What you are looking for is in the "default" section of the switch statement.
the original code (at least in the sources I am watching right now) looks like this:
default:
if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
|| (c >= '\u2000' && c < '\u2100')) {
w.write("\\u");
hhhh = Integer.toHexString(c);
w.write("0000", 0, 4 - hhhh.length());
w.write(hhhh);
} else {
w.write(c);
}
you need to change the "if" test to match all the characters you want to be escaped.
this does what you want:
default:
if (c < ' ' || c >= '\u0080') {
w.write("\\u");
hhhh = Integer.toHexString(c);
w.write("0000", 0, 4 - hhhh.length());
w.write(hhhh);
} else {
w.write(c);
}
Hope this helps.
P.S: I run into your question because I met your same problem: the json strings I am generating need to travel through a system that accepts only ascii characters and mangles any character >127.