From 818c343e89feb418d8699f893624f1b0745e7907 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Thu, 17 Oct 2019 18:53:11 +0200 Subject: [PATCH] Use Base64 to correctly handle binary ByteStrings --- hanafuda-APILanguage.cabal | 1 + src/Hanafuda/Message.hs | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/hanafuda-APILanguage.cabal b/hanafuda-APILanguage.cabal index 9cded08..3530d35 100644 --- a/hanafuda-APILanguage.cabal +++ b/hanafuda-APILanguage.cabal @@ -23,6 +23,7 @@ library -- other-extensions: build-depends: aeson , base >=4.9 && <4.13 + , base64-bytestring , bytestring , containers , hanafuda >= 0.3.3 diff --git a/src/Hanafuda/Message.hs b/src/Hanafuda/Message.hs index c95114a..08f561c 100644 --- a/src/Hanafuda/Message.hs +++ b/src/Hanafuda/Message.hs @@ -22,6 +22,7 @@ import Data.Aeson ( ) import Data.Aeson.Types (toJSONKeyText) import Data.ByteString (ByteString) +import qualified Data.ByteString.Base64 as B64 (decode, encode) import Data.Map (Map) import Data.Monoid ((<>)) import Data.Text (Text) @@ -182,10 +183,10 @@ instance ToJSON PublicState where toEncoding = genericToEncoding defaultOptions instance FromJSON ByteString where - parseJSON = withText "ByteString" (return . encodeUtf8) + parseJSON = withText "ByteString" (either fail return . B64.decode . encodeUtf8) instance ToJSON ByteString where - toJSON = String . decodeUtf8 - toEncoding = toEncoding . decodeUtf8 + toJSON = String . decodeUtf8 . B64.encode + toEncoding = toEncoding . decodeUtf8 . B64.encode data PublicGame = PublicGame { nonce :: ByteString