json:escape chars<=0x1f, python:encode tab

git-svn-id: https://svn.apache.org/repos/asf/incubator/solr/trunk@429137 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2006-08-06 15:29:03 +00:00
parent 75770514c9
commit 95639bc526
1 changed files with 33 additions and 19 deletions

View File

@ -384,6 +384,15 @@ class JSONWriter extends TextResponseWriter {
// it might be more efficient to use a stringbuilder or write substrings // it might be more efficient to use a stringbuilder or write substrings
// if writing chars to the stream is slow. // if writing chars to the stream is slow.
if (needsEscaping) { if (needsEscaping) {
/* http://www.ietf.org/internet-drafts/draft-crockford-jsonorg-json-04.txt
All Unicode characters may be placed within
the quotation marks except for the characters which must be
escaped: quotation mark, reverse solidus, and the control
characters (U+0000 through U+001F).
*/
for (int i=0; i<val.length(); i++) { for (int i=0; i<val.length(); i++) {
char ch = val.charAt(i); char ch = val.charAt(i);
switch(ch) { switch(ch) {
@ -392,15 +401,19 @@ class JSONWriter extends TextResponseWriter {
writer.write('\\'); writer.write('\\');
writer.write(ch); writer.write(ch);
break; break;
/*** the following are not required to be escaped case '\r': writer.write("\\r"); break;
case '\r': case '\n': writer.write("\\n"); break;
case '\n': case '\t': writer.write("\\t"); break;
case '\t': case '\b': writer.write("\\b"); break;
case '\b': case '\f': writer.write("\\f"); break;
case '\f': // case '/':
case '/': default: {
***/ if (ch <= 0x1F) {
default: writer.write(ch); unicodeEscape(writer,ch);
} else {
writer.write(ch);
}
}
} }
} }
} else { } else {
@ -608,17 +621,18 @@ class PythonWriter extends JSONWriter {
case '\\': sb.append('\\'); sb.append(ch); break; case '\\': sb.append('\\'); sb.append(ch); break;
case '\r': sb.append("\\r"); break; case '\r': sb.append("\\r"); break;
case '\n': sb.append("\\n"); break; case '\n': sb.append("\\n"); break;
default: case '\t': sb.append("\\t"); break;
// we don't strictly have to escape these chars, but it will probably increase default:
// portability to stick to visible ascii // we don't strictly have to escape these chars, but it will probably increase
if (ch<' ' || ch>127) { // portability to stick to visible ascii
unicodeEscape(sb, ch); if (ch<' ' || ch>127) {
needUnicode=true; unicodeEscape(sb, ch);
} else { needUnicode=true;
sb.append(ch); } else {
} sb.append(ch);
} }
} }
}
writer.write( needUnicode ? "u'" : "'"); writer.write( needUnicode ? "u'" : "'");
writer.append(sb); writer.append(sb);