Skip to content

Commit 7b8c085

Browse files
committed
Merge pull request #3 from winny-/master
Escape \ and " in JSON strings
2 parents 7e99386 + 44afa49 commit 7b8c085

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

json.applescript

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ end
2626
on encodeString(value)
2727
set rv to ""
2828
repeat with ch in value
29-
if id of ch >= 32 and id of ch < 127
29+
if id of ch = 34
30+
set quoted_ch to "\\\""
31+
else if id of ch = 92 then
32+
set quoted_ch to "\\\\"
33+
else if id of ch >= 32 and id of ch < 127
3034
set quoted_ch to ch
3135
else
3236
set quoted_ch to "\\u" & hex4(id of ch)

tests.applescript

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ assert_eq(json's encode("foo"), "\"foo\"")
3232
assert_eq(json's encode(""), "\"\"")
3333
assert_eq(json's encode("\n"), "\"\\u000a\"")
3434
assert_eq(json's encode("ș"), "\"\\u0219\"")
35+
assert_eq(json's encode("\"bar\""), "\"\\\"bar\\\"\"")
36+
assert_eq(json's encode("\\"), "\"\\\\\"")
3537

3638
assert_eq(json's encode({1, 2, 3}), "[1, 2, 3]")
3739

0 commit comments

Comments
 (0)