on master help sign in
Documentation, example
1c8d8a0RyanGlScott
Data/Aeson/TH.hs+24−8
@@ -49,6 +49,20 @@ d = Record { testOne = 3.14159
49>>> fromJSON (toJSON d) == Success d
49>>> fromJSON (toJSON d) == Success d
50> True
50> True
51
51
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+
52Please note that you can derive instances for tuples using the following syntax:
66Please note that you can derive instances for tuples using the following syntax:
53
67
54@
68@
@@ -125,7 +139,7 @@ import qualified Data.Vector.Mutable as VM ( unsafeNew, unsafeWrite )
125--------------------------------------------------------------------------------
139--------------------------------------------------------------------------------
126
140
127-- | Generates both 'ToJSON' and 'FromJSON' instance declarations for the given
141-- | Generates both 'ToJSON' and 'FromJSON' instance declarations for the given
128−-- data type.
142+-- data type or data family instance constructor.
129--
143--
130-- This is a convienience function which is equivalent to calling both
144-- This is a convienience function which is equivalent to calling both
131-- 'deriveToJSON' and 'deriveFromJSON'.
145-- 'deriveToJSON' and 'deriveFromJSON'.
@@ -154,7 +168,8 @@ instance (ToJSON a) ⇒ ToJSON Foo where ...
154The above (ToJSON a) constraint is not necessary and perhaps undesirable.
168The above (ToJSON a) constraint is not necessary and perhaps undesirable.
155-}
169-}
156
170
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.
158deriveToJSON :: Options
173deriveToJSON :: Options
159 -- ^ Encoding options.
174 -- ^ Encoding options.
160 -> Name
175 -> Name
@@ -183,15 +198,15 @@ deriveToJSON opts name =
183 (instanceCxt, instanceType) =
198 (instanceCxt, instanceType) =
184 buildTypeInstance name' ''ToJSON tvbs mbTys
199 buildTypeInstance name' ''ToJSON tvbs mbTys
185
200
186−-- | Generates a lambda expression which encodes the given data type as a
201+-- | Generates a lambda expression which encodes the given data type or
187−-- 'Value'.
202+-- data family instance constructor as a 'Value'.
188mkToJSON :: Options -- ^ Encoding options.
203mkToJSON :: Options -- ^ Encoding options.
189 -> Name -- ^ Name of the type to encode.
204 -> Name -- ^ Name of the type to encode.
190 -> Q Exp
205 -> Q Exp
191mkToJSON opts name = withType name (\_ _ cons _ -> consToValue opts cons)
206mkToJSON opts name = withType name (\_ _ cons _ -> consToValue opts cons)
192
207
193−-- | Generates a lambda expression which encodes the given data type
208+-- | Generates a lambda expression which encodes the given data type or
194−-- as a JSON string.
209+-- data family instance constructor as a JSON string.
195mkToEncoding :: Options -- ^ Encoding options.
210mkToEncoding :: Options -- ^ Encoding options.
196 -> Name -- ^ Name of the type to encode.
211 -> Name -- ^ Name of the type to encode.
197 -> Q Exp
212 -> Q Exp
@@ -537,7 +552,8 @@ argsToEncoding opts multiCons (ForallC _ _ con) =
537-- FromJSON
552-- FromJSON
538--------------------------------------------------------------------------------
553--------------------------------------------------------------------------------
539
554
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.
541deriveFromJSON :: Options
557deriveFromJSON :: Options
542 -- ^ Encoding options.
558 -- ^ Encoding options.
543 -> Name
559 -> Name
@@ -562,7 +578,7 @@ deriveFromJSON opts name =
562 buildTypeInstance name' ''FromJSON tvbs mbTys
578 buildTypeInstance name' ''FromJSON tvbs mbTys
563
579
564-- | Generates a lambda expression which parses the JSON encoding of the given
580-- | Generates a lambda expression which parses the JSON encoding of the given
565−-- data type.
581+-- data type or data family instance constructor.
566mkParseJSON :: Options -- ^ Encoding options.
582mkParseJSON :: Options -- ^ Encoding options.
567 -> Name -- ^ Name of the encoded type.
583 -> Name -- ^ Name of the encoded type.
568 -> Q Exp
584 -> Q Exp