on master help sign in
Add defaultObjectWithType and improve documentation
commitb8e917ea35bed6c7d5f82c9cbec0b752b9c9dc6b authorBas van Dijk <v.dijk.bas@gmail.com> dateFri 18 Jan 2013, 18:26 (14 years ago) parenta0325b2
1 file changed+35−17
Data/Aeson/TH.hs+35−17
Data/Aeson/TH.hs+35−17
@@ -27,12 +27,13 @@ data D a = Nullary
2727 } deriving Eq
2828@
2929
30−Next we derive the necessary instances. Note that we make use of the feature to
31−change record field names. In this case we drop the first 4 characters of every
32−field name.
30+Next we derive the necessary instances. Note that we make use of the
31+feature to change record field names. In this case we drop the first 4
32+characters of every field name. We also modify constructor names by
33+lower-casing them:
3334
3435@
35−$('deriveJSON' 'defaultOptions'{'fieldNameModifier' = 'drop' 4} ''D)
36+$('deriveJSON' 'defaultOptions'{'fieldNameModifier' = 'drop' 4, 'constructorNameModifier' = map toLower} ''D)
3637@
3738
3839Now we can use the newly created instances.
@@ -58,7 +59,7 @@ $('deriveJSON' 'defaultOptions' ''(,,,))
5859-}
5960
6061module Data.Aeson.TH
61− ( Options(..), SumEncoding(..), defaultOptions
62+ ( Options(..), SumEncoding(..), defaultOptions, defaultObjectWithType
6263
6364 , deriveJSON
6465
@@ -136,24 +137,27 @@ data Options = Options
136137data SumEncoding =
137138 TwoElemArray -- ^ A constructor will be encoded to a 2-element
138139 -- array where the first element is the name of the
139− -- constructor and the second element the content of
140− -- the constructor.
140+ -- constructor (modified by the
141+ -- 'constructorNameModifier') and the second element
142+ -- the content of the constructor.
141143 | ObjectWithType { typeFieldName :: String
142144 , valueFieldName :: String
143145 }
144146 -- ^ A constructor will be encoded to an object with a field
145− -- 'typeFieldName' which specifies the constructor name. If the
146− -- constructor is not a record the constructor content will be
147− -- stored under the 'valueFieldName' field.
147+ -- 'typeFieldName' which specifies the constructor name (modified
148+ -- by the 'constructorNameModifier'). If the constructor is not a
149+ -- record the constructor content will be stored under the
150+ -- 'valueFieldName' field.
148151 | ObjectWithSingleField
149152 -- ^ A constructor will be encoded to an object with a single
150− -- field named after the constructor and the value will be the
151− -- contents of the constructor.
152−
153−-- | Default encoding options which specify to not modify field names,
154−-- encode the constructors of a datatype with all nullary constructors
155−-- to just strings with the name of the constructor and use a
156−-- 2-element array for other sum datatypes.
153+ -- field named after the constructor (modified by the
154+ -- 'constructorNameModifier') and the value will be the contents
155+ -- of the constructor.
156+
157+-- | Default encoding options which specify to not modify field and
158+-- constructor names, encode the constructors of a datatype with all
159+-- nullary constructors to just strings with the name of the
160+-- constructor and use a 2-element array for other sum datatypes.
157161defaultOptions :: Options
158162defaultOptions = Options
159163 { fieldNameModifier = id
@@ -162,6 +166,20 @@ defaultOptions = Options
162166 , sumEncoding = TwoElemArray
163167 }
164168
169+-- | Note that:
170+--
171+-- @
172+-- defaultObjectWithType = 'ObjectWithType'
173+-- { 'typeFieldName' = \"type\"
174+-- , 'valueFieldName' = \"value\"
175+-- }
176+-- @
177+defaultObjectWithType :: SumEncoding
178+defaultObjectWithType = ObjectWithType
179+ { typeFieldName = "type"
180+ , valueFieldName = "value"
181+ }
182+
165183--------------------------------------------------------------------------------
166184-- Convenience
167185--------------------------------------------------------------------------------