@@ -87,6 +87,12 @@ data Rec (lts :: [*]) = MkRec
8787 { _unRec :: SmallArray# Any -- Note that the values are physically in reverse order
8888 }
8989
90+data JSONOptions = JSONOptions
91+ {fieldTransform :: String -> String}
92+
93+defaultJSONOptions :: JSONOptions
94+defaultJSONOptions = JSONOptions {fieldTransform = id}
95+
9096type role Rec representational
9197
9298instance
@@ -107,11 +113,11 @@ instance
107113 ) =>
108114 ToJSON (Rec lts)
109115 where
110− toJSON = recToValue
111− toEncoding = recToEncoding
116+ toJSON = recToValue defaultJSONOptions
117+ toEncoding = recToEncoding defaultJSONOptions
112118
113119instance (RecSize lts ~ s, KnownNat s, RecJsonParse lts) => FromJSON (Rec lts) where
114− parseJSON = recJsonParser
120+ parseJSON = recJsonParser defaultJSONOptions
115121
116122instance RecNfData lts lts => NFData (Rec lts) where
117123 rnf = recNfData (Proxy :: Proxy lts)
@@ -222,7 +228,7 @@ type family KeyDoesNotExist (l :: Symbol) (lts :: [*]) :: Constraint where
222228 )
223229 KeyDoesNotExist q (l := t ': lts) = KeyDoesNotExist q lts
224230
225−type family Reverse (xs :: [*]) where
231+type family Reverse (xs :: [*]) where
226232 Reverse '[] = '[]
227233 Reverse (x ': xs) = RecAppend (Reverse xs) '[x]
228234
@@ -430,7 +436,7 @@ union lts rts =
430436{-# INLINE union #-}
431437
432438-- | Insert a field
433−--
439+--
434440-- Insert at beginning, unless the field already exists, in which case set it
435441-- directly.
436442insert ::
@@ -459,8 +465,6 @@ insert (l := v) rts =
459465 (# s''''#, a# #) -> (# s''''#, MkRec a# #)
460466{-# INLINE insert #-}
461467
462−
463−
464468-- | Alias for 'combine'
465469(++:) ::
466470 forall lhs rhs res.
@@ -538,16 +542,21 @@ reflectRecFold _ f r =
538542showRec :: forall lts. (RecApply lts lts Show) => Rec lts -> [(String, String)]
539543showRec = reflectRec @Show Proxy (\k v -> (k, show v))
540544
541−recToValue :: forall lts. (RecApply lts lts ToJSON) => Rec lts -> Value
542−recToValue r = object $ reflectRec @ToJSON Proxy (\k v -> (T.pack k, toJSON v)) r
545+recToValue :: forall lts. (RecApply lts lts ToJSON) => JSONOptions -> Rec lts -> Value
546+recToValue options r =
547+ object $ reflectRec @ToJSON Proxy (\k v -> (T.pack (fieldTransform options k), toJSON v)) r
543548
544−recToEncoding :: forall lts. (RecApply lts lts ToJSON) => Rec lts -> Encoding
545−recToEncoding r = pairs $ mconcat $ reflectRec @ToJSON Proxy (\k v -> (T.pack k .= v)) r
549+recToEncoding :: forall lts. (RecApply lts lts ToJSON) => JSONOptions -> Rec lts -> Encoding
550+recToEncoding options r =
551+ pairs $ mconcat $ reflectRec @ToJSON Proxy (\k v -> (T.pack (fieldTransform options k) .= v)) r
546552
547−recJsonParser :: forall lts s. (RecSize lts ~ s, KnownNat s, RecJsonParse lts) => Value -> Parser (Rec lts)
548−recJsonParser =
553+recJsonParser ::
554+ forall lts s.
555+ (RecSize lts ~ s, KnownNat s, RecJsonParse lts) =>
556+ (JSONOptions -> Value -> Parser (Rec lts))
557+recJsonParser options =
549558 withObject "Record" $ \o ->
550− recJsonParse initSize o
559+ recJsonParse options initSize o
551560 where
552561 initSize = fromIntegral $ natVal' (proxy# :: Proxy# s)
553562
@@ -607,10 +616,10 @@ type family RemoveAccessTo (l :: Symbol) (lts :: [*]) :: [*] where
607616
608617-- | Machinery to implement parseJSON
609618class RecJsonParse (lts :: [*]) where
610− recJsonParse :: Int -> Object -> Parser (Rec lts)
619+ recJsonParse :: JSONOptions -> Int -> Object -> Parser (Rec lts)
611620
612621instance RecJsonParse '[] where
613− recJsonParse initSize _ = pure (unsafeRNil initSize)
622+ recJsonParse _ initSize _ = pure (unsafeRNil initSize)
614623
615624instance
616625 ( KnownSymbol l,
@@ -622,13 +631,12 @@ instance
622631 ) =>
623632 RecJsonParse (l := t ': lts)
624633 where
625− recJsonParse initSize obj =
626− do
627− let lbl :: FldProxy l
628− lbl = FldProxy
629− rest <- recJsonParse initSize obj
630− (v :: t) <- obj .: T.pack (symbolVal lbl)
631− pure $ unsafeRCons (lbl := v) rest
634+ recJsonParse options initSize obj = do
635+ let lbl :: FldProxy l
636+ lbl = FldProxy
637+ rest <- recJsonParse options initSize obj
638+ (v :: t) <- obj .: T.pack (fieldTransform options (symbolVal lbl))
639+ pure $ unsafeRCons (lbl := v) rest
632640
633641-- | Machinery for NFData
634642class RecNfData (lts :: [*]) (rts :: [*]) where
@@ -744,7 +752,7 @@ class NoConstraint x
744752instance NoConstraint x
745753
746754-- | Convert a record into a list of fields.
747−--
755+--
748756-- | Not present in original superrecord
749757getFields :: RecApply fields fields NoConstraint => Rec fields -> [Any]
750758getFields =