summaryrefslogtreecommitdiffstats
path: root/src/Data/ByteString/Extended.hs
blob: 144c9331663f62ccb5d0f50b35f9a68047ff95c2 (plain)
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
{-# LANGUAGE FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Data.ByteString.Extended
    (
    ) where

import Data.Aeson
import Data.ByteString
import qualified Data.CaseInsensitive
import Data.Text.Encoding
import qualified Data.Text.Encoding.Error


instance FromJSON ByteString where
    parseJSON =
        withText "ByteString" $
          pure . Data.Text.Encoding.encodeUtf8

instance ToJSON Data.ByteString.ByteString where
    toJSON =
        String .
        Data.Text.Encoding.decodeUtf8With
          Data.Text.Encoding.Error.lenientDecode


instance ToJSON (Data.CaseInsensitive.CI Data.ByteString.ByteString) where
    toJSON =
        toJSON . Data.CaseInsensitive.foldedCase

instance FromJSON (Data.CaseInsensitive.CI Data.ByteString.ByteString) where
    parseJSON =
        withText "CI ByteString" $
          pure . Data.CaseInsensitive.mk . Data.Text.Encoding.encodeUtf8