@@ -67,7 +67,7 @@ instance decodeJsonNumber :: DecodeJson Number where
6767 decodeJson = caseJsonNumber (Left " Value is not a Number" ) Right
6868
6969instance decodeJsonInt :: DecodeJson Int where
70- decodeJson =
70+ decodeJson =
7171 maybe (Left " Value is not an integer" ) Right
7272 <<< fromNumber
7373 <=< decodeJson
@@ -79,22 +79,22 @@ instance decodeJsonJson :: DecodeJson Json where
7979 decodeJson = Right
8080
8181instance decodeJsonNonEmpty_Array :: (DecodeJson a ) => DecodeJson (NonEmpty Array a ) where
82- decodeJson =
82+ decodeJson =
8383 lmap (" Couldn't decode NonEmpty Array: " <> _)
8484 <<< (traverse decodeJson <=< (lmap (" JSON Array" <> _) <<< rmap (\x -> x.head :| x.tail) <<< note " is empty" <<< Arr .uncons) <=< decodeJArray)
8585
8686instance decodeJsonNonEmptyArray :: (DecodeJson a ) => DecodeJson (NonEmptyArray a ) where
87- decodeJson =
87+ decodeJson =
8888 lmap (" Couldn't decode NonEmptyArray: " <> _)
8989 <<< (traverse decodeJson <=< (lmap (" JSON Array" <> _) <<< rmap (\x -> NEA .cons' x.head x.tail) <<< note " is empty" <<< Arr .uncons) <=< decodeJArray)
9090
9191instance decodeJsonNonEmpty_List :: (DecodeJson a ) => DecodeJson (NonEmpty List a ) where
92- decodeJson =
92+ decodeJson =
9393 lmap (" Couldn't decode NonEmpty List: " <> _)
9494 <<< (traverse decodeJson <=< (lmap (" JSON Array" <> _) <<< rmap (\x -> x.head :| x.tail) <<< note " is empty" <<< L .uncons) <=< map (map fromFoldable) decodeJArray)
9595
9696instance decodeJsonNonEmptyList :: (DecodeJson a ) => DecodeJson (NonEmptyList a ) where
97- decodeJson =
97+ decodeJson =
9898 lmap (" Couldn't decode NonEmptyList: " <> _)
9999 <<< (traverse decodeJson <=< (lmap (" JSON Array" <> _) <<< rmap (\x -> NEL .cons' x.head x.tail) <<< note " is empty" <<< L .uncons) <=< map (map fromFoldable) decodeJArray)
100100
@@ -104,20 +104,20 @@ instance decodeJsonChar :: DecodeJson CodePoint where
104104 =<< codePointAt 0 <$> decodeJson j
105105
106106instance decodeForeignObject :: DecodeJson a => DecodeJson (FO.Object a ) where
107- decodeJson =
107+ decodeJson =
108108 lmap (" Couldn't decode ForeignObject: " <> _)
109109 <<< (traverse decodeJson <=< decodeJObject)
110110
111111instance decodeArray :: DecodeJson a => DecodeJson (Array a ) where
112- decodeJson =
113- lmap (" Couldn't decode Array (" <> _)
112+ decodeJson =
113+ lmap (" Couldn't decode Array (" <> _)
114114 <<< (traverseWithIndex f <=< decodeJArray)
115115 where
116116 msg i m = " Failed at index " <> show i <> " ): " <> m
117117 f i = lmap (msg i) <<< decodeJson
118118
119119instance decodeList :: DecodeJson a => DecodeJson (List a ) where
120- decodeJson =
120+ decodeJson =
121121 lmap (" Couldn't decode List: " <> _)
122122 <<< (traverse decodeJson <=< map (map fromFoldable) decodeJArray)
123123
@@ -160,19 +160,25 @@ instance gDecodeJsonCons
160160 , Row.Lacks field rowTail
161161 )
162162 => GDecodeJson row (RL.Cons field value tail ) where
163- gDecodeJson object _ = do
164- let
163+ gDecodeJson object _ =
164+ let
165165 sProxy :: SProxy field
166166 sProxy = SProxy
167167
168168 fieldName = reflectSymbol sProxy
169+ in case FO .lookup fieldName object of
170+ Just jsonVal -> do
171+ val <- elaborateFailure fieldName <<< decodeJson $ jsonVal
169172
170- rest <- gDecodeJson object (RLProxy :: RLProxy tail )
173+ rest <- gDecodeJson object (RLProxy :: RLProxy tail )
171174
172- case FO .lookup fieldName object of
173- Just jsonVal -> do
174- val <- decodeJson jsonVal
175175 Right $ Record .insert sProxy val rest
176176
177177 Nothing ->
178178 Left $ " JSON was missing expected field: " <> fieldName
179+
180+ elaborateFailure :: ∀ a . String -> Either String a -> Either String a
181+ elaborateFailure s e =
182+ lmap msg e
183+ where
184+ msg m = " Failed to decode key '" <> s <> " ': " <> m
0 commit comments