]> xenbits.xensource.com Git - xenclient/toolstack.git/commitdiff
json: escape characters when printing
authorVincent Hanquez <vincent.hanquez@eu.citrix.com>
Fri, 5 Jun 2009 14:09:00 +0000 (15:09 +0100)
committerVincent Hanquez <vincent.hanquez@eu.citrix.com>
Fri, 5 Jun 2009 14:09:00 +0000 (15:09 +0100)
libs/json/json.ml

index c4762a527c6482120b8125770dd41c0359c1eae3..ed369a59fba20952f7c29520e0f19ae9dddd80cf 100644 (file)
@@ -42,8 +42,21 @@ let rec list_iter_between f o a =
 let escape_string s =
        let buf = Buffer.create 80 in
        Buffer.add_string buf "\"";
-       (* FIXME: we need to make sure it doesn't contains deny characters *)
-       Buffer.add_string buf s;
+       for i = 0 to String.length s - 1
+       do
+               let x =
+                       match s.[i] with
+                       | '\n'   -> "\\n"
+                       | '\t'   -> "\\t"
+                       | '\r'   -> "\\r"
+                       | '\b'   -> "\\b"
+                       | '/'    -> "\\/"
+                       | '"'    -> "\\\""
+                       | '\x0c' -> "\\f"
+                       | c      -> String.make 1 c
+                       in
+               Buffer.add_string buf x
+       done;
        Buffer.add_string buf "\"";
        Buffer.contents buf