forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTByteString.hs
More file actions
39 lines (32 loc) · 1016 Bytes
/
TByteString.hs
File metadata and controls
39 lines (32 loc) · 1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module Data.TByteString
( TByteString
, fromText
, fromBS
, fromLBS
) where
import qualified Data.Aeson as J
import qualified Data.ByteString as B
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Data.Bool (bool)
import Prelude
newtype TByteString
= TByteString (Bool, T.Text)
deriving (Show, Eq)
instance J.ToJSON TByteString where
toJSON (TByteString (isBase64, t)) =
bool (J.toJSON t) (J.toJSON ["Base64", t]) isBase64
fromText :: T.Text -> TByteString
fromText t = TByteString (False, t)
fromBS :: B.ByteString -> TByteString
fromBS bs =
TByteString $
-- if the bs in not utf-8 encoded, encode it to Base64
case TE.decodeUtf8' bs of
Left _ -> (True, TE.decodeUtf8 $ B64.encode bs)
Right t -> (False, t)
fromLBS :: BL.ByteString -> TByteString
fromLBS =
fromBS . BL.toStrict