@@ -2,15 +2,15 @@ module Data.Argonaut.Decode.Class where
22
33import Prelude
44
5- import Data.Array as Arr
65import Control.Alternative (class Plus )
76import Data.Argonaut.Core (Json , isNull , caseJsonNull , caseJsonBoolean , caseJsonNumber , caseJsonString , toArray , toObject , toString , stringify )
8- import Data.Bifunctor (lmap )
9- import Data.Either (Either (..))
7+ import Data.Array as Arr
8+ import Data.Bifunctor (lmap , rmap )
9+ import Data.Either (Either (..), note )
1010import Data.Int (fromNumber )
1111import Data.List (List (..), (:), fromFoldable )
12- import Data.Map as M
1312import Data.List as L
13+ import Data.Map as M
1414import Data.Maybe (maybe , Maybe (..))
1515import Data.NonEmpty (NonEmpty , singleton , (:|))
1616import Data.String (CodePoint , codePointAt )
@@ -70,22 +70,15 @@ instance decodeJsonString :: DecodeJson String where
7070instance decodeJsonJson :: DecodeJson Json where
7171 decodeJson = Right
7272
73-
74- toNonEmpty :: forall a f . (Plus f ) => ({ head :: f a -> Maybe a , tail :: f a -> Maybe (f a ) } ) -> (f a ) -> Either String (NonEmpty f a )
75- toNonEmpty i a = case (Tuple (i.head a) (i.tail a)) of
76- (Tuple Nothing _) -> Left " is empty."
77- (Tuple (Just h) Nothing ) -> Right $ singleton h
78- (Tuple (Just h) (Just t)) -> Right $ h :| t
79-
8073instance decodeJsonNonEmptyArray :: (DecodeJson a ) => DecodeJson (NonEmpty Array a ) where
8174 decodeJson
8275 = lmap (" Couldn't decode NonEmpty Array: " <> _)
83- <<< (traverse decodeJson <=< (lmap (" Array" <> _) <<< toNonEmpty { head : Arr .head, tail : Arr .tail } ) <=< decodeJArray)
76+ <<< (traverse decodeJson <=< (lmap (" JSON Array" <> _) <<< rmap (\x -> x .head :| x .tail) <<< note " is empty " <<< Arr .uncons ) <=< decodeJArray)
8477
8578instance decodeJsonNonEmptyList :: (DecodeJson a ) => DecodeJson (NonEmpty List a ) where
8679 decodeJson
8780 = lmap (" Couldn't decode NonEmpty List: " <> _)
88- <<< (traverse decodeJson <=< (lmap (" List " <> _) <<< toNonEmpty { head : L .head, tail : L .tail } ) <=< map (map fromFoldable) decodeJArray)
81+ <<< (traverse decodeJson <=< (lmap (" JSON Array " <> _) <<< rmap (\x -> x. head :| x. tail) <<< note " is empty " <<< L .uncons ) <=< map (map fromFoldable) decodeJArray)
8982
9083instance decodeJsonChar :: DecodeJson CodePoint where
9184 decodeJson j =
0 commit comments