on master help sign in
Track JSONPath in derived parsers
commitcdf6db1412d13c5bd05089cd9e4007175dcbc3fc authorlyxia <li-yao.xia@ens.fr> dateFri 20 May 2016, 17:27 (10 years ago) parente518edc
2 files changed+6−7
Data/Aeson/TH.hs+2−2 Data/Aeson/Types/Generic.hs+4−5
Data/Aeson/TH.hs+2−2
@@ -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))
111111import Control.Monad ( liftM2, return, mapM, fail )
112112import Data.Bool ( Bool(False, True), otherwise, (&&), not )
113113import 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
10151015instance (FromJSON a) => LookupField (Maybe a) where
10161016 lookupField _ _ = (.:?)
Data/Aeson/Types/Generic.hs+4−5
@@ -679,12 +679,11 @@ instance (FromRecord a, FromRecord b) => FromRecord (a :*: b) where
679679 <*> parseRecord opts Nothing obj
680680
681681instance (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 = (<?> Key label) . 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
689688instance OVERLAPPING_ (Selector s, FromJSON a) =>
690689 FromRecord (S1 s (K1 i (Maybe a))) where