Created
March 19, 2018 11:29
-
-
Save adetokunbo/80f8de342ac263f8c133a3552a469efa to your computer and use it in GitHub Desktop.
Haskell snippet: ToJSON a => a -> Text,FromJSON a => Text -> Maybe a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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