on 0.9.x help sign in
382 lines 12 kB history raw
1{-# LANGUAGE CPP #-}
3-- |
4-- Module: Data.Aeson
5-- Copyright: (c) 2011, 2012 Bryan O'Sullivan
6-- (c) 2011 MailRank, Inc.
7-- License: Apache
8-- Maintainer: Bryan O'Sullivan <bos@serpentine.com>
9-- Stability: experimental
10-- Portability: portable
11--
12-- Types and functions for working efficiently with JSON data.
13--
14-- (A note on naming: in Greek mythology, Aeson was the father of Jason.)
16module Data.Aeson
17 (
18 -- * How to use this library
19 -- $use
21 -- ** Working with the AST
22 -- $ast
24 -- ** Decoding to a Haskell value
25 -- $haskell
27 -- ** Decoding a mixed-type object
28 -- $mixed
30 -- ** Automatically decoding data types
31 -- $typeable
33 -- ** Pitfalls
34 -- $pitfalls
36 -- * Encoding and decoding
37 -- $encoding_and_decoding
38 decode
39 , decode'
40 , eitherDecode
41 , eitherDecode'
42 , encode
43 -- ** Variants for strict bytestrings
44 , decodeStrict
45 , decodeStrict'
46 , eitherDecodeStrict
47 , eitherDecodeStrict'
48 -- * Core JSON types
49 , Value(..)
50 , Array
51 , Object
52 -- * Convenience types
53 , DotNetTime(..)
54 -- * Type conversion
55 , FromJSON(..)
56 , Result(..)
57 , fromJSON
58 , ToJSON(..)
59#ifdef GENERICS
60 -- ** Generic JSON classes
61 , GFromJSON(..)
62 , GToJSON(..)
63 , genericToJSON
64 , genericParseJSON
65#endif
66 -- * Inspecting @'Value's@
67 , withObject
68 , withText
69 , withArray
70 , withNumber
71 , withScientific
72 , withBool
73 -- * Constructors and accessors
74 , (.=)
75 , (.:)
76 , (.:?)
77 , (.!=)
78 , object
79 -- * Parsing
80 , json
81 , json'
82 ) where
84import Data.Aeson.Encode (encode)
85import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith,
86 eitherDecodeWith, eitherDecodeStrictWith,
87 jsonEOF, json, jsonEOF', json')
88import Data.Aeson.Types
89import qualified Data.ByteString as B
90import qualified Data.ByteString.Lazy as L
92-- | Efficiently deserialize a JSON value from a lazy 'L.ByteString'.
93-- If this fails due to incomplete or invalid input, 'Nothing' is
94-- returned.
95--
96-- The input must consist solely of a JSON document, with no trailing
97-- data except for whitespace.
98--
99-- This function parses immediately, but defers conversion. See
100-- 'json' for details.
101decode :: (FromJSON a) => L.ByteString -> Maybe a
102decode = decodeWith jsonEOF fromJSON
103{-# INLINE decode #-}
105-- | Efficiently deserialize a JSON value from a strict 'B.ByteString'.
106-- If this fails due to incomplete or invalid input, 'Nothing' is
107-- returned.
108--
109-- The input must consist solely of a JSON document, with no trailing
110-- data except for whitespace.
111--
112-- This function parses immediately, but defers conversion. See
113-- 'json' for details.
114decodeStrict :: (FromJSON a) => B.ByteString -> Maybe a
115decodeStrict = decodeStrictWith jsonEOF fromJSON
116{-# INLINE decodeStrict #-}
118-- | Efficiently deserialize a JSON value from a lazy 'L.ByteString'.
119-- If this fails due to incomplete or invalid input, 'Nothing' is
120-- returned.
121--
122-- The input must consist solely of a JSON document, with no trailing
123-- data except for whitespace. This restriction is necessary to ensure
124-- that if data is being lazily read from a file handle, the file
125-- handle will be closed in a timely fashion once the document has
126-- been parsed.
127--
128-- This function parses and performs conversion immediately. See
129-- 'json'' for details.
130decode' :: (FromJSON a) => L.ByteString -> Maybe a
131decode' = decodeWith jsonEOF' fromJSON
132{-# INLINE decode' #-}
134-- | Efficiently deserialize a JSON value from a lazy 'L.ByteString'.
135-- If this fails due to incomplete or invalid input, 'Nothing' is
136-- returned.
137--
138-- The input must consist solely of a JSON document, with no trailing
139-- data except for whitespace.
140--
141-- This function parses and performs conversion immediately. See
142-- 'json'' for details.
143decodeStrict' :: (FromJSON a) => B.ByteString -> Maybe a
144decodeStrict' = decodeStrictWith jsonEOF' fromJSON
145{-# INLINE decodeStrict' #-}
147-- | Like 'decode' but returns an error message when decoding fails.
148eitherDecode :: (FromJSON a) => L.ByteString -> Either String a
149eitherDecode = eitherDecodeWith jsonEOF fromJSON
150{-# INLINE eitherDecode #-}
152-- | Like 'decodeStrict' but returns an error message when decoding fails.
153eitherDecodeStrict :: (FromJSON a) => B.ByteString -> Either String a
154eitherDecodeStrict = eitherDecodeStrictWith jsonEOF fromJSON
155{-# INLINE eitherDecodeStrict #-}
157-- | Like 'decode'' but returns an error message when decoding fails.
158eitherDecode' :: (FromJSON a) => L.ByteString -> Either String a
159eitherDecode' = eitherDecodeWith jsonEOF' fromJSON
160{-# INLINE eitherDecode' #-}
162-- | Like 'decodeStrict'' but returns an error message when decoding fails.
163eitherDecodeStrict' :: (FromJSON a) => B.ByteString -> Either String a
164eitherDecodeStrict' = eitherDecodeStrictWith jsonEOF' fromJSON
165{-# INLINE eitherDecodeStrict' #-}
167-- $use
168--
169-- This section contains basic information on the different ways to
170-- decode data using this library. These range from simple but
171-- inflexible, to complex but flexible.
172--
173-- The most common way to use the library is to define a data type,
174-- corresponding to some JSON data you want to work with, and then
175-- write either a 'FromJSON' instance, a to 'ToJSON' instance, or both
176-- for that type. For example, given this JSON data:
177--
178-- > { "name": "Joe", "age": 12 }
179--
180-- we create a matching data type:
181--
182-- > data Person = Person
183-- > { name :: Text
184-- > , age :: Int
185-- > } deriving Show
186--
187-- To decode data, we need to define a 'FromJSON' instance:
188--
189-- > {-# LANGUAGE OverloadedStrings #-}
190-- >
191-- > instance FromJSON Person where
192-- > parseJSON (Object v) = Person <$>
193-- > v .: "name" <*>
194-- > v .: "age"
195-- > -- A non-Object value is of the wrong type, so fail.
196-- > parseJSON _ = mzero
197--
198-- We can now parse the JSON data like so:
199--
200-- > >>> decode "{\"name\":\"Joe\",\"age\":12}" :: Maybe Person
201-- > Just (Person {name = "Joe", age = 12})
202--
203-- To encode data, we need to define a 'ToJSON' instance:
204--
205-- > instance ToJSON Person where
206-- > toJSON (Person name age) = object ["name" .= name, "age" .= age]
207--
208-- We can now encode a value like so:
209--
210-- > >>> encode (Person {name = "Joe", age = 12})
211-- > "{\"name\":\"Joe\",\"age\":12}"
212--
213-- There are predefined 'FromJSON' and 'ToJSON' instances for many
214-- types. Here's an example using lists and 'Int's:
215--
216-- > >>> decode "[1,2,3]" :: Maybe [Int]
217-- > Just [1,2,3]
218--
219-- And here's an example using the 'Data.Map.Map' type to get a map of
220-- 'Int's.
221--
222-- > >>> decode "{\"foo\":1,\"bar\":2}" :: Maybe (Map String Int)
223-- > Just (fromList [("bar",2),("foo",1)])
225-- While the notes below focus on decoding, you can apply almost the
226-- same techniques to /encoding/ data. (The main difference is that
227-- encoding always succeeds, but decoding has to handle the
228-- possibility of failure, where an input doesn't match our
229-- expectations.)
230--
231-- See the documentation of 'FromJSON' and 'ToJSON' for some examples
232-- of how you can automatically derive instances in some
233-- circumstances.
235-- $ast
236--
237-- Sometimes you want to work with JSON data directly, without first
238-- converting it to a custom data type. This can be useful if you want
239-- to e.g. convert JSON data to YAML data, without knowing what the
240-- contents of the original JSON data was. The 'Value' type, which is
241-- an instance of 'FromJSON', is used to represent an arbitrary JSON
242-- AST (abstract syntax tree). Example usage:
243--
244-- > >>> decode "{\"foo\": 123}" :: Maybe Value
245-- > Just (Object (fromList [("foo",Number 123)]))
246--
247-- > >>> decode "{\"foo\": [\"abc\",\"def\"]}" :: Maybe Value
248-- > Just (Object (fromList [("foo",Array (fromList [String "abc",String "def"]))]))
249--
250-- Once you have a 'Value' you can write functions to traverse it and
251-- make arbitrary transformations.
253-- $haskell
254--
255-- Any instance of 'FromJSON' can be specified (but see the
256-- \"Pitfalls\" section here&#8212;"Data.Aeson#pitfalls"):
257--
258-- > λ> decode "[1,2,3]" :: Maybe [Int]
259-- > Just [1,2,3]
260--
261-- Alternatively, there are instances for standard data types, so you
262-- can use them directly. For example, use the 'Data.Map.Map' type to
263-- get a map of 'Int's.
264--
265-- > λ> :m + Data.Map
266-- > λ> decode "{\"foo\":1,\"bar\":2}" :: Maybe (Map String Int)
267-- > Just (fromList [("bar",2),("foo",1)])
269-- $mixed
270--
271-- The above approach with maps of course will not work for mixed-type
272-- objects that don't follow a strict schema, but there are a couple
273-- of approaches available for these.
274--
275-- The 'Object' type contains JSON objects:
276--
277-- > λ> decode "{\"name\":\"Dave\",\"age\":2}" :: Maybe Object
278-- > Just (fromList) [("name",String "Dave"),("age",Number 2)]
279--
280-- You can extract values from it with a parser using 'parse',
281-- 'parseEither' or, in this example, 'parseMaybe':
282--
283-- > λ> do result <- decode "{\"name\":\"Dave\",\"age\":2}"
284-- > flip parseMaybe result $ \obj -> do
285-- > age <- obj .: "age"
286-- > name <- obj .: "name"
287-- > return (name ++ ": " ++ show (age*2))
288-- >
289-- > Just "Dave: 4"
290--
291-- Considering that any type that implements 'FromJSON' can be used
292-- here, this is quite a powerful way to parse JSON. See the
293-- documentation in 'FromJSON' for how to implement this class for
294-- your own data types.
295--
296-- The downside is that you have to write the parser yourself; the
297-- upside is that you have complete control over the way the JSON is
298-- parsed.
300-- $typeable
301--
302-- If you don't want fine control and would prefer the JSON be parsed
303-- to your own data types automatically according to some reasonably
304-- sensible isomorphic implementation, you can use the generic parser
305-- based on 'Data.Typeable.Typeable' and 'Data.Data.Data'. Switch to
306-- the 'Data.Aeson.Generic' module, and you can do the following:
307--
308-- > λ> decode "[1]" :: Maybe [Int]
309-- > Just [1]
310-- > λ> :m + Data.Typeable Data.Data
311-- > λ> :set -XDeriveDataTypeable
312-- > λ> data Person = Person { personName :: String, personAge :: Int } deriving (Data,Typeable,Show)
313-- > λ> encode Person { personName = "Chris", personAge = 123 }
314-- > "{\"personAge\":123,\"personName\":\"Chris\"}"
315-- > λ> decode "{\"personAge\":123,\"personName\":\"Chris\"}" :: Maybe Person
316-- > Just (Person {
317-- > personName = "Chris", personAge = 123
318-- > })
319--
320-- Be aware that the encoding may not always be what you'd naively
321-- expect:
322--
323-- > λ> data Foo = Foo Int Int deriving (Data,Typeable,Show)
324-- > λ> encode (Foo 1 2)
325-- > "[1,2]"
326--
327-- With this approach, it's best to treat the
328-- 'Data.Aeson.Generic.decode' and 'Data.Aeson.Generic.encode'
329-- functions as an isomorphism, and not to rely upon (or care about)
330-- the specific intermediate representation.
332-- $pitfalls
333-- #pitfalls#
334--
335-- Note that the JSON standard requires that the top-level value be
336-- either an array or an object. If you try to use 'decode' with a
337-- result type that is /not/ represented in JSON as an array or
338-- object, your code will typecheck, but it will always \"fail\" at
339-- runtime:
340--
341-- > >>> decode "1" :: Maybe Int
342-- > Nothing
343-- > >>> decode "1" :: Maybe String
344-- > Nothing
345--
346-- So stick to objects (e.g. maps in Haskell) or arrays (lists or
347-- vectors in Haskell):
348--
349-- > >>> decode "[1,2,3]" :: Maybe [Int]
350-- > Just [1,2,3]
351--
352-- When encoding to JSON you can encode anything that's an instance of
353-- 'ToJSON', and this may include simple types. So beware that this
354-- aspect of the API is not isomorphic. You can round-trip arrays and
355-- maps, but not simple values:
356--
357-- > >>> encode [1,2,3]
358-- > "[1,2,3]"
359-- > >>> decode (encode [1]) :: Maybe [Int]
360-- > Just [1]
361-- > >>> encode 1
362-- > "1"
363-- > >>> decode (encode (1 :: Int)) :: Maybe Int
364-- > Nothing
365--
366-- Alternatively, see 'Data.Aeson.Parser.value' to parse non-top-level
367-- JSON values.
369-- $encoding_and_decoding
370--
371-- Encoding and decoding are each two-step processes.
372--
373-- * To encode a value, it is first converted to an abstract syntax
374-- tree (AST), using 'ToJSON'. This generic representation is then
375-- encoded as bytes.
376--
377-- * When decoding a value, the process is reversed: the bytes are
378-- converted to an AST, then the 'FromJSON' class is used to convert
379-- to the desired type.
380--
381-- For convenience, the 'encode' and 'decode' functions combine both
382-- steps.