Created
February 6, 2018 00:19
-
-
Save ptitfred/4a7b3ab6cc9e260b85fbfd06c70aab89 to your computer and use it in GitHub Desktop.
Support MessagePack in your Servant APIs
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
-- msgpack-aeson for msgpack instance derived from aeson instances | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
module Servant.MessagePack | |
( MessagePack | |
) where | |
import Data.Aeson (FromJSON, ToJSON) | |
import Data.List.NonEmpty (NonEmpty ((:|)), (<|)) | |
import Data.MessagePack.Aeson as MP | |
import Network.HTTP.Media ((//)) | |
import Servant.API.ContentTypes | |
data MessagePack | |
instance Accept MessagePack where | |
contentTypes _ = "application" // "msgpack" | |
<| "application" // "x-msgpack" | |
<| "application" // "vnd.msgpack" | |
:| [] | |
instance ToJSON a => MimeRender MessagePack a where | |
mimeRender _ = MP.packAeson | |
instance FromJSON a => MimeUnrender MessagePack a where | |
mimeUnrender _ = presentError . MP.unpackAeson | |
where | |
presentError = maybe (Left "Error while parsing MessagePack") Right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment