on master help sign in
Expunge mentions of Data.Aeson.Generic
commit0b31b5b55f7870ca366abe2b7329a3a0b235565f authorBryan O'Sullivan <bos@serpentine.com> dateTue 26 May 2015, 16:58 (11 years ago) parent6276c0d
3 files changed+17−90
Data/Aeson.hs−35 Data/Aeson/Types/Class.hs+17−25 examples/GenericSYB.hs−30
Data/Aeson.hs−35
@@ -25,9 +25,6 @@ module Data.Aeson
2525 -- ** Decoding a mixed-type object
2626 -- $mixed
2727
28− -- ** Automatically decoding data types
29− -- $typeable
30−
3128 -- ** Pitfalls
3229 -- $pitfalls
3330
@@ -302,38 +299,6 @@ eitherDecodeStrict' = eitherFormatError . eitherDecodeStrictWith jsonEOF' fromJS
302299-- upside is that you have complete control over the way the JSON is
303300-- parsed.
304301
305−-- $typeable
306−--
307−-- If you don't want fine control and would prefer the JSON be parsed
308−-- to your own data types automatically according to some reasonably
309−-- sensible isomorphic implementation, you can use the generic parser
310−-- based on 'Data.Typeable.Typeable' and 'Data.Data.Data'. Switch to
311−-- the 'Data.Aeson.Generic' module, and you can do the following:
312−--
313−-- > λ> decode "[1]" :: Maybe [Int]
314−-- > Just [1]
315−-- > λ> :m + Data.Typeable Data.Data
316−-- > λ> :set -XDeriveDataTypeable
317−-- > λ> data Person = Person { personName :: String, personAge :: Int } deriving (Data,Typeable,Show)
318−-- > λ> encode Person { personName = "Chris", personAge = 123 }
319−-- > "{\"personAge\":123,\"personName\":\"Chris\"}"
320−-- > λ> decode "{\"personAge\":123,\"personName\":\"Chris\"}" :: Maybe Person
321−-- > Just (Person {
322−-- > personName = "Chris", personAge = 123
323−-- > })
324−--
325−-- Be aware that the encoding may not always be what you'd naively
326−-- expect:
327−--
328−-- > λ> data Foo = Foo Int Int deriving (Data,Typeable,Show)
329−-- > λ> encode (Foo 1 2)
330−-- > "[1,2]"
331−--
332−-- With this approach, it's best to treat the
333−-- 'Data.Aeson.Generic.decode' and 'Data.Aeson.Generic.encode'
334−-- functions as an isomorphism, and not to rely upon (or care about)
335−-- the specific intermediate representation.
336−
337302-- $pitfalls
338303-- #pitfalls#
339304--
Data/Aeson/Types/Class.hs+17−25
@@ -82,22 +82,18 @@ genericParseJSON opts = fmap to . gParseJSON opts
8282-- Instead of manually writing your 'ToJSON' instance, there are three options
8383-- to do it automatically:
8484--
85−-- * "Data.Aeson.TH" provides template-haskell functions which will derive an
86−-- instance at compile-time. The generated instance is optimized for your type
85+-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
86+-- instance at compile time. The generated instance is optimized for your type
8787-- so will probably be more efficient than the following two options:
8888--
89−-- * "Data.Aeson.Generic" provides a generic @toJSON@ function that accepts any
90−-- type which is an instance of 'Data'.
89+-- * The compiler can provide default generic implementations for 'toJSON' and
90+-- 'toEncoding'.
9191--
92−-- * If your compiler has support for the @DeriveGeneric@ and
93−-- @DefaultSignatures@ language extensions (GHC 7.2 and newer),
94−-- @toJSON@ will have a default generic implementation.
92+-- To use the second, simply add a @deriving 'Generic'@ clause to your
93+-- datatype and declare a 'ToJSON' instance for your datatype without giving
94+-- definitions for 'toJSON' or 'toEncoding'.
9595--
96−-- To use the latter option, simply add a @deriving 'Generic'@ clause to your
97−-- datatype and declare a @ToJSON@ instance for your datatype without giving a
98−-- definition for @toJSON@.
99−--
100−-- For example the previous example can be simplified to just:
96+-- For example, the previous example can be simplified to just:
10197--
10298-- @{-\# LANGUAGE DeriveGeneric \#-}
10399--
@@ -154,25 +150,21 @@ class ToJSON a where
154150-- Note the use of the @OverloadedStrings@ language extension which enables
155151-- 'Text' values to be written as string literals.
156152--
157−-- Instead of manually writing your 'FromJSON' instance, there are three options
153+-- Instead of manually writing your 'FromJSON' instance, there are two options
158154-- to do it automatically:
159155--
160−-- * "Data.Aeson.TH" provides template-haskell functions which will derive an
161−-- instance at compile-time. The generated instance is optimized for your type
156+-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
157+-- instance at compile time. The generated instance is optimized for your type
162158-- so will probably be more efficient than the following two options:
163159--
164−-- * "Data.Aeson.Generic" provides a generic @fromJSON@ function that parses to
165−-- any type which is an instance of 'Data'.
166−--
167−-- * If your compiler has support for the @DeriveGeneric@ and
168−-- @DefaultSignatures@ language extensions, @parseJSON@ will have a default
169−-- generic implementation.
160+-- * The compiler can provide a default generic implementation for
161+-- 'parseJSON'.
170162--
171−-- To use this, simply add a @deriving 'Generic'@ clause to your datatype and
172−-- declare a @FromJSON@ instance for your datatype without giving a definition
173−-- for @parseJSON@.
163+-- To use the second, simply add a @deriving 'Generic'@ clause to your
164+-- datatype and declare a 'FromJSON' instance for your datatype without giving
165+-- a definition for 'parseJSON'.
174166--
175−-- For example the previous example can be simplified to just:
167+-- For example, the previous example can be simplified to just:
176168--
177169-- @
178170-- {-\# LANGUAGE DeriveGeneric \#-}
examples/GenericSYB.hsdeleted−30
@@ -1,30 +0,0 @@
1−-- This example is basically the same as in Simplest.hs, only it uses
2−-- SYB generics instead of explicit instances of ToJSON and FromJSON.
3−
4−-- This mechanism is much slower than the newer generics mechanism
5−-- demonstrated in Generic.hs, but it works on versions of GHC older
6−-- than 7.2.
7−
8−-- We enable the DeriveDataTypeable language extension so that GHC can
9−-- automatically derive the Typeable and Data classes for us.
10−
11−{-# LANGUAGE DeriveDataTypeable #-}
12−
13−{-# LANGUAGE OverloadedStrings #-}
14−
15−import Data.Data (Typeable, Data)
16−import Data.Aeson.Generic (decode, encode)
17−import qualified Data.ByteString.Lazy.Char8 as BL
18−
19−-- To decode or encode a value using the generic machinery, we must
20−-- make the type an instance of the Typeable and Data classes.
21−
22−data Coord = Coord { x :: Double, y :: Double }
23− deriving (Show, Typeable, Data)
24−
25−main :: IO ()
26−main = do
27− let req = decode "{\"x\":3.0,\"y\":-1.0}" :: Maybe Coord
28− print req
29− let reply = Coord 123.4 20
30− BL.putStrLn (encode reply)