I would like to input a string from the console and output a JSON string.
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Aeson
import Data.Map.Strict
main :: IO ()
main = interact $ encode
This code fails. Of course, I see encode has type ToJSON a => a -> Data.ByteString.Lazy.Internal.ByteString and interact' takes String -> String, but that's the reason I'm using OverloadedStrings.
How can I solve this problem ? Perform a cast to String ?
OverloadedStrings only works for string literals. It thus converts a"foo"expression tofromString "foo".But you can use this to convert this to a
ByteString. You can first usedecodeUtf8 :: ByteString -> Textto convert theByteStringto aText, then use theStringdata constructor of theValuetype, and then encode the data, so:module Main where
This will thus convert the input to a JSON string literal.