@@ -3,7 +3,7 @@ module Data.Argonaut.JCursor where
33import Prelude
44
55import Data.Argonaut.Core as J
6- import Data.Argonaut.Decode (class DecodeJson , decodeJson )
6+ import Data.Argonaut.Decode (class DecodeJson , decodeJson , JsonDecodeError (..) )
77import Data.Argonaut.Encode (class EncodeJson , encodeJson )
88import Data.Array as A
99import Data.Either (Either (..))
@@ -173,19 +173,19 @@ fromPrims lst = foldl f (inferEmpty <<< fst <$> head lst) lst
173173 f :: Maybe J.Json -> Tuple JCursor JsonPrim -> Maybe J.Json
174174 f j (Tuple c p) = j >>= cursorSet c (primToJson p)
175175
176- fail :: forall a b . Show a => a -> Either String b
177- fail x = Left $ " Expected String or Number but found: " <> show x
178-
179176instance decodeJsonJCursor :: DecodeJson JCursor where
180177 decodeJson j = decodeJson j >>= loop
181178 where
182- loop :: Array J.Json -> Either String JCursor
179+ loop :: Array J.Json -> Either JsonDecodeError JCursor
183180 loop arr =
184181 maybe (Right JCursorTop ) goLoop $ Tuple <$> A .head arr <*> A .tail arr
185- goLoop :: Tuple J.Json (Array J.Json ) -> Either String JCursor
182+ goLoop :: Tuple J.Json (Array J.Json ) -> Either JsonDecodeError JCursor
186183 goLoop (Tuple x xs) = do
187184 c <- loop xs
188- J .caseJson fail fail (goNum c) (Right <<< flip JField c) (map J .stringify >>> fail) (map J .stringify >>> fail) x
189- goNum :: JCursor -> Number -> Either String JCursor
185+ let
186+ fail :: forall a . Either JsonDecodeError a
187+ fail = Left (Named " Int or String" $ UnexpectedValue x)
188+ J .caseJson (const fail) (const fail) (goNum c) (Right <<< flip JField c) (const fail) (const fail) x
189+ goNum :: JCursor -> Number -> Either JsonDecodeError JCursor
190190 goNum c =
191- maybe (Left " Not an Int" ) (Right <<< flip JIndex c) <<< I .fromNumber
191+ maybe (Left $ TypeMismatch " Int" ) (Right <<< flip JIndex c) <<< I .fromNumber
0 commit comments