Added basic support for backslash escaping for json writer, parser implementation is incomplete
This commit is contained in:
parent
0bd94eeebf
commit
ddaac6163a
2 changed files with 16 additions and 4 deletions
|
|
@ -108,6 +108,7 @@ public class JSONParser{
|
|||
end.i = 0;
|
||||
break;
|
||||
// Parse String
|
||||
// TODO: Support double backslash escaping
|
||||
case '\"':
|
||||
root = new DataNode(DataType.String);
|
||||
StringBuilder str = new StringBuilder();
|
||||
|
|
@ -131,8 +132,10 @@ public class JSONParser{
|
|||
root = new DataNode(DataType.Boolean);
|
||||
else if( NUMBER_PATTERN.matcher(data).matches() )
|
||||
root = new DataNode(DataType.Number);
|
||||
else
|
||||
else {
|
||||
root = new DataNode(DataType.String);
|
||||
data = unEscapeString(data);
|
||||
}
|
||||
root.set(data);
|
||||
break;
|
||||
}
|
||||
|
|
@ -140,5 +143,9 @@ public class JSONParser{
|
|||
return root;
|
||||
}
|
||||
|
||||
private static String unEscapeString(String str){
|
||||
// Replace two backslash with one
|
||||
return str.replaceAll("\\\\\\\\", "\\\\");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ public class JSONWriter{
|
|||
out.append(", ");
|
||||
String key = it.next();
|
||||
out.append('\"');
|
||||
out.append(key);
|
||||
out.append(escapeString(key));
|
||||
out.append("\": ");
|
||||
write(root.get(key));
|
||||
first = false;
|
||||
|
|
@ -104,7 +104,7 @@ public class JSONWriter{
|
|||
default:
|
||||
if(root.getString() != null && root.getType() == DataType.String){
|
||||
out.append('\"');
|
||||
out.append(root.toString());
|
||||
out.append(escapeString(root.toString()));
|
||||
out.append('\"');
|
||||
} else
|
||||
out.append(root.toString());
|
||||
|
|
@ -112,6 +112,11 @@ public class JSONWriter{
|
|||
}
|
||||
}
|
||||
|
||||
private static String escapeString(String str){
|
||||
// Replace one backslash with two
|
||||
return str.replaceAll("\\\\", "\\\\\\\\");
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the internal stream
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue