Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

putStrLn unicode character error: invalid character #956

Open
clojurians-org opened this issue Apr 4, 2019 · 2 comments
Open

putStrLn unicode character error: invalid character #956

clojurians-org opened this issue Apr 4, 2019 · 2 comments

Comments

@clojurians-org
Copy link

clojurians-org commented Apr 4, 2019

it's very strange, the first print with show is success, but the second fail!

import qualified Data.Aeson as J (encode)
import Data.ByteString.Lazy.Char8 as BLC (unpack)
import Data.Map (Map, singleton)

(=:) = singleton
main :: IO ()
main = do
  let jsonValue = "id" =: "140502198811102244"
               <> "cell" =: "13986671110"
               <> "name" =: "罗浩"
               <> "meal" =: "Execution"
  let jsonData = BLC.unpack $ J.encode jsonValue
  putStrLn (show jsonData)
  putStrLn jsonData
[op@my-200 callJava]$ etlas run
Up to date
"{\"cell\":\"13986671110\",\"id\":\"140502198811102244\",\"meal\":\"Execution\",\"name\":\"\231\189\151\230\181\169\"}"
{"cell":"13986671110","id":"140502198811102244","meal":"Execution","name":"Exception in thread "main" eta.runtime.exception.EtaException: <stdout>: commitBuffer: invalid argument (invalid character)
        at base.ghc.TopHandler$runMainIO2.call(TopHandler.hs)
        at base.ghc.TopHandler$runMainIO2.apply1V(TopHandler.hs)
        at eta.runtime.exception.Exception.catch_(Exception.java:166)
        at main.Main$main22.call(Main.hs)
        at main.Main$DZCmain.call(Main.hs:62)
        at main.Main$DZCmain.applyV(Main.hs:62)
        at eta.runtime.stg.Closures$EvalLazyIO.enter(Closures.java:152)
        at eta.runtime.stg.Capability.schedule(Capability.java:246)
        at eta.runtime.stg.Capability.scheduleClosure(Capability.java:202)
        at eta.runtime.Runtime.evalLazyIO(Runtime.java:392)
        at eta.runtime.Runtime.main(Runtime.java:385)
        at eta.main.main(Unknown Source)
Caused by: eta.runtime.exception.EtaException: <stdout>: commitBuffer: invalid argument (invalid character)
        at base.ghc.io.handle.Internals$sat$2.apply1V(Internals.hs:173)
        at eta.runtime.exception.Exception.catch_(Exception.java:166)
        at base.ghc.io.handle.Internals$$wa2.call(Internals.hs:167)
        at base.ghc.io.handle.Internals$a3.applyV(Internals.hs:133)
        at eta.runtime.exception.Exception.maskAsyncExceptions(Exception.java:42)
        at base.ghc.io.handle.Internals$$wa4.call(Internals.hs:131)
        at base.ghc.io.handle.Internals$$wa3.call(Internals.hs:237)
        at base.ghc.io.handle.Internals$wantWritableHandle1.call(Internals.hs:227)
        at base.ghc.io.handle.Text$$wa8.call(Text.hs:630)
        at base.ghc.io.handle.Text$hPutStr2.call(Text.hs:553)
        at main.Main$main1.call(Main.hs:71)
        at main.Main$main1.applyV(Main.hs)
        at eta.runtime.exception.Exception.catch_(Exception.java:129)
        ... 9 more
Caused by: eta.runtime.exception.EtaException: recoverEncode: invalid argument (invalid character)
        at base.ghc.io.encoding.Failure$$wa2.call(Failure.hs:199)
        at base.ghc.io.encoding.Failure$$wa1.call(Failure.hs:166)
        at base.ghc.io.encoding.Failure$recoverEncode1.call(Failure.hs)
        at base.ghc.io.encoding.Latin1$sat$40.apply2V(Latin1.hs:107)
        at base.ghc.io.handle.Internals$a2.apply2V(Internals.hs:393)
        at base.ghc.io.handle.Internals$$wa.call(Internals.hs:376)
        at base.ghc.io.handle.Internals$$wa6.call(Internals.hs:518)
        at base.ghc.io.handle.Text$sat$147.apply1V(Text.hs:605)
        at base.ghc.io.handle.Internals$sat$56.apply1V(Internals.hs:256)
        at eta.runtime.apply.PAP1_1.applyV(PAP1_1.java:19)
        at eta.runtime.exception.Exception.catch_(Exception.java:129)
        ... 20 more
@jneira
Copy link
Collaborator

jneira commented Apr 4, 2019

Mmm, maybe the error is caused by Data.ByteString.Lazy.Char8.unpack, it only handles valid ascii chars, right?
Maybe you can use Data.ByteString.UTF8.toString from utf8-string package instead?

@clojurians-org
Copy link
Author

it's still the same.
it run success on my mac, but it fail on readhat7.
maybe the system encoding have impact on it.
but add show method can success.

import qualified Data.Aeson as J (encode)
import Data.ByteString.Lazy.Char8 as BLC (unpack)
import Data.Map (Map, singleton)

import Data.ByteString.Lazy.UTF8 as BLU (toString)
  let jsonValue = "id" =: "140502198811102244"
               <> "cell" =: "13986671110"
               <> "name" =: "罗浩"
               <> "meal" =: "Execution"
  let jsonData = BLU.toString $ J.encode jsonValue
  putStrLn (show jsonData)
  putStrLn jsonData

@clojurians-org clojurians-org reopened this Apr 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants