on master help sign in
Add defaultObjectWithType and improve documentation
b8e917eBas van Dijk
Data/Aeson/TH.hs+35−17
@@ -27,12 +27,13 @@ data D a = Nullary
27 } deriving Eq
27 } deriving Eq
28@
28@
29
29
30−Next we derive the necessary instances. Note that we make use of the feature to
30+Next we derive the necessary instances. Note that we make use of the
31−change record field names. In this case we drop the first 4 characters of every
31+feature to change record field names. In this case we drop the first 4
32−field name.
32+characters of every field name. We also modify constructor names by
33+lower-casing them:
33
34
34@
35@
35−$('deriveJSON' 'defaultOptions'{'fieldNameModifier' = 'drop' 4} ''D)
36+$('deriveJSON' 'defaultOptions'{'fieldNameModifier' = 'drop' 4, 'constructorNameModifier' = map toLower} ''D)
36@
37@
37
38
38Now we can use the newly created instances.
39Now we can use the newly created instances.
@@ -58,7 +59,7 @@ $('deriveJSON' 'defaultOptions' ''(,,,))
58-}
59-}
59
60
60module Data.Aeson.TH
61module Data.Aeson.TH
61− ( Options(..), SumEncoding(..), defaultOptions
62+ ( Options(..), SumEncoding(..), defaultOptions, defaultObjectWithType
62
63
63 , deriveJSON
64 , deriveJSON
64
65
@@ -136,24 +137,27 @@ data Options = Options
136data SumEncoding =
137data SumEncoding =
137 TwoElemArray -- ^ A constructor will be encoded to a 2-element
138 TwoElemArray -- ^ A constructor will be encoded to a 2-element
138 -- array where the first element is the name of the
139 -- array where the first element is the name of the
140+ -- constructor (modified by the
139− -- constructor and the second element the content of
141+ -- 'constructorNameModifier') and the second element
140− -- the constructor.
142+ -- the content of the constructor.
141 | ObjectWithType { typeFieldName :: String
143 | ObjectWithType { typeFieldName :: String
142 , valueFieldName :: String
144 , valueFieldName :: String
143 }
145 }
144 -- ^ A constructor will be encoded to an object with a field
146 -- ^ A constructor will be encoded to an object with a field
145− -- 'typeFieldName' which specifies the constructor name. If the
147+ -- 'typeFieldName' which specifies the constructor name (modified
148+ -- by the 'constructorNameModifier'). If the constructor is not a
146− -- constructor is not a record the constructor content will be
149+ -- record the constructor content will be stored under the
147− -- stored under the 'valueFieldName' field.
150+ -- 'valueFieldName' field.
148 | ObjectWithSingleField
151 | ObjectWithSingleField
149 -- ^ A constructor will be encoded to an object with a single
152 -- ^ A constructor will be encoded to an object with a single
150− -- field named after the constructor and the value will be the
153+ -- field named after the constructor (modified by the
154+ -- 'constructorNameModifier') and the value will be the contents
151− -- contents of the constructor.
155+ -- of the constructor.
152−
156+
153−-- | Default encoding options which specify to not modify field names,
157+-- | Default encoding options which specify to not modify field and
154−-- encode the constructors of a datatype with all nullary constructors
158+-- constructor names, encode the constructors of a datatype with all
155−-- to just strings with the name of the constructor and use a
159+-- nullary constructors to just strings with the name of the
156−-- 2-element array for other sum datatypes.
160+-- constructor and use a 2-element array for other sum datatypes.
157defaultOptions :: Options
161defaultOptions :: Options
158defaultOptions = Options
162defaultOptions = Options
159 { fieldNameModifier = id
163 { fieldNameModifier = id
@@ -162,6 +166,20 @@ defaultOptions = Options
162 , sumEncoding = TwoElemArray
166 , sumEncoding = TwoElemArray
163 }
167 }
164
168
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+
165--------------------------------------------------------------------------------
183--------------------------------------------------------------------------------
166-- Convenience
184-- Convenience
167--------------------------------------------------------------------------------
185--------------------------------------------------------------------------------