Skip to content

Instantly share code, notes, and snippets.

@adetokunbo
Created March 19, 2018 11:29
Show Gist options
  • Save adetokunbo/80f8de342ac263f8c133a3552a469efa to your computer and use it in GitHub Desktop.
Save adetokunbo/80f8de342ac263f8c133a3552a469efa to your computer and use it in GitHub Desktop.
Haskell snippet: ToJSON a => a -> Text,FromJSON a => Text -> Maybe a
import Data.Text (Text)
import Data.Text.Lazy (fromStrict, toStrict)
import Data.Text.Lazy.Builder (toLazyText)
import Data.Text.Lazy.Encoding (encodeUtf8)
import Data.Aeson
import Data.Aeson.Text (encodeToTextBuilder)
asText :: ToJSON a => a -> Text
asText = toStrict . toLazyText . encodeToTextBuilder . toJSON
fromText :: FromJSON a => Text -> Maybe a
fromText = decode . encodeUtf8 . fromStrict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment