@@ -49,6 +49,20 @@ d = Record { testOne = 3.14159
49>>> fromJSON (toJSON d) == Success d
49>>> fromJSON (toJSON d) == Success d
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):
58+data instance DF Int = DF1 Int
62+$('deriveJSON' 'defaultOptions' 'DF1)
63+-- Alternatively, one could pass 'DF2 instead
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:
@@ -125,7 +139,7 @@ import qualified Data.Vector.Mutable as VM ( unsafeNew, unsafeWrite )
125--------------------------------------------------------------------------------
139--------------------------------------------------------------------------------
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.
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.
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.
@@ -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
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.
191mkToJSON opts name = withType name (\_ _ cons _ -> consToValue opts cons)
206mkToJSON opts name = withType name (\_ _ cons _ -> consToValue opts cons)
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.
@@ -537,7 +552,8 @@ argsToEncoding opts multiCons (ForallC _ _ con) =
537-- FromJSON
552-- FromJSON
538--------------------------------------------------------------------------------
553--------------------------------------------------------------------------------
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.
@@ -562,7 +578,7 @@ deriveFromJSON opts name =
562 buildTypeInstance name' ''FromJSON tvbs mbTys
578 buildTypeInstance name' ''FromJSON tvbs mbTys
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.