Last active
February 28, 2017 17:19
-
-
Save schell/6d566486ee343e88ab5c3804dd95821f to your computer and use it in GitHub Desktop.
Unicode Shenanigans
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
{-# LANGUAGE OverloadedStrings #-} | |
module Lib | |
( someFunc | |
) where | |
import Data.Aeson | |
import Data.Aeson.Parser (value) | |
import Data.Aeson.Text | |
import Data.Aeson.Types | |
import qualified Data.ByteString.Lazy as BL | |
import Data.Proxy (Proxy (..)) | |
import Data.Text | |
import qualified Data.Text.IO as TIO | |
import qualified Data.Text.Lazy as TL | |
import Data.Text.Lazy.Encoding | |
import Servant.API.ContentTypes (JSON, mimeRender, mimeUnrender) | |
newtype MyText = MyText { unMyText :: Text } deriving (Show) | |
instance ToJSON MyText where | |
toJSON (MyText txt) = object ["myText" .= txt] | |
instance FromJSON MyText where | |
parseJSON (Object v) = MyText <$> v .: "myText" | |
parseJSON v = typeMismatch "MyText" v | |
someFunc :: IO () | |
someFunc = do | |
let x = MyText "\229 - å" -- these two characters are the same, == "å - å" | |
encoded = encode x :: BL.ByteString | |
decoded = decode encoded :: Maybe MyText | |
jsonType = Proxy :: Proxy JSON | |
mimed = mimeRender jsonType x | |
unmimed = mimeUnrender jsonType mimed :: Either String MyText | |
TIO.putStrLn $ unMyText x | |
print ("default encoding/decoding path", encoded, decoded) | |
print ("servant encoding/decoding path", mimed, unmimed) | |
{- | |
å - å | |
("default encoding/decoding path","{\"myText\":\"\195\165 - \195\165\"}",Just (MyText {unMyText = "\229 - \229"})) | |
("servant encoding/decoding path","{\"myText\":\"\195\165 - \195\165\"}",Right (MyText {unMyText = "\229 - \229"})) | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment