json string escapes

462 views
Skip to first unread message

sluramod

unread,
Aug 16, 2006, 1:50:06 PM8/16/06
to Google Web Toolkit
Please fix a bug with string escapes. Use code published here
http://www.JSON.org/java/json_simple.zip as a reference.

Also, it would be nice to have JSONInt class or something similar.

Thank you,
Alexei

Scott Blum

unread,
Aug 16, 2006, 4:20:12 PM8/16/06
to Google Web Toolkit
Would you mind describing the particular bug you have in mind?

Thanks,
Scott

sluramod

unread,
Aug 16, 2006, 5:21:38 PM8/16/06
to Google Web Toolkit
Scott,

Here is the code I'm referring to:

public static String escape(String s){
if(s==null)
return null;
StringBuffer sb=new StringBuffer();
for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
switch(ch){
case '"':
sb.append("\\\"");
break;
case '\\':
sb.append("\\\\");
break;
case '\b':
sb.append("\\b");
break;
case '\f':
sb.append("\\f");
break;
case '\n':
sb.append("\\n");
break;
case '\r':
sb.append("\\r");
break;
case '\t':
sb.append("\\t");
break;
case '/':
sb.append("\\/");
break;
default:
if(ch>='\u0000' && ch<='\u001F'){
String ss=Integer.toHexString(ch);
sb.append("\\u");
for(int k=0;k<4-ss.length();k++){
sb.append('0');
}
sb.append(ss.toUpperCase());
}
else{
sb.append(ch);
}
}
}//for
return sb.toString();
}

Alexei

vsmak

unread,
Aug 17, 2006, 4:02:31 PM8/17/06
to Google Web Toolkit
Strings with escaped characters aren't written out properly because
JSONString's toString() only replaces the character '\\' and not other
escaped characters like '\n', '\r', etc.

>From JSONString:
public String toString() {
// Backslashes and quotes must be escaped
String s = value;
// replaces \ with \\
s = s.replaceAll("\\\\", "\\\\\\\\");
// replaces " with \"
s = s.replaceAll("\\\"", "\\\\\"");
// wrap the result in quotes
return "\"" + s + "\"";
}

Scott Blum

unread,
Aug 17, 2006, 11:07:40 PM8/17/06
to Google-We...@googlegroups.com
Thanks, guys.  We'll get right on this.

Scott

Reply all
Reply to author
Forward
0 new messages