json.writer: support escaping unicode > 0x10000. Thanks @jonenst!

db4
John Benediktsson 2015-01-03 08:30:26 -08:00
parent ee8be0a8dd
commit 1ec43d7913
2 changed files with 28 additions and 7 deletions

View File

@ -68,3 +68,6 @@ TUPLE: person first-name age ;
"\0\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\e\x1c\x1d\x1e\x1f"
>json
] unit-test
{ "\"\\ud834\\udd1e\"" }
[ t json-escape-unicode? [ "𝄞" >json ] with-variable ] unit-test

View File

@ -1,8 +1,9 @@
! Copyright (C) 2006 Chris Double.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors ascii assocs combinators fry hashtables io
io.streams.string json kernel locals math math.parser mirrors
namespaces sequences strings tr words ;
USING: accessors ascii assocs combinators formatting fry
hashtables io io.encodings.utf16.private io.streams.string json
kernel locals math math.parser mirrors namespaces sequences
strings tr words ;
IN: json.writer
SYMBOL: json-allow-fp-special?
@ -39,6 +40,26 @@ M: t stream-json-print
M: json-null stream-json-print
[ drop "null" ] [ stream-write ] bi* ;
<PRIVATE
: json-print-generic-escape-surrogate-pair ( stream char -- stream )
0x10000 - [ encode-first ] [ encode-second ] bi
"\\u%x%x\\u%x%x" sprintf over stream-write ;
: json-print-generic-escape-bmp ( stream char -- stream )
"\\u" pick stream-write
>hex 4 CHAR: 0 pad-head
over stream-write ;
: json-print-generic-escape ( stream char -- stream )
dup 0xffff > [
json-print-generic-escape-surrogate-pair
] [
json-print-generic-escape-bmp
] if ;
PRIVATE>
M: string stream-json-print
CHAR: " over stream-write1 swap [
{
@ -62,10 +83,7 @@ M: string stream-json-print
{ [ dup control? ] [ t ] }
[ json-escape-unicode? get ]
} cond [
dup 0xffff > [ json-error ] when
"\\u" pick stream-write
>hex 4 CHAR: 0 pad-head
over stream-write
json-print-generic-escape
] [
over stream-write1
] if