diff --git a/basis/json/writer/writer-tests.factor b/basis/json/writer/writer-tests.factor index c43840d7f8..bceaa7b21a 100644 --- a/basis/json/writer/writer-tests.factor +++ b/basis/json/writer/writer-tests.factor @@ -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 diff --git a/basis/json/writer/writer.factor b/basis/json/writer/writer.factor index 7a6771e02b..d91f3d7817 100644 --- a/basis/json/writer/writer.factor +++ b/basis/json/writer/writer.factor @@ -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* ; +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