on master help sign in
Rename constructor name/type to constructor tag which is more accurate.

- Rename "constructorNameModifier" to "constructorTagModifier". - Rename "ObjectWithType" to "TaggedObject". - Rename "typeFieldName" to "tagFieldName". - Rename "defaultObjectWithType" to "defaultTaggedObject".

commit828321a3ebf063891d3d4a03e51f617f66e72b26 authorBas van Dijk <v.dijk.bas@gmail.com> dateTue 23 Apr 2013, 01:24 (13 years ago) parent00ebbbe
4 files changed+150−150
Data/Aeson/TH.hs+23−23 Data/Aeson/Types.hs+1−1 Data/Aeson/Types/Generic.hs+88−88 Data/Aeson/Types/Internal.hs+38−38
Data/Aeson/TH.hs+23−23
@@ -33,7 +33,7 @@ characters of every field name. We also modify constructor names by
3333lower-casing them:
3434
3535@
36−$('deriveJSON' 'defaultOptions'{'fieldNameModifier' = 'drop' 4, 'constructorNameModifier' = map toLower} ''D)
36+$('deriveJSON' 'defaultOptions'{'fieldNameModifier' = 'drop' 4, 'constructorTagModifier' = map toLower} ''D)
3737@
3838
3939Now we can use the newly created instances.
@@ -60,7 +60,7 @@ $('deriveJSON' 'defaultOptions' ''(,,,))
6060
6161module Data.Aeson.TH
6262 ( -- * Encoding configuration
63− Options(..), SumEncoding(..), defaultOptions, defaultObjectWithType
63+ Options(..), SumEncoding(..), defaultOptions, defaultTaggedObject
6464
6565 -- * FromJSON and ToJSON derivation
6666 , deriveJSON
@@ -85,7 +85,7 @@ import Data.Aeson.Types ( Value(..), Parser
8585 , Options(..)
8686 , SumEncoding(..)
8787 , defaultOptions
88− , defaultObjectWithType
88+ , defaultTaggedObject
8989 )
9090-- from base:
9191import Control.Applicative ( pure, (<$>), (<*>) )
@@ -221,7 +221,7 @@ conTxt :: Options -> Name -> Q Exp
221221conTxt opts = appE [|T.pack|] . conStringE opts
222222
223223conStringE :: Options -> Name -> Q Exp
224−conStringE opts = stringE . constructorNameModifier opts . nameBase
224+conStringE opts = stringE . constructorTagModifier opts . nameBase
225225
226226-- | If constructor is nullary.
227227isNullary :: Con -> Bool
@@ -234,9 +234,9 @@ encodeSum opts multiCons conName exp
234234 case sumEncoding opts of
235235 TwoElemArray ->
236236 [|Array|] `appE` ([|V.fromList|] `appE` listE [conStr opts conName, exp])
237− ObjectWithType{typeFieldName, contentsFieldName} ->
237+ TaggedObject{tagFieldName, contentsFieldName} ->
238238 [|object|] `appE` listE
239− [ infixApp [|T.pack typeFieldName|] [|(.=)|] (conStr opts conName)
239+ [ infixApp [|T.pack tagFieldName|] [|(.=)|] (conStr opts conName)
240240 , infixApp [|T.pack contentsFieldName|] [|(.=)|] exp
241241 ]
242242 ObjectWithSingleField ->
@@ -324,11 +324,11 @@ encodeArgs opts multiCons (RecC conName ts) = do
324324 $ if multiCons
325325 then case sumEncoding opts of
326326 TwoElemArray -> [|toJSON|] `appE` tupE [conStr opts conName, exp]
327− ObjectWithType{typeFieldName} ->
327+ TaggedObject{tagFieldName} ->
328328 [|object|] `appE`
329329 -- TODO: Maybe throw an error in case
330− -- typeFieldName overwrites a field in pairs.
331− infixApp (infixApp [|T.pack typeFieldName|]
330+ -- tagFieldName overwrites a field in pairs.
331+ infixApp (infixApp [|T.pack tagFieldName|]
332332 [|(.=)|]
333333 (conStr opts conName))
334334 [|(:)|]
@@ -454,8 +454,8 @@ consFromJSON tName opts cons = do
454454
455455 mixedMatches =
456456 case sumEncoding opts of
457− ObjectWithType {typeFieldName, contentsFieldName} ->
458− parseObject $ parseObjectWithType typeFieldName contentsFieldName
457+ TaggedObject {tagFieldName, contentsFieldName} ->
458+ parseObject $ parseTaggedObject tagFieldName contentsFieldName
459459 ObjectWithSingleField ->
460460 parseObject $ parseObjectWithSingleField
461461 TwoElemArray ->
@@ -496,13 +496,13 @@ consFromJSON tName opts cons = do
496496 []
497497 ]
498498
499− parseObjectWithType typFieldName valFieldName obj = do
499+ parseTaggedObject typFieldName valFieldName obj = do
500500 conKey <- newName "conKey"
501501 doE [ bindS (varP conKey)
502502 (infixApp (varE obj)
503503 [|(.:)|]
504504 ([|T.pack|] `appE` stringE typFieldName))
505− , noBindS $ parseContents conKey (Left (valFieldName, obj)) 'conNotFoundFailObjectWithType
505+ , noBindS $ parseContents conKey (Left (valFieldName, obj)) 'conNotFoundFailTaggedObject
506506 ]
507507
508508 parse2ElemArray arr = do
@@ -571,7 +571,7 @@ consFromJSON tName opts cons = do
571571 `appE` (litE $ stringL $ show tName)
572572 `appE` listE (map ( litE
573573 . stringL
574− . constructorNameModifier opts
574+ . constructorTagModifier opts
575575 . nameBase
576576 . getConName
577577 ) cons
@@ -623,7 +623,7 @@ parseRecord opts tName conName ts obj =
623623 where
624624 x:xs = [ [|lookupField|]
625625 `appE` (litE $ stringL $ show tName)
626− `appE` (litE $ stringL $ constructorNameModifier opts $ nameBase conName)
626+ `appE` (litE $ stringL $ constructorTagModifier opts $ nameBase conName)
627627 `appE` (varE obj)
628628 `appE` ( [|T.pack|] `appE` fieldNameExp opts field
629629 )
@@ -777,7 +777,7 @@ firstElemNoStringFail t o = fail $ printf "When parsing %s expected an Array of
777777
778778wrongPairCountFail :: String -> String -> Parser fail
779779wrongPairCountFail t n =
780− fail $ printf "When parsing %s expected an Object with a single name/value pair but got %s pairs."
780+ fail $ printf "When parsing %s expected an Object with a single tag/contents pair but got %s pairs."
781781 t n
782782
783783noStringFail :: String -> String -> Parser fail
@@ -785,24 +785,24 @@ noStringFail t o = fail $ printf "When parsing %s expected String but got %s." t
785785
786786noMatchFail :: String -> String -> Parser fail
787787noMatchFail t o =
788− fail $ printf "When parsing %s expected a String with the name of a constructor but got %s." t o
788+ fail $ printf "When parsing %s expected a String with the tag of a constructor but got %s." t o
789789
790790not2ElemArray :: String -> Int -> Parser fail
791791not2ElemArray t i = fail $ printf "When parsing %s expected an Array of 2 elements but got %i elements" t i
792792
793793conNotFoundFail2ElemArray :: String -> [String] -> String -> Parser fail
794794conNotFoundFail2ElemArray t cs o =
795− fail $ printf "When parsing %s expected a 2-element Array with a name and value element where the name is one of [%s], but got %s."
795+ fail $ printf "When parsing %s expected a 2-element Array with a tag and contents element where the tag is one of [%s], but got %s."
796796 t (intercalate ", " cs) o
797797
798798conNotFoundFailObjectSingleField :: String -> [String] -> String -> Parser fail
799799conNotFoundFailObjectSingleField t cs o =
800− fail $ printf "When parsing %s expected an Object with a name/value pair where the name is one of [%s], but got %s."
800+ fail $ printf "When parsing %s expected an Object with a single tag/contents pair where the tag is one of [%s], but got %s."
801801 t (intercalate ", " cs) o
802802
803−conNotFoundFailObjectWithType :: String -> [String] -> String -> Parser fail
804−conNotFoundFailObjectWithType t cs o =
805− fail $ printf "When parsing %s expected an Object with a type field where the value is one of [%s], but got %s."
803+conNotFoundFailTaggedObject :: String -> [String] -> String -> Parser fail
804+conNotFoundFailTaggedObject t cs o =
805+ fail $ printf "When parsing %s expected an Object with a tag field where the value is one of [%s], but got %s."
806806 t (intercalate ", " cs) o
807807
808808parseTypeMismatch' :: String -> String -> String -> String -> Parser fail
@@ -854,7 +854,7 @@ tvbName (KindedTV name _) = name
854854conNameExp :: Options -> Con -> Q Exp
855855conNameExp opts = litE
856856 . stringL
857− . constructorNameModifier opts
857+ . constructorTagModifier opts
858858 . nameBase
859859 . getConName
860860
Data/Aeson/Types.hs+1−1
@@ -60,7 +60,7 @@ module Data.Aeson.Types
6060 , Options(..)
6161 , SumEncoding(..)
6262 , defaultOptions
63− , defaultObjectWithType
63+ , defaultTaggedObject
6464 ) where
6565
6666import Data.Aeson.Types.Class
Data/Aeson/Types/Generic.hs+88−88
@@ -88,67 +88,67 @@ class SumToJSON f allNullary where
8888 sumToJSON :: Options -> f a -> Tagged allNullary Value
8989
9090instance ( GetConName f
91− , ObjectWithType f
91+ , TaggedObject f
9292 , ObjectWithSingleField f
9393 , TwoElemArray f ) => SumToJSON f True where
9494 sumToJSON opts
9595 | nullaryToString opts = Tagged . String . pack
96− . constructorNameModifier opts . getConName
96+ . constructorTagModifier opts . getConName
9797 | otherwise = Tagged . nonAllNullarySumToJSON opts
9898 {-# INLINE sumToJSON #-}
9999
100100instance ( TwoElemArray f
101− , ObjectWithType f
101+ , TaggedObject f
102102 , ObjectWithSingleField f ) => SumToJSON f False where
103103 sumToJSON opts = Tagged . nonAllNullarySumToJSON opts
104104 {-# INLINE sumToJSON #-}
105105
106106nonAllNullarySumToJSON :: ( TwoElemArray f
107− , ObjectWithType f
107+ , TaggedObject f
108108 , ObjectWithSingleField f
109109 ) => Options -> f a -> Value
110110nonAllNullarySumToJSON opts =
111111 case sumEncoding opts of
112− ObjectWithType{..} -> object . objectWithType opts typeFieldName
113− contentsFieldName
112+ TaggedObject{..} -> object . taggedObject opts tagFieldName
113+ contentsFieldName
114114 ObjectWithSingleField -> Object . objectWithSingleField opts
115115 TwoElemArray -> Array . twoElemArray opts
116116{-# INLINE nonAllNullarySumToJSON #-}
117117
118118--------------------------------------------------------------------------------
119119
120−class ObjectWithType f where
121− objectWithType :: Options -> String -> String -> f a -> [Pair]
122−
123−instance ( ObjectWithType a
124− , ObjectWithType b ) => ObjectWithType (a :+: b) where
125− objectWithType opts typeFieldName contentsFieldName (L1 x) =
126− objectWithType opts typeFieldName contentsFieldName x
127− objectWithType opts typeFieldName contentsFieldName (R1 x) =
128− objectWithType opts typeFieldName contentsFieldName x
129− {-# INLINE objectWithType #-}
130−
131−instance ( IsRecord a isRecord
132− , ObjectWithType' a isRecord
133− , Constructor c ) => ObjectWithType (C1 c a) where
134− objectWithType opts typeFieldName contentsFieldName =
135− (pack typeFieldName .= constructorNameModifier opts
120+class TaggedObject f where
121+ taggedObject :: Options -> String -> String -> f a -> [Pair]
122+
123+instance ( TaggedObject a
124+ , TaggedObject b ) => TaggedObject (a :+: b) where
125+ taggedObject opts tagFieldName contentsFieldName (L1 x) =
126+ taggedObject opts tagFieldName contentsFieldName x
127+ taggedObject opts tagFieldName contentsFieldName (R1 x) =
128+ taggedObject opts tagFieldName contentsFieldName x
129+ {-# INLINE taggedObject #-}
130+
131+instance ( IsRecord a isRecord
132+ , TaggedObject' a isRecord
133+ , Constructor c ) => TaggedObject (C1 c a) where
134+ taggedObject opts tagFieldName contentsFieldName =
135+ (pack tagFieldName .= constructorTagModifier opts
136136 (conName (undefined :: t c a p)) :) .
137137 (unTagged :: Tagged isRecord [Pair] -> [Pair]) .
138− objectWithType' opts contentsFieldName . unM1
139− {-# INLINE objectWithType #-}
138+ taggedObject' opts contentsFieldName . unM1
139+ {-# INLINE taggedObject #-}
140140
141−class ObjectWithType' f isRecord where
142− objectWithType' :: Options -> String -> f a -> Tagged isRecord [Pair]
141+class TaggedObject' f isRecord where
142+ taggedObject' :: Options -> String -> f a -> Tagged isRecord [Pair]
143143
144−instance (RecordToPairs f) => ObjectWithType' f True where
145− objectWithType' opts _ = Tagged . toList . recordToPairs opts
146− {-# INLINE objectWithType' #-}
144+instance (RecordToPairs f) => TaggedObject' f True where
145+ taggedObject' opts _ = Tagged . toList . recordToPairs opts
146+ {-# INLINE taggedObject' #-}
147147
148−instance (GToJSON f) => ObjectWithType' f False where
149− objectWithType' opts contentsFieldName =
148+instance (GToJSON f) => TaggedObject' f False where
149+ taggedObject' opts contentsFieldName =
150150 Tagged . (:[]) . (pack contentsFieldName .=) . gToJSON opts
151− {-# INLINE objectWithType' #-}
151+ {-# INLINE taggedObject' #-}
152152
153153--------------------------------------------------------------------------------
154154
@@ -179,7 +179,7 @@ instance ( GToJSON a, ConsToJSON a
179179 , Constructor c ) => TwoElemArray (C1 c a) where
180180 twoElemArray opts x = V.create $ do
181181 mv <- VM.unsafeNew 2
182− VM.unsafeWrite mv 0 $ String $ pack $ constructorNameModifier opts
182+ VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
183183 $ conName (undefined :: t c a p)
184184 VM.unsafeWrite mv 1 $ gToJSON opts x
185185 return mv
@@ -277,7 +277,7 @@ instance ( GToJSON a, ConsToJSON a
277277 , Constructor c ) => ObjectWithSingleField (C1 c a) where
278278 objectWithSingleField opts = H.singleton typ . gToJSON opts
279279 where
280− typ = pack $ constructorNameModifier opts $
280+ typ = pack $ constructorTagModifier opts $
281281 conName (undefined :: t c a p)
282282 {-# INLINE objectWithSingleField #-}
283283
@@ -339,16 +339,16 @@ instance ( AllNullary (a :+: b) allNullary
339339class ParseSum f allNullary where
340340 parseSum :: Options -> Value -> Tagged allNullary (Parser (f a))
341341
342−instance ( SumFromString (a :+: b)
343− , FromPair (a :+: b)
344− , FromObjectWithType (a :+: b) ) => ParseSum (a :+: b) True where
342+instance ( SumFromString (a :+: b)
343+ , FromPair (a :+: b)
344+ , FromTaggedObject (a :+: b) ) => ParseSum (a :+: b) True where
345345 parseSum opts
346346 | nullaryToString opts = Tagged . parseAllNullarySum opts
347347 | otherwise = Tagged . parseNonAllNullarySum opts
348348 {-# INLINE parseSum #-}
349349
350−instance ( FromPair (a :+: b)
351− , FromObjectWithType (a :+: b) ) => ParseSum (a :+: b) False where
350+instance ( FromPair (a :+: b)
351+ , FromTaggedObject (a :+: b) ) => ParseSum (a :+: b) False where
352352 parseSum opts = Tagged . parseNonAllNullarySum opts
353353 {-# INLINE parseSum #-}
354354
@@ -372,89 +372,89 @@ instance (Constructor c) => SumFromString (C1 c U1) where
372372 parseSumFromString opts key | key == name = Just $ M1 U1
373373 | otherwise = Nothing
374374 where
375− name = pack $ constructorNameModifier opts $
375+ name = pack $ constructorTagModifier opts $
376376 conName (undefined :: t c U1 p)
377377 {-# INLINE parseSumFromString #-}
378378
379379--------------------------------------------------------------------------------
380380
381381parseNonAllNullarySum :: ( FromPair (a :+: b)
382− , FromObjectWithType (a :+: b)
382+ , FromTaggedObject (a :+: b)
383383 ) => Options -> Value -> Parser ((a :+: b) c)
384384parseNonAllNullarySum opts =
385385 case sumEncoding opts of
386− ObjectWithType{..} ->
386+ TaggedObject{..} ->
387387 withObject "Object" $ \obj -> do
388− key <- obj .: pack typeFieldName
389− fromMaybe (notFound $ unpack key) $
390− parseFromObjectWithType opts contentsFieldName obj key
388+ tag <- obj .: pack tagFieldName
389+ fromMaybe (notFound $ unpack tag) $
390+ parseFromTaggedObject opts contentsFieldName obj tag
391391
392392 ObjectWithSingleField ->
393393 withObject "Object" $ \obj ->
394394 case H.toList obj of
395− [keyVal@(key, _)] -> fromMaybe (notFound $ unpack key) $
396− parsePair opts keyVal
395+ [pair@(tag, _)] -> fromMaybe (notFound $ unpack tag) $
396+ parsePair opts pair
397397 _ -> fail "Object doesn't have a single field"
398398
399399 TwoElemArray ->
400400 withArray "Array" $ \arr ->
401401 if V.length arr == 2
402402 then case V.unsafeIndex arr 0 of
403− String key -> fromMaybe (notFound $ unpack key) $
404− parsePair opts (key, V.unsafeIndex arr 1)
403+ String tag -> fromMaybe (notFound $ unpack tag) $
404+ parsePair opts (tag, V.unsafeIndex arr 1)
405405 _ -> fail "First element is not a String"
406406 else fail "Array doesn't have 2 elements"
407407{-# INLINE parseNonAllNullarySum #-}
408408
409409--------------------------------------------------------------------------------
410410
411−class FromObjectWithType f where
412− parseFromObjectWithType :: Options -> String -> Object -> Text
413− -> Maybe (Parser (f a))
414−
415−instance (FromObjectWithType a, FromObjectWithType b) =>
416− FromObjectWithType (a :+: b) where
417− parseFromObjectWithType opts contentsFieldName obj key =
418− (fmap L1 <$> parseFromObjectWithType opts contentsFieldName obj key) <|>
419− (fmap R1 <$> parseFromObjectWithType opts contentsFieldName obj key)
420− {-# INLINE parseFromObjectWithType #-}
421−
422−instance ( FromObjectWithType' f
423− , Constructor c ) => FromObjectWithType (C1 c f) where
424− parseFromObjectWithType opts contentsFieldName obj key
425− | key == name = Just $ M1 <$> parseFromObjectWithType'
411+class FromTaggedObject f where
412+ parseFromTaggedObject :: Options -> String -> Object -> Text
413+ -> Maybe (Parser (f a))
414+
415+instance (FromTaggedObject a, FromTaggedObject b) =>
416+ FromTaggedObject (a :+: b) where
417+ parseFromTaggedObject opts contentsFieldName obj tag =
418+ (fmap L1 <$> parseFromTaggedObject opts contentsFieldName obj tag) <|>
419+ (fmap R1 <$> parseFromTaggedObject opts contentsFieldName obj tag)
420+ {-# INLINE parseFromTaggedObject #-}
421+
422+instance ( FromTaggedObject' f
423+ , Constructor c ) => FromTaggedObject (C1 c f) where
424+ parseFromTaggedObject opts contentsFieldName obj tag
425+ | tag == name = Just $ M1 <$> parseFromTaggedObject'
426426 opts contentsFieldName obj
427427 | otherwise = Nothing
428428 where
429− name = pack $ constructorNameModifier opts $
429+ name = pack $ constructorTagModifier opts $
430430 conName (undefined :: t c f p)
431− {-# INLINE parseFromObjectWithType #-}
431+ {-# INLINE parseFromTaggedObject #-}
432432
433433--------------------------------------------------------------------------------
434434
435−class FromObjectWithType' f where
436− parseFromObjectWithType' :: Options -> String -> Object -> Parser (f a)
435+class FromTaggedObject' f where
436+ parseFromTaggedObject' :: Options -> String -> Object -> Parser (f a)
437437
438−class FromObjectWithType'' f isRecord where
439− parseFromObjectWithType'' :: Options -> String -> Object
440− -> Tagged isRecord (Parser (f a))
438+class FromTaggedObject'' f isRecord where
439+ parseFromTaggedObject'' :: Options -> String -> Object
440+ -> Tagged isRecord (Parser (f a))
441441
442−instance ( IsRecord f isRecord
443− , FromObjectWithType'' f isRecord
444− ) => FromObjectWithType' f where
445− parseFromObjectWithType' opts contentsFieldName =
442+instance ( IsRecord f isRecord
443+ , FromTaggedObject'' f isRecord
444+ ) => FromTaggedObject' f where
445+ parseFromTaggedObject' opts contentsFieldName =
446446 (unTagged :: Tagged isRecord (Parser (f a)) -> Parser (f a)) .
447− parseFromObjectWithType'' opts contentsFieldName
448− {-# INLINE parseFromObjectWithType' #-}
447+ parseFromTaggedObject'' opts contentsFieldName
448+ {-# INLINE parseFromTaggedObject' #-}
449449
450−instance (FromRecord f) => FromObjectWithType'' f True where
451− parseFromObjectWithType'' opts _ = Tagged . parseRecord opts
452− {-# INLINE parseFromObjectWithType'' #-}
450+instance (FromRecord f) => FromTaggedObject'' f True where
451+ parseFromTaggedObject'' opts _ = Tagged . parseRecord opts
452+ {-# INLINE parseFromTaggedObject'' #-}
453453
454−instance (GFromJSON f) => FromObjectWithType'' f False where
455− parseFromObjectWithType'' opts contentsFieldName = Tagged .
454+instance (GFromJSON f) => FromTaggedObject'' f False where
455+ parseFromTaggedObject'' opts contentsFieldName = Tagged .
456456 (gParseJSON opts <=< (.: pack contentsFieldName))
457− {-# INLINE parseFromObjectWithType'' #-}
457+ {-# INLINE parseFromTaggedObject'' #-}
458458
459459--------------------------------------------------------------------------------
460460
@@ -546,16 +546,16 @@ class FromPair f where
546546 parsePair :: Options -> Pair -> Maybe (Parser (f a))
547547
548548instance (FromPair a, FromPair b) => FromPair (a :+: b) where
549− parsePair opts keyVal = (fmap L1 <$> parsePair opts keyVal) <|>
550− (fmap R1 <$> parsePair opts keyVal)
549+ parsePair opts pair = (fmap L1 <$> parsePair opts pair) <|>
550+ (fmap R1 <$> parsePair opts pair)
551551 {-# INLINE parsePair #-}
552552
553553instance (Constructor c, GFromJSON a, ConsFromJSON a) => FromPair (C1 c a) where
554− parsePair opts (key, value)
555− | key == name = Just $ gParseJSON opts value
554+ parsePair opts (tag, value)
555+ | tag == tag' = Just $ gParseJSON opts value
556556 | otherwise = Nothing
557557 where
558− name = pack $ constructorNameModifier opts $
558+ tag' = pack $ constructorTagModifier opts $
559559 conName (undefined :: t c a p)
560560 {-# INLINE parsePair #-}
561561
Data/Aeson/Types/Internal.hs+38−38
@@ -34,7 +34,7 @@ module Data.Aeson.Types.Internal
3434 , Options(..)
3535 , SumEncoding(..)
3636 , defaultOptions
37− , defaultObjectWithType
37+ , defaultTaggedObject
3838 ) where
3939
4040import Control.Applicative
@@ -254,14 +254,14 @@ data Options = Options
254254 { fieldNameModifier :: String -> String
255255 -- ^ Function applied to field names.
256256 -- Handy for removing common record prefixes for example.
257− , constructorNameModifier :: String -> String
258− -- ^ Function applied to constructor names.
259− -- Handy for lower-casing constructor names for example.
257+ , constructorTagModifier :: String -> String
258+ -- ^ Function applied to constructor tags which could be handy
259+ -- for lower-casing them for example.
260260 , nullaryToString :: Bool
261− -- ^ If 'True' the constructors of a datatype, with /all/ nullary
262− -- constructors, will be encoded to a string with the
263− -- constructor name. If 'False' the encoding will always follow
264− -- the `sumEncoding`.
261+ -- ^ If 'True' the constructors of a datatype, with /all/
262+ -- nullary constructors, will be encoded to just a string with
263+ -- the constructor tag. If 'False' the encoding will always
264+ -- follow the `sumEncoding`.
265265 , omitNothingFields :: Bool
266266 -- ^ If 'True' record fields with a 'Nothing' value will be
267267 -- omitted from the resulting object. If 'False' the resulting
@@ -272,27 +272,27 @@ data Options = Options
272272
273273-- | Specifies how to encode constructors of a sum datatype.
274274data SumEncoding =
275− ObjectWithType { typeFieldName :: String
276− , contentsFieldName :: String
277− }
275+ TaggedObject { tagFieldName :: String
276+ , contentsFieldName :: String
277+ }
278278 -- ^ A constructor will be encoded to an object with a field
279− -- 'typeFieldName' which specifies the constructor name (modified
280− -- by the 'constructorNameModifier'). If the constructor is a
281− -- record the encoded record fields will be unpacked into this
282− -- object. So make sure that your record doesn't have a field with
283− -- the same name as the 'typeFieldName'. Otherwise the type gets
284− -- overwritten by that field! If the constructor is not a record
285− -- the encoded constructor contents will be stored under the
286− -- 'contentsFieldName' field.
279+ -- 'tagFieldName' which specifies the constructor tag (modified by
280+ -- the 'constructorTagModifier'). If the constructor is a record
281+ -- the encoded record fields will be unpacked into this object. So
282+ -- make sure that your record doesn't have a field with the same
283+ -- label as the 'tagFieldName'. Otherwise the tag gets overwritten
284+ -- by the encoded value of that field! If the constructor is not a
285+ -- record the encoded constructor contents will be stored under
286+ -- the 'contentsFieldName' field.
287287 | ObjectWithSingleField
288288 -- ^ A constructor will be encoded to an object with a single
289− -- field named after the constructor (modified by the
290− -- 'constructorNameModifier') which maps to the encoded contents
291− -- of the constructor.
289+ -- field named after the constructor tag (modified by the
290+ -- 'constructorTagModifier') which maps to the encoded contents of
291+ -- the constructor.
292292 | TwoElemArray
293293 -- ^ A constructor will be encoded to a 2-element array where the
294− -- first element is the name of the constructor (modified by the
295− -- 'constructorNameModifier') and the second element the encoded
294+ -- first element is the tag of the constructor (modified by the
295+ -- 'constructorTagModifier') and the second element the encoded
296296 -- contents of the constructor.
297297
298298-- | Default encoding 'Options':
@@ -300,31 +300,31 @@ data SumEncoding =
300300-- @
301301-- 'Options'
302302-- { 'fieldNameModifier' = id
303−-- , 'constructorNameModifier' = id
303+-- , 'constructorTagModifier' = id
304304-- , 'nullaryToString' = True
305305-- , 'omitNothingFields' = False
306−-- , 'sumEncoding' = 'defaultObjectWithType'
306+-- , 'sumEncoding' = 'defaultTaggedObject'
307307-- }
308308-- @
309309defaultOptions :: Options
310310defaultOptions = Options
311311 { fieldNameModifier = id
312− , constructorNameModifier = id
312+ , constructorTagModifier = id
313313 , nullaryToString = True
314314 , omitNothingFields = False
315− , sumEncoding = defaultObjectWithType
315+ , sumEncoding = defaultTaggedObject
316316 }
317317
318−-- | Default 'ObjectWithType' 'SumEncoding' options:
318+-- | Default 'TaggedObject' 'SumEncoding' options:
319319--
320320-- @
321−-- defaultObjectWithType = 'ObjectWithType'
322−-- { 'typeFieldName' = \"type\"
323−-- , 'contentsFieldName' = \"contents\"
324−-- }
321+-- defaultTaggedObject = 'TaggedObject'
322+-- { 'tagFieldName' = \"tag\"
323+-- , 'contentsFieldName' = \"contents\"
324+-- }
325325-- @
326−defaultObjectWithType :: SumEncoding
327−defaultObjectWithType = ObjectWithType
328− { typeFieldName = "type"
329− , contentsFieldName = "contents"
330− }
326+defaultTaggedObject :: SumEncoding
327+defaultTaggedObject = TaggedObject
328+ { tagFieldName = "tag"
329+ , contentsFieldName = "contents"
330+ }