Updated Gson escaper to not escape unicode characters. This is in response to Issue 80.

This commit is contained in:
Inderjeet Singh 2008-12-19 20:41:53 +00:00
parent 53f7d26b6d
commit e4fdea0fc1
2 changed files with 11 additions and 2 deletions

View File

@ -77,7 +77,7 @@ class Escaper {
int codePoint = Character.codePointAt(plainText, i); int codePoint = Character.codePointAt(plainText, i);
charCount = Character.charCount(codePoint); charCount = Character.charCount(codePoint);
if (!((codePoint < 0x20 || codePoint >= 0x7f) if (!(isControlCharacter(codePoint)
|| mustEscapeCharInJsString(codePoint))) { || mustEscapeCharInJsString(codePoint))) {
continue; continue;
} }
@ -103,6 +103,9 @@ class Escaper {
case '\\': case '\\':
out.append("\\\\"); out.append("\\\\");
break; break;
case '/':
out.append("\\/");
break;
case '"': case '"':
out.append('\\').append((char) codePoint); out.append('\\').append((char) codePoint);
break; break;
@ -117,6 +120,12 @@ class Escaper {
out.append(plainText, pos, len); out.append(plainText, pos, len);
} }
private static boolean isControlCharacter(int codePoint) {
return codePoint < 0x20
|| codePoint == 0x7f
|| (codePoint >= 0x80 && codePoint <= 0x9f);
}
private static void appendHexJavaScriptRepresentation(int codePoint, Appendable out) private static void appendHexJavaScriptRepresentation(int codePoint, Appendable out)
throws IOException { throws IOException {
if (Character.isSupplementaryCodePoint(codePoint)) { if (Character.isSupplementaryCodePoint(codePoint)) {

View File

@ -53,7 +53,7 @@ public class InternationalizationTest extends TestCase {
public void testStringsWithUnicodeChineseCharactersSerialization() throws Exception { public void testStringsWithUnicodeChineseCharactersSerialization() throws Exception {
String target = "\u597d\u597d\u597d"; String target = "\u597d\u597d\u597d";
String json = gson.toJson(target); String json = gson.toJson(target);
String expected = "\"\\u597d\\u597d\\u597d\""; String expected = "\"\u597d\u597d\u597d\"";
assertEquals(expected, json); assertEquals(expected, json);
} }