Skip to content

Commit ad86475

Browse files
authored
Merge pull request #4 from claui/combining-characters
Add support for combining characters
2 parents 7b8c085 + 5684b4e commit ad86475

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

json.applescript

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,22 @@ end
2525

2626
on encodeString(value)
2727
set rv to ""
28-
repeat with ch in value
29-
if id of ch = 34
28+
set codepoints to id of value
29+
30+
if (class of codepoints) is not list
31+
set codepoints to {codepoints}
32+
end
33+
34+
repeat with codepoint in codepoints
35+
set codepoint to codepoint as integer
36+
if codepoint = 34
3037
set quoted_ch to "\\\""
31-
else if id of ch = 92 then
38+
else if codepoint = 92 then
3239
set quoted_ch to "\\\\"
33-
else if id of ch >= 32 and id of ch < 127
34-
set quoted_ch to ch
40+
else if codepoint >= 32 and codepoint < 127
41+
set quoted_ch to character id codepoint
3542
else
36-
set quoted_ch to "\\u" & hex4(id of ch)
43+
set quoted_ch to "\\u" & hex4(codepoint)
3744
end
3845
set rv to rv & quoted_ch
3946
end

tests.applescript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ 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("u" & "̈"), "\"u\\u0308\"")
3536
assert_eq(json's encode("\"bar\""), "\"\\\"bar\\\"\"")
3637
assert_eq(json's encode("\\"), "\"\\\\\"")
3738

0 commit comments

Comments
 (0)