我有一个代码,如果从emacs内的slime提示执行运行没有错误。如果我从提示符开始sbcl,我收到错误:
* (ei:proc-file "BRAvESP000.log" "lixo")
debugger invoked on a SB-INT:STREAM-ENCODING-ERROR:
:UTF-8 stream encoding error on
#<SB-SYS:FD-STREAM for "file /Users/arademaker/work/IBM/scolapp/lixo"
{10049E8FF3}>:
the character with code 55357 cannot be encoded.
Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [OUTPUT-NOTHING ] Skip output of this character.
1: [OUTPUT-REPLACEMENT] Output replacement string.
2: [ABORT ] Exit debugger, returning to top level.
(SB-IMPL::STREAM-ENCODING-ERROR-AND-HANDLE #<SB-SYS:FD-STREAM for "file /Users/arademaker/work/IBM/scolapp/lixo" {10049E8FF3}> 55357)
0]
在两种情况下,我都使用相同的sbcl 1.1.8和相同的机器Mac OS 10.8.4。任何的想法?
代码:
(defun proc-file (filein fileout &key (fn-convert #'identity))
(with-open-file (fout fileout
:direction :output
:if-exists :supersede
:external-format :utf8)
(with-open-file (fin filein :external-format :utf8)
(loop for line = (read-line fin nil)
while line
do
(handler-case
(let* ((line (ppcre:regex-replace "^.*{jsonTweet=" line "{\"jsonTweet\":"))
(data (gethash "jsonTweet" (yason:parse line))))
(yason:encode (funcall fn-convert (yason:parse data)) fout)
(format fout "~%"))
(end-of-file ()
(format *standard-output* "Error[~a]: ~a~%" filein line)))))))
答案 0 :(得分:1)
这几乎可以肯定是一个错误。 JSON要求如果转义非BMP字符,则通过代理对完成。这是一个简单的例子,U + 10000(可选择在json中转义为“\ ud800 \ udc00”;我使用babel作为babel的转换不那么严格):
(map 'list #'char-code (yason:parse "\"\\ud800\\udc00\""))
=> (55296 56320)
unicode代码点55296(十进制)是代理项对的开始,除非作为UTF-16中的代理项对,否则不应出现。幸运的是,通过使用babel将字符串编码为UTF-16并再次返回,可以很容易地解决这个问题:
(babel:octets-to-string (babel:string-to-octets (yason:parse "\"\\ud800\\udc00\"") :encoding :utf-16le) :encoding :utf-16le)
=> ""
您应该可以通过更改此行来解决此问题:
(yason:encode (funcall fn-convert (yason:parse data)) fout)
使用转换为UTF-16并返回的中间字符串。
(write-sequence
(babel:octets-to-string
(babel:string-to-octets
(with-output-to-string (outs)
(yason:encode (funcall fn-convert (yason:parse data)) outs))
:encoding :utf-16le)
:encoding :utf-16le)
fout)
我提交了一个已被接受的补丁,以便在yason中解决这个问题:
https://github.com/hanshuebner/yason/commit/4a9bdaae652b7ceea79984e0349a992a5458a0dc