summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar majorseitan@blockfreie.org <majorseitan@blockfreie.org>2018-04-14 21:56:09 -0400
committerGravatar majorseitan@blockfreie.org <majorseitan@blockfreie.org>2018-04-14 22:00:10 -0400
commit2bc51bd866b52bc738f259ffe6e9fb8f6068a6b6 (patch)
treedf350f2ffd18c9848e4679243de395ae42d2b957 /lib
parent0cadb1a719bc515af2449ac966e545a6599aee4d (diff)
Handling of JSON escape characters
1. Handle escape sequence chars \t \n \r 2. Fail on unsupported escape characters. Instead of skipping \ on unsupported sequences it now fails.
Diffstat (limited to 'lib')
-rw-r--r--lib/ur/json.ur22
1 files changed, 17 insertions, 5 deletions
diff --git a/lib/ur/json.ur b/lib/ur/json.ur
index 9288a6dd..1e3e3f39 100644
--- a/lib/ur/json.ur
+++ b/lib/ur/json.ur
@@ -46,10 +46,14 @@ fun escape s =
let
val ch = String.sub s 0
in
- (if ch = #"\"" || ch = #"\\" then
- "\\" ^ String.str ch
- else
- String.str ch) ^ esc (String.suffix s 1)
+ (case ch of
+ #"\n" => "\\n"
+ | #"\r" => "\\r"
+ | #"\t" => "\\t"
+ | #"\"" => "\\\""
+ | #"\'" => "\\\'"
+ | x => String.str ch
+ ) ^ esc (String.suffix s 1)
end
in
"\"" ^ esc s
@@ -90,7 +94,15 @@ fun unescape s =
if i+1 >= len then
error <xml>JSON unescape: Bad escape sequence: {[s]}</xml>
else
- String.str (String.sub s (i+1)) ^ unesc (i+2)
+ (case String.sub s (i+1) of
+ #"n" => "\n"
+ | #"r" => "\r"
+ | #"t" => "\t"
+ | #"\"" => "\""
+ | #"\'" => "\'"
+ | x => error <xml>JSON unescape: Bad escape char: {[x]}</xml>)
+ ^
+ unesc (i+2)
| _ => String.str ch ^ unesc (i+1)
end
in