@@ -35,49 +35,50 @@ import qualified Data.Vector.Mutable as VM
35-- Generic toJSON
35-- Generic toJSON
37instance (GToJSON a) => GToJSON (M1 i c a) where
37instance (GToJSON a) => GToJSON (M1 i c a) where
38− gToJSON = gToJSON . unM1
38+ gToJSON opts = gToJSON opts . unM1
39 {-# INLINE gToJSON #-}
39 {-# INLINE gToJSON #-}
41instance (ToJSON a) => GToJSON (K1 i a) where
41instance (ToJSON a) => GToJSON (K1 i a) where
42− gToJSON = toJSON . unK1
42+ gToJSON _opts = toJSON . unK1
43 {-# INLINE gToJSON #-}
43 {-# INLINE gToJSON #-}
45instance GToJSON U1 where
45instance GToJSON U1 where
46− gToJSON _ = emptyArray
46+ gToJSON _opts _ = emptyArray
47 {-# INLINE gToJSON #-}
47 {-# INLINE gToJSON #-}
49instance (ConsToJSON a) => GToJSON (C1 c a) where
49instance (ConsToJSON a) => GToJSON (C1 c a) where
50− gToJSON = consToJSON . unM1
50+ gToJSON opts = consToJSON opts . unM1
51 {-# INLINE gToJSON #-}
51 {-# INLINE gToJSON #-}
53instance ( GProductToValues a, GProductToValues b
53instance ( GProductToValues a, GProductToValues b
54 , ProductSize a, ProductSize b) => GToJSON (a :*: b) where
54 , ProductSize a, ProductSize b) => GToJSON (a :*: b) where
55− gToJSON p = Array $ V.create $ do
56+ Array $ V.create $ do
56− mv <- VM.unsafeNew lenProduct
57+ mv <- VM.unsafeNew lenProduct
57− gProductToValues mv 0 lenProduct p
58+ gProductToValues opts mv 0 lenProduct p
58− return mv
59+ return mv
60 lenProduct = unTagged2 (productSize :: Tagged2 (a :*: b) Int)
61 lenProduct = unTagged2 (productSize :: Tagged2 (a :*: b) Int)
61 {-# INLINE gToJSON #-}
62 {-# INLINE gToJSON #-}
63instance (GObject a, GObject b) => GToJSON (a :+: b) where
64instance (GObject a, GObject b) => GToJSON (a :+: b) where
64− gToJSON (L1 x) = Object $ gObject x
65+ gToJSON opts (L1 x) = Object $ gObject opts x
65− gToJSON (R1 x) = Object $ gObject x
66+ gToJSON opts (R1 x) = Object $ gObject opts x
66 {-# INLINE gToJSON #-}
67 {-# INLINE gToJSON #-}
68--------------------------------------------------------------------------------
69--------------------------------------------------------------------------------
70−class ConsToJSON f where consToJSON :: f a -> Value
71+class ConsToJSON f where consToJSON :: Options -> f a -> Value
71−class ConsToJSON' b f where consToJSON' :: Tagged b (f a -> Value)
72+class ConsToJSON' b f where consToJSON' :: Tagged b (Options -> f a -> Value)
73newtype Tagged s b = Tagged {unTagged :: b}
74newtype Tagged s b = Tagged {unTagged :: b}
75instance (IsRecord f b, ConsToJSON' b f) => ConsToJSON f where
76instance (IsRecord f b, ConsToJSON' b f) => ConsToJSON f where
76− consToJSON = unTagged (consToJSON' :: Tagged b (f a -> Value))
77+ consToJSON = unTagged (consToJSON' :: Tagged b (Options -> f a -> Value))
77 {-# INLINE consToJSON #-}
78 {-# INLINE consToJSON #-}
79instance (GRecordToPairs f) => ConsToJSON' True f where
80instance (GRecordToPairs f) => ConsToJSON' True f where
80− consToJSON' = Tagged (object . toList . gRecordToPairs)
81+ consToJSON' = Tagged (\opts -> object . toList . gRecordToPairs opts)
81 {-# INLINE consToJSON' #-}
82 {-# INLINE consToJSON' #-}
83instance GToJSON f => ConsToJSON' False f where
84instance GToJSON f => ConsToJSON' False f where
@@ -87,24 +88,33 @@ instance GToJSON f => ConsToJSON' False f where
87--------------------------------------------------------------------------------
88--------------------------------------------------------------------------------
89class GRecordToPairs f where
90class GRecordToPairs f where
90− gRecordToPairs :: f a -> DList Pair
91+ gRecordToPairs :: Options -> f a -> DList Pair
92instance (GRecordToPairs a, GRecordToPairs b) => GRecordToPairs (a :*: b) where
93instance (GRecordToPairs a, GRecordToPairs b) => GRecordToPairs (a :*: b) where
93− gRecordToPairs (a :*: b) = gRecordToPairs a `mappend` gRecordToPairs b
94+ gRecordToPairs opts (a :*: b) = gRecordToPairs opts a `mappend`
95+ gRecordToPairs opts b
94 {-# INLINE gRecordToPairs #-}
96 {-# INLINE gRecordToPairs #-}
96instance (Selector s, GToJSON a) => GRecordToPairs (S1 s a) where
98instance (Selector s, GToJSON a) => GRecordToPairs (S1 s a) where
97− gRecordToPairs m1 = pure (pack (selName m1), gToJSON (unM1 m1))
99+ gRecordToPairs opts m1 = pure ( pack $ fieldNameModifier opts $ selName m1
100+ , gToJSON opts (unM1 m1)
98 {-# INLINE gRecordToPairs #-}
102 {-# INLINE gRecordToPairs #-}
100--------------------------------------------------------------------------------
104--------------------------------------------------------------------------------
102class GProductToValues f where
106class GProductToValues f where
107+ gProductToValues :: Options
103− gProductToValues :: VM.MVector s Value -> Int -> Int -> f a -> ST s ()
108+ -> VM.MVector s Value
105instance (GProductToValues a, GProductToValues b) => GProductToValues (a :*: b) where
114instance (GProductToValues a, GProductToValues b) => GProductToValues (a :*: b) where
106− gProductToValues mv ix len (a :*: b) = do gProductToValues mv ix lenL a
115+ gProductToValues opts mv ix len (a :*: b) = do
116+ gProductToValues opts mv ix lenL a
107− gProductToValues mv ixR lenR b
117+ gProductToValues opts mv ixR lenR b
109 lenL = len `shiftR` 1
119 lenL = len `shiftR` 1
110 ixR = ix + lenL
120 ixR = ix + lenL
@@ -112,48 +122,50 @@ instance (GProductToValues a, GProductToValues b) => GProductToValues (a :*: b)
112 {-# INLINE gProductToValues #-}
122 {-# INLINE gProductToValues #-}
114instance (GToJSON a) => GProductToValues a where
124instance (GToJSON a) => GProductToValues a where
115− gProductToValues mv ix _ = VM.unsafeWrite mv ix . gToJSON
125+ gProductToValues opts mv ix _ = VM.unsafeWrite mv ix . gToJSON opts
116 {-# INLINE gProductToValues #-}
126 {-# INLINE gProductToValues #-}
118--------------------------------------------------------------------------------
128--------------------------------------------------------------------------------
120class GObject f where
130class GObject f where
121− gObject :: f a -> Object
131+ gObject :: Options -> f a -> Object
123instance (GObject a, GObject b) => GObject (a :+: b) where
133instance (GObject a, GObject b) => GObject (a :+: b) where
124− gObject (L1 x) = gObject x
134+ gObject opts (L1 x) = gObject opts x
125− gObject (R1 x) = gObject x
135+ gObject opts (R1 x) = gObject opts x
126 {-# INLINE gObject #-}
136 {-# INLINE gObject #-}
128instance (Constructor c, GToJSON a, ConsToJSON a) => GObject (C1 c a) where
138instance (Constructor c, GToJSON a, ConsToJSON a) => GObject (C1 c a) where
139+ gObject opts = H.singleton (pack $ constructorNameModifier opts
129− gObject = H.singleton (pack $ conName (undefined :: t c a p)) . gToJSON
140+ $ conName (undefined :: t c a p))
130 {-# INLINE gObject #-}
142 {-# INLINE gObject #-}
132--------------------------------------------------------------------------------
144--------------------------------------------------------------------------------
133-- Generic parseJSON
145-- Generic parseJSON
135instance (GFromJSON a) => GFromJSON (M1 i c a) where
147instance (GFromJSON a) => GFromJSON (M1 i c a) where
136− gParseJSON = fmap M1 . gParseJSON
148+ gParseJSON opts = fmap M1 . gParseJSON opts
137 {-# INLINE gParseJSON #-}
149 {-# INLINE gParseJSON #-}
139instance (FromJSON a) => GFromJSON (K1 i a) where
151instance (FromJSON a) => GFromJSON (K1 i a) where
140− gParseJSON = fmap K1 . parseJSON
152+ gParseJSON _opts = fmap K1 . parseJSON
141 {-# INLINE gParseJSON #-}
153 {-# INLINE gParseJSON #-}
143instance GFromJSON U1 where
155instance GFromJSON U1 where
144− gParseJSON v
156+ gParseJSON _opts v
145 | isEmptyArray v = pure U1
157 | isEmptyArray v = pure U1
146 | otherwise = typeMismatch "unit constructor (U1)" v
158 | otherwise = typeMismatch "unit constructor (U1)" v
147 {-# INLINE gParseJSON #-}
159 {-# INLINE gParseJSON #-}
149instance (ConsFromJSON a) => GFromJSON (C1 c a) where
161instance (ConsFromJSON a) => GFromJSON (C1 c a) where
150− gParseJSON = fmap M1 . consParseJSON
162+ gParseJSON opts = fmap M1 . consParseJSON opts
151 {-# INLINE gParseJSON #-}
163 {-# INLINE gParseJSON #-}
153instance ( GFromProduct a, GFromProduct b
165instance ( GFromProduct a, GFromProduct b
154 , ProductSize a, ProductSize b) => GFromJSON (a :*: b) where
166 , ProductSize a, ProductSize b) => GFromJSON (a :*: b) where
155− gParseJSON (Array arr)
167+ gParseJSON opts (Array arr)
156− | lenArray == lenProduct = gParseProduct arr 0 lenProduct
168+ | lenArray == lenProduct = gParseProduct opts arr 0 lenProduct
157 | otherwise =
169 | otherwise =
158 fail $ "When expecting a product of " ++ show lenProduct ++
170 fail $ "When expecting a product of " ++ show lenProduct ++
159 " values, encountered an Array of " ++ show lenArray ++
171 " values, encountered an Array of " ++ show lenArray ++
@@ -162,15 +174,15 @@ instance ( GFromProduct a, GFromProduct b
162 lenArray = V.length arr
174 lenArray = V.length arr
163 lenProduct = unTagged2 (productSize :: Tagged2 (a :*: b) Int)
175 lenProduct = unTagged2 (productSize :: Tagged2 (a :*: b) Int)
165− gParseJSON v = typeMismatch "product (:*:)" v
177+ gParseJSON _opts v = typeMismatch "product (:*:)" v
166 {-# INLINE gParseJSON #-}
178 {-# INLINE gParseJSON #-}
168instance (GFromSum a, GFromSum b) => GFromJSON (a :+: b) where
180instance (GFromSum a, GFromSum b) => GFromJSON (a :+: b) where
169− gParseJSON (Object (H.toList -> [keyVal@(key, _)])) =
181+ gParseJSON opts (Object (H.toList -> [keyVal@(key, _)])) =
170− case gParseSum keyVal of
182+ case gParseSum opts keyVal of
171 Nothing -> notFound $ unpack key
183 Nothing -> notFound $ unpack key
172 Just p -> p
184 Just p -> p
173− gParseJSON v = typeMismatch "sum (:+:)" v
185+ gParseJSON _opts v = typeMismatch "sum (:+:)" v
174 {-# INLINE gParseJSON #-}
186 {-# INLINE gParseJSON #-}
176notFound :: String -> Parser a
188notFound :: String -> Parser a
@@ -179,18 +191,21 @@ notFound key = fail $ "The key \"" ++ key ++ "\" was not found"
180--------------------------------------------------------------------------------
192--------------------------------------------------------------------------------
182−class ConsFromJSON f where consParseJSON :: Value -> Parser (f a)
194+class ConsFromJSON f where
195+ consParseJSON :: Options -> Value -> Parser (f a)
196+class ConsFromJSON' b f where
183−class ConsFromJSON' b f where consParseJSON' :: Tagged b (Value -> Parser (f a))
197+ consParseJSON' :: Tagged b (Options -> Value -> Parser (f a))
185instance (IsRecord f b, ConsFromJSON' b f) => ConsFromJSON f where
199instance (IsRecord f b, ConsFromJSON' b f) => ConsFromJSON f where
186− consParseJSON = unTagged (consParseJSON' :: Tagged b (Value -> Parser (f a)))
201+ unTagged (consParseJSON' :: Tagged b (Options -> Value -> Parser (f a)))
187 {-# INLINE consParseJSON #-}
202 {-# INLINE consParseJSON #-}
189instance (GFromRecord f) => ConsFromJSON' True f where
204instance (GFromRecord f) => ConsFromJSON' True f where
190 consParseJSON' = Tagged parseRecord
205 consParseJSON' = Tagged parseRecord
192− parseRecord (Object obj) = gParseRecord obj
207+ parseRecord opts (Object obj) = gParseRecord opts obj
193− parseRecord v = typeMismatch "record (:*:)" v
208+ parseRecord _opts v = typeMismatch "record (:*:)" v
194 {-# INLINE consParseJSON' #-}
209 {-# INLINE consParseJSON' #-}
196instance (GFromJSON f) => ConsFromJSON' False f where
211instance (GFromJSON f) => ConsFromJSON' False f where
@@ -200,16 +215,18 @@ instance (GFromJSON f) => ConsFromJSON' False f where
200--------------------------------------------------------------------------------
215--------------------------------------------------------------------------------
202class GFromRecord f where
217class GFromRecord f where
203− gParseRecord :: Object -> Parser (f a)
218+ gParseRecord :: Options -> Object -> Parser (f a)
205instance (GFromRecord a, GFromRecord b) => GFromRecord (a :*: b) where
220instance (GFromRecord a, GFromRecord b) => GFromRecord (a :*: b) where
206− gParseRecord obj = (:*:) <$> gParseRecord obj <*> gParseRecord obj
221+ gParseRecord opts obj = (:*:) <$> gParseRecord opts obj
222+ <*> gParseRecord opts obj
207 {-# INLINE gParseRecord #-}
223 {-# INLINE gParseRecord #-}
209instance (Selector s, GFromJSON a) => GFromRecord (S1 s a) where
225instance (Selector s, GFromJSON a) => GFromRecord (S1 s a) where
210− gParseRecord = maybe (notFound key) gParseJSON . H.lookup (T.pack key)
226+ gParseRecord opts = maybe (notFound key) (gParseJSON opts)
227+ . H.lookup (T.pack key)
212− key = selName (undefined :: t s a p)
229+ key = fieldNameModifier opts $ selName (undefined :: t s a p)
213 {-# INLINE gParseRecord #-}
230 {-# INLINE gParseRecord #-}
215--------------------------------------------------------------------------------
232--------------------------------------------------------------------------------
@@ -229,11 +246,12 @@ instance ProductSize (S1 s a) where
229--------------------------------------------------------------------------------
246--------------------------------------------------------------------------------
231class GFromProduct f where
248class GFromProduct f where
232− gParseProduct :: Array -> Int -> Int -> Parser (f a)
249+ gParseProduct :: Options -> Array -> Int -> Int -> Parser (f a)
234instance (GFromProduct a, GFromProduct b) => GFromProduct (a :*: b) where
251instance (GFromProduct a, GFromProduct b) => GFromProduct (a :*: b) where
252+ gParseProduct opts arr ix len =
235− gParseProduct arr ix len = (:*:) <$> gParseProduct arr ix lenL
253+ (:*:) <$> gParseProduct opts arr ix lenL
236− <*> gParseProduct arr ixR lenR
254+ <*> gParseProduct opts arr ixR lenR
238 lenL = len `shiftR` 1
256 lenL = len `shiftR` 1
239 ixR = ix + lenL
257 ixR = ix + lenL
@@ -241,22 +259,25 @@ instance (GFromProduct a, GFromProduct b) => GFromProduct (a :*: b) where
241 {-# INLINE gParseProduct #-}
259 {-# INLINE gParseProduct #-}
243instance (GFromJSON a) => GFromProduct (S1 s a) where
261instance (GFromJSON a) => GFromProduct (S1 s a) where
244− gParseProduct arr ix _ = gParseJSON $ V.unsafeIndex arr ix
262+ gParseProduct opts arr ix _ = gParseJSON opts $ V.unsafeIndex arr ix
245 {-# INLINE gParseProduct #-}
263 {-# INLINE gParseProduct #-}
247--------------------------------------------------------------------------------
265--------------------------------------------------------------------------------
249class GFromSum f where
267class GFromSum f where
250− gParseSum :: Pair -> Maybe (Parser (f a))
268+ gParseSum :: Options -> Pair -> Maybe (Parser (f a))
252instance (GFromSum a, GFromSum b) => GFromSum (a :+: b) where
270instance (GFromSum a, GFromSum b) => GFromSum (a :+: b) where
253− gParseSum keyVal = (fmap L1 <$> gParseSum keyVal) <|>
271+ gParseSum opts keyVal = (fmap L1 <$> gParseSum opts keyVal) <|>
254− (fmap R1 <$> gParseSum keyVal)
272+ (fmap R1 <$> gParseSum opts keyVal)
255 {-# INLINE gParseSum #-}
273 {-# INLINE gParseSum #-}
257instance (Constructor c, GFromJSON a, ConsFromJSON a) => GFromSum (C1 c a) where
275instance (Constructor c, GFromJSON a, ConsFromJSON a) => GFromSum (C1 c a) where
258− gParseSum (key, value)
276+ gParseSum opts (key, value)
277+ | key == pack ( constructorNameModifier opts
259− | key == pack (conName (undefined :: t c a p)) = Just $ gParseJSON value
278+ $ conName (undefined :: t c a p)
280+ = Just $ gParseJSON opts value
260 | otherwise = Nothing
281 | otherwise = Nothing
261 {-# INLINE gParseSum #-}
282 {-# INLINE gParseSum #-}