json string escapes

473 visualizações
Pular para a primeira mensagem não lida

sluramod

não lida,
16 de ago. de 2006, 13:50:0616/08/2006
para 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

não lida,
16 de ago. de 2006, 16:20:1216/08/2006
para Google Web Toolkit
Would you mind describing the particular bug you have in mind?

Thanks,
Scott

sluramod

não lida,
16 de ago. de 2006, 17:21:3816/08/2006
para 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

não lida,
17 de ago. de 2006, 16:02:3117/08/2006
para 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

não lida,
17 de ago. de 2006, 23:07:4017/08/2006
para Google-We...@googlegroups.com
Thanks, guys.  We'll get right on this.

Scott

Responder a todos
Responder ao autor
Encaminhar
0 nova mensagem