Skip to content

Commit a49b224

Browse files
author
lyxia
committed
Track JSONPath in derived parsers
1 parent e518edc commit a49b224

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

Data/Aeson/TH.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ import Data.Aeson.Types ( Value(..), Parser
107107
, defaultOptions
108108
, defaultTaggedObject
109109
)
110-
import Data.Aeson.Types.Internal (Encoding(..))
110+
import Data.Aeson.Types.Internal (Encoding(..), (<?>), JSONPathElement(Key))
111111
import Control.Monad ( liftM2, return, mapM, fail )
112112
import Data.Bool ( Bool(False, True), otherwise, (&&), not )
113113
import Data.Either ( Either(Left, Right) )
@@ -1010,7 +1010,7 @@ instance OVERLAPPABLE_ (FromJSON a) => LookupField a where
10101010
lookupField tName rec obj key =
10111011
case H.lookup key obj of
10121012
Nothing -> unknownFieldFail tName rec (T.unpack key)
1013-
Just v -> parseJSON v
1013+
Just v -> parseJSON v <?> Key key
10141014

10151015
instance (FromJSON a) => LookupField (Maybe a) where
10161016
lookupField _ _ = (.:?)

Data/Aeson/Types/Generic.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -679,12 +679,11 @@ instance (FromRecord a, FromRecord b) => FromRecord (a :*: b) where
679679
<*> parseRecord opts Nothing obj
680680

681681
instance (Selector s, GFromJSON a) => FromRecord (S1 s a) where
682-
parseRecord opts (Just lab) = maybe (notFound $ unpack lab)
683-
(gParseJSON opts) . H.lookup lab
684-
parseRecord opts Nothing = maybe (notFound label)
685-
(gParseJSON opts) . H.lookup (pack label)
682+
parseRecord opts lab = gParseJSON opts <=< (.: label)
686683
where
687-
label = fieldLabelModifier opts $ selName (undefined :: t s a p)
684+
label = fromMaybe defLabel lab
685+
defLabel = pack . fieldLabelModifier opts $
686+
selName (undefined :: t s a p)
688687

689688
instance OVERLAPPING_ (Selector s, FromJSON a) =>
690689
FromRecord (S1 s (K1 i (Maybe a))) where

0 commit comments

Comments
 (0)