Can parse JSON using json package in GHCI, but not when compiling with GHC

I am trying to parse a JSON string using the json library. This code works fine in GHCI:

import Text.JSON as JS JS.decode "{}" :: Result JSValue 

But when I use the same code in a compiled program:

 case JS.decode "{}" of JS.Ok value -> putStrLn value JS.Error err -> error err 

Unable to parse string with:

 Unable to read String 

I suspect that only I am doing something stupid, but I can’t understand what ....

Any ideas are very welcome!

Update:

I wrote this code to see if it was something in another part of the application that caused the problem:

 import qualified Text.JSON as JS main :: IO () main = do case JS.decode "{}" of JS.Ok value -> putStrLn value JS.Error err -> error err 

However, I get the same error:

 test: Unable to read String 

It was compiled with GHC 7.0.3, and the source was edited using vim. However, even the string data transferred from outside the application generates the same error. Really out of ideas now ...

+4
source share
2 answers

Use print instead of putStrLn , which only works with strings:

 import qualified Text.JSON as JS main :: IO () main = do case JS.decode "{}" of JS.Ok value -> print (value :: JSValue) JS.Error err -> error err 
+3
source

I think this is just a capitalization issue with JS.Decode instead of JS.Decode . The following works fine for me with the latest Haskell platform.

 module Foo where import qualified Text.JSON as JS foo :: IO () foo = case JS.decode "{}" of JS.Ok value -> putStrLn value JS.Error err -> error err 
+2
source

Source: https://habr.com/ru/post/1387341/


All Articles