on master help sign in
omit optional fields when null while decoding
d56df0ekana-sama
src/JRec/Internal.hs+11−1
@@ -631,9 +631,19 @@ class RecJsonParse (lts :: [*]) where
631instance RecJsonParse '[] where
631instance RecJsonParse '[] where
632 recJsonParse _ initSize _ = pure (ForallST (unsafeRNil initSize))
632 recJsonParse _ initSize _ = pure (ForallST (unsafeRNil initSize))
633
633
634+class FromJSON a => ParseField a where
635+ parseField :: Object -> T.Text -> Parser a
636+
637+instance FromJSON a => ParseField a where
638+ parseField = (.:)
639+
640+instance {-# OVERLAPS #-} FromJSON a => ParseField (Maybe a) where
641+ parseField = (.:?)
642+
634instance
643instance
635 ( KnownSymbol l,
644 ( KnownSymbol l,
636 FromJSON t,
645 FromJSON t,
646+ ParseField t,
637 RecJsonParse lts,
647 RecJsonParse lts,
638 RecSize lts ~ s,
648 RecSize lts ~ s,
639 KnownNat s,
649 KnownNat s,
@@ -645,7 +655,7 @@ instance
645 let lbl :: FldProxy l
655 let lbl :: FldProxy l
646 lbl = FldProxy
656 lbl = FldProxy
647 rest <- recJsonParse options initSize obj
657 rest <- recJsonParse options initSize obj
648− (v :: t) <- obj .: T.pack (fieldTransform options (symbolVal lbl))
658+ (v :: t) <- obj `parseField` T.pack (fieldTransform options (symbolVal lbl))
649 pure $ ForallST (unsafeRCons (lbl := v) =<< unForallST rest)
659 pure $ ForallST (unsafeRCons (lbl := v) =<< unForallST rest)
650
660
651-- | Machinery for NFData
661-- | Machinery for NFData