on eaec028 help sign in
75 lines 2.0 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE ScopedTypeVariables #-}
5-- |
6-- Module : Network.Google.Data.JSON
7-- Copyright : (c) 2015-2016 Brendan Hay <brendan.g.hay@gmail.com>
8-- License : Mozilla Public License, v. 2.0.
9-- Maintainer : Brendan Hay <brendan.g.hay@gmail.com>
10-- Stability : provisional
11-- Portability : non-portable (GHC extensions)
12--
13module Network.Google.Data.JSON
14 ( JSONValue
15 , Textual (..)
16 , parseJSONObject
17 , parseJSONText
18 , toJSONText
20 -- * Re-exports
21 , FromJSON (..)
22 , ToJSON (..)
24 , withObject
25 , emptyObject
26 , object
28 , (.=)
29 , (.:)
30 , (.:?)
31 , (.!=)
32 ) where
34import Data.Aeson
35import Data.Aeson.Types
36import Data.Data
37import Data.HashMap.Strict (HashMap)
38import Data.Text (Text)
39import qualified Data.Text as Text
40import Web.HttpApiData (FromHttpApiData (..), ToHttpApiData (..))
42type JSONValue = Value
44newtype Textual a = Textual a
45 deriving
46 ( Eq
47 , Ord
48 , Read
49 , Show
50 , Num
51 , Fractional
52 , Data
53 , Typeable
54 , ToHttpApiData
55 , FromHttpApiData
56 )
58instance (FromJSON a, FromHttpApiData a) => FromJSON (Textual a) where
59 parseJSON (String s) =
60 either (fail . Text.unpack) (pure . Textual) (parseQueryParam s)
61 parseJSON o = Textual <$> parseJSON o
63instance ToHttpApiData a => ToJSON (Textual a) where
64 toJSON (Textual x) = String (toQueryParam x)
66parseJSONObject :: FromJSON a => HashMap Text Value -> Parser a
67parseJSONObject = parseJSON . Object
69parseJSONText :: FromHttpApiData a => String -> Value -> Parser a
70parseJSONText n = withText n (either (fail . f) pure . parseQueryParam)
71 where
72 f x = n ++ " - " ++ Text.unpack x
74toJSONText :: ToHttpApiData a => a -> Value
75toJSONText = String . toQueryParam