data User = User { city :: Text
, country :: Text
, phone :: Text
, email :: Text}
instance ToJSON User where
toJSON (User a b c d)= object ["a" .= a
,"b" .= b
,"c" .= c
,"d" .= d]
test:: User -> IO Value
test u = do
let j = toJSON u
return j
What I want is Text like so
test::User -> IO Text
test u = do
let j = pack ("{\"city\":\"test\",\"country\":\"test\",\"phone\":\"test\",\"email\":\"test\"}")
return j
I can’t figure it out how to go from Value to Text
It is more difficult to do this than it should be for what (I think) is a generally useful function.
Data.Aeson.Encode.encodedoes too much work and converts it all the way to aByteString.Starting with
encodeand switching theLazy.Text -> ByteStringtoLazy.Text -> Strict.Textconversion does what you want: