on master help sign in
Documentation, example
commit1c8d8a08ce8a615be51d3494eb90f77c2318d7a2 authorRyanGlScott <ryan.gl.scott@gmail.com> dateSun 26 Jul 2015, 19:44 (11 years ago) parent25b3791
1 file changed+24−8
Data/Aeson/TH.hs+24−8
Data/Aeson/TH.hs+24−8
@@ -49,6 +49,20 @@ d = Record { testOne = 3.14159
4949>>> fromJSON (toJSON d) == Success d
5050> True
5151
52+This also works for data family instances, but instead of passing in the data
53+family name (with double quotes), we pass in a data family instance
54+constructor (with a single quote):
55+
56+@
57+data family DF a
58+data instance DF Int = DF1 Int
59+ | DF2 Int Int
60+ deriving Eq
61+
62+$('deriveJSON' 'defaultOptions' 'DF1)
63+-- Alternatively, one could pass 'DF2 instead
64+@
65+
5266Please note that you can derive instances for tuples using the following syntax:
5367
5468@
@@ -125,7 +139,7 @@ import qualified Data.Vector.Mutable as VM ( unsafeNew, unsafeWrite )
125139--------------------------------------------------------------------------------
126140
127141-- | Generates both 'ToJSON' and 'FromJSON' instance declarations for the given
128−-- data type.
142+-- data type or data family instance constructor.
129143--
130144-- This is a convienience function which is equivalent to calling both
131145-- 'deriveToJSON' and 'deriveFromJSON'.
@@ -154,7 +168,8 @@ instance (ToJSON a) ⇒ ToJSON Foo where ...
154168The above (ToJSON a) constraint is not necessary and perhaps undesirable.
155169-}
156170
157−-- | Generates a 'ToJSON' instance declaration for the given data type.
171+-- | Generates a 'ToJSON' instance declaration for the given data type or
172+-- data family instance constructor.
158173deriveToJSON :: Options
159174 -- ^ Encoding options.
160175 -> Name
@@ -183,15 +198,15 @@ deriveToJSON opts name =
183198 (instanceCxt, instanceType) =
184199 buildTypeInstance name' ''ToJSON tvbs mbTys
185200
186−-- | Generates a lambda expression which encodes the given data type as a
187−-- 'Value'.
201+-- | Generates a lambda expression which encodes the given data type or
202+-- data family instance constructor as a 'Value'.
188203mkToJSON :: Options -- ^ Encoding options.
189204 -> Name -- ^ Name of the type to encode.
190205 -> Q Exp
191206mkToJSON opts name = withType name (\_ _ cons _ -> consToValue opts cons)
192207
193−-- | Generates a lambda expression which encodes the given data type
194−-- as a JSON string.
208+-- | Generates a lambda expression which encodes the given data type or
209+-- data family instance constructor as a JSON string.
195210mkToEncoding :: Options -- ^ Encoding options.
196211 -> Name -- ^ Name of the type to encode.
197212 -> Q Exp
@@ -537,7 +552,8 @@ argsToEncoding opts multiCons (ForallC _ _ con) =
537552-- FromJSON
538553--------------------------------------------------------------------------------
539554
540−-- | Generates a 'FromJSON' instance declaration for the given data type.
555+-- | Generates a 'FromJSON' instance declaration for the given data type or
556+-- data family instance constructor.
541557deriveFromJSON :: Options
542558 -- ^ Encoding options.
543559 -> Name
@@ -562,7 +578,7 @@ deriveFromJSON opts name =
562578 buildTypeInstance name' ''FromJSON tvbs mbTys
563579
564580-- | Generates a lambda expression which parses the JSON encoding of the given
565−-- data type.
581+-- data type or data family instance constructor.
566582mkParseJSON :: Options -- ^ Encoding options.
567583 -> Name -- ^ Name of the encoded type.
568584 -> Q Exp