Data.Aeson ( https://hackage.haskell.org/package/aeson-0.6.1.0/docs/Data-Aeson.html ) を使用して JSON 文字列をデコードしようとしていますが、文字列の解析に失敗しています。非標準文字が含まれています。
例として、ファイル:
import Data.Aeson
import Data.ByteString.Lazy.Char8 (pack)
test1 :: Maybe Value
test1 = decode $ pack "{ \"foo\": \"bar\"}"
test2 :: Maybe Value
test2 = decode $ pack "{ \"foo\": \"bòz\"}"
ghci で実行すると、次の結果が得られます。
*Main> :l ~/test.hs
[1 of 1] Compiling Main ( /Users/ltomlin/test.hs, interpreted )
Ok, modules loaded: Main.
*Main> test1
Just (Object fromList [("foo",String "bar")])
*Main> test2
Nothing
文字列をユニコード文字で解析しない理由はありますか? 私は、Haskell が Unicode にかなり優れているという印象を受けました。どんな提案でも大歓迎です!
ありがとう、
テチギ
編集
を使用してさらに調査するeitherDecode
と、次のエラー メッセージが表示されます。
*Main> test2
Left "Failed reading: Cannot decode byte '\\x61': Data.Text.Encoding.decodeUtf8: Invalid UTF-8 stream"
x61
特殊な Unicode 文字の直後にある「z」の Unicode 文字です。特殊文字の後の文字を読み取れない理由がわかりません!
代わりにに変更test2
するとtest2 = decode $ pack "{ \"foo\": \"bòz\"}"
、エラーが発生します。
Left "Failed reading: Cannot decode byte '\\xf2': Data.Text.Encoding.decodeUtf8: Invalid UTF-8 stream"
これは「ò」の文字で、もう少し意味があります。