forked from haskell/aeson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Types.hs
291 lines (238 loc) · 7.96 KB
/
Types.hs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
{-# LANGUAGE DeriveDataTypeable #-}
-- Module: Data.Aeson.Types
-- Copyright: (c) 2011 MailRank, Inc.
-- License: Apache
-- Maintainer: Bryan O'Sullivan <bos@mailrank.com>
-- Stability: experimental
-- Portability: portable
--
-- Types for working with JSON data.
module Data.Aeson.Types
(
-- * Core JSON types
Value(..)
, Array
, Object
-- * Type conversion
, FromJSON(..)
, ToJSON(..)
-- * Constructors and accessors
, (.=)
, (.:)
, (.:?)
, object
) where
import Control.Applicative
import Control.DeepSeq (NFData(..))
import Data.Map (Map)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LB
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import Data.Text (Text, pack, unpack)
import qualified Data.Text.Lazy as LT
import Data.Time.Clock (UTCTime)
import Data.Time.Format (formatTime, parseTime)
import Data.Typeable (Typeable)
import Data.Vector (Vector)
import System.Locale (defaultTimeLocale)
import qualified Data.Map as M
import qualified Data.Vector as V
type Object = Map Text Value
type Array = Vector Value
-- | A JSON value represented as a Haskell value.
data Value = Object Object
| Array Array
| String Text
| Number Double
| Bool !Bool
| Null
deriving (Eq, Show, Typeable)
instance NFData Value where
rnf (Object o) = rnf o
rnf (Array a) = V.foldl' (\x y -> rnf y `seq` x) () a
rnf (String s) = rnf s
rnf (Number n) = rnf n
rnf (Bool b) = rnf b
rnf Null = ()
-- | Construct an 'Object' from a key and a value.
(.=) :: ToJSON a => Text -> a -> Object
name .= value = M.singleton name (toJSON value)
{-# INLINE (.=) #-}
-- | Retrieve the value associated with the given key of an 'Object'.
-- The result is 'empty' if the key is not present or the value cannot
-- be converted to the desired type.
--
-- This accessor is appropriate if the key and value /must/ be present
-- in an object for it to be valid. If the key and value are
-- optional, use '(.:?)' instead.
(.:) :: (Alternative f, FromJSON a) => Object -> Text -> f a
obj .: key = case M.lookup key obj of
Nothing -> empty
Just v -> fromJSON v
{-# INLINE (.:) #-}
-- | Retrieve the value associated with the given key of an 'Object'.
-- The result is 'Nothing' if the key is not present, or 'empty' if
-- the value cannot be converted to the desired type.
--
-- This accessor is most useful if the key and value can be absent
-- from an object without affecting its validity. If the key and
-- value are mandatory, use '(.:?)' instead.
(.:?) :: (Alternative f, FromJSON a) => Object -> Text -> f (Maybe a)
obj .:? key = case M.lookup key obj of
Nothing -> pure Nothing
Just v -> fromJSON v
{-# INLINE (.:?) #-}
-- | Create a 'Value' from a list of 'Object's. If duplicate
-- keys arise, earlier keys and their associated values win.
object :: [Object] -> Value
object = Object . M.unions
{-# INLINE object #-}
-- | A type that can be converted to JSON.
--
-- An example type and instance:
--
-- @data Coord { x :: Double, y :: Double }
--
-- instance ToJSON Coord where
-- toJSON (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
-- @
class ToJSON a where
toJSON :: a -> Value
-- | A type that can be converted from JSON, with the possibility of
-- failure.
--
-- When writing an instance, use 'empty' to make a conversion fail,
-- e.g. if an 'Object' is missing a required key, or the value is of
-- the wrong type.
--
-- An example type and instance:
--
-- @data Coord { x :: Double, y :: Double }
--
-- instance FromJSON Coord where
-- fromJSON ('Object' v) = Coord '<$>'
-- v '.:' \"x\" '<*>'
-- v '.:' \"y\"
--
-- \-- A non-'Object' value is of the wrong type, so use 'empty' to fail.
-- fromJSON _ = 'empty'
-- @
class FromJSON a where
fromJSON :: Alternative f => Value -> f a
instance (ToJSON a) => ToJSON (Maybe a) where
toJSON (Just a) = toJSON a
toJSON Nothing = Null
{-# INLINE toJSON #-}
instance (FromJSON a) => FromJSON (Maybe a) where
fromJSON Null = pure Nothing
fromJSON a = Just <$> fromJSON a
{-# INLINE fromJSON #-}
instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
toJSON (Left a) = toJSON a
toJSON (Right b) = toJSON b
{-# INLINE toJSON #-}
instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
fromJSON a = Left <$> fromJSON a <|> Right <$> fromJSON a
{-# INLINE fromJSON #-}
instance ToJSON Bool where
toJSON = Bool
{-# INLINE toJSON #-}
instance FromJSON Bool where
fromJSON (Bool b) = pure b
fromJSON _ = empty
{-# INLINE fromJSON #-}
instance ToJSON Double where
toJSON = Number
{-# INLINE toJSON #-}
instance FromJSON Double where
fromJSON (Number n) = pure n
fromJSON _ = empty
{-# INLINE fromJSON #-}
instance ToJSON Int where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
instance FromJSON Int where
fromJSON (Number n) = pure (floor n)
fromJSON _ = empty
{-# INLINE fromJSON #-}
instance ToJSON Integer where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
instance FromJSON Integer where
fromJSON (Number n) = pure (floor n)
fromJSON _ = empty
{-# INLINE fromJSON #-}
instance ToJSON Text where
toJSON = String
{-# INLINE toJSON #-}
instance FromJSON Text where
fromJSON (String t) = pure t
fromJSON _ = empty
{-# INLINE fromJSON #-}
instance ToJSON LT.Text where
toJSON = String . LT.toStrict
{-# INLINE toJSON #-}
instance FromJSON LT.Text where
fromJSON (String t) = pure (LT.fromStrict t)
fromJSON _ = empty
{-# INLINE fromJSON #-}
instance ToJSON B.ByteString where
toJSON = String . decodeUtf8
{-# INLINE toJSON #-}
instance FromJSON B.ByteString where
fromJSON (String t) = pure . encodeUtf8 $ t
fromJSON _ = empty
{-# INLINE fromJSON #-}
instance ToJSON LB.ByteString where
toJSON = toJSON . B.concat . LB.toChunks
{-# INLINE toJSON #-}
instance FromJSON LB.ByteString where
fromJSON (String t) = pure . LB.fromChunks . (:[]) . encodeUtf8 $ t
fromJSON _ = empty
{-# INLINE fromJSON #-}
mapA :: (Applicative f) => (t -> f a) -> [t] -> f [a]
mapA f = go
where
go (a:as) = (:) <$> f a <*> go as
go [] = pure []
instance (ToJSON a) => ToJSON [a] where
toJSON = Array . V.fromList . map toJSON
{-# INLINE toJSON #-}
instance (FromJSON a) => FromJSON [a] where
fromJSON (Array a) = mapA fromJSON (V.toList a)
fromJSON _ = empty
{-# INLINE fromJSON #-}
instance (ToJSON a) => ToJSON (Vector a) where
toJSON = Array . V.map toJSON
{-# INLINE toJSON #-}
instance (FromJSON a) => FromJSON (Vector a) where
fromJSON (Array a) = V.fromList <$> mapA fromJSON (V.toList a)
fromJSON _ = empty
{-# INLINE fromJSON #-}
instance ToJSON Value where
toJSON a = a
{-# INLINE toJSON #-}
instance FromJSON Value where
fromJSON a = pure a
{-# INLINE fromJSON #-}
-- We happen to use the same JSON formatting for a UTCTime as .NET
-- does for a DateTime. How handy!
instance ToJSON UTCTime where
toJSON t = String (pack (formatTime defaultTimeLocale "/Date(%s)/" t))
{-# INLINE toJSON #-}
instance FromJSON UTCTime where
fromJSON (String t) =
case parseTime defaultTimeLocale "/Date(%s)/" (unpack t) of
Just d -> pure d
_ -> empty
fromJSON _ = empty
{-# INLINE fromJSON #-}
instance (ToJSON a, ToJSON b) => ToJSON (a,b) where
toJSON (a,b) = toJSON [toJSON a, toJSON b]
{-# INLINE toJSON #-}
instance (FromJSON a, FromJSON b) => FromJSON (a,b) where
fromJSON (Array ab) = case V.toList ab of
[a,b] -> (,) <$> fromJSON a <*> fromJSON b
_ -> empty
fromJSON _ = empty
{-# INLINE fromJSON #-}