@@ -2,15 +2,15 @@ module Data.Argonaut.Decode.Class where
2
2
3
3
import Prelude
4
4
5
- import Data.Array as Arr
6
5
import Control.Alternative (class Plus )
7
6
import 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 )
10
10
import Data.Int (fromNumber )
11
11
import Data.List (List (..), (:), fromFoldable )
12
- import Data.Map as M
13
12
import Data.List as L
13
+ import Data.Map as M
14
14
import Data.Maybe (maybe , Maybe (..))
15
15
import Data.NonEmpty (NonEmpty , singleton , (:|))
16
16
import Data.String (CodePoint , codePointAt )
@@ -70,22 +70,15 @@ instance decodeJsonString :: DecodeJson String where
70
70
instance decodeJsonJson :: DecodeJson Json where
71
71
decodeJson = Right
72
72
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
-
80
73
instance decodeJsonNonEmptyArray :: (DecodeJson a ) => DecodeJson (NonEmpty Array a ) where
81
74
decodeJson
82
75
= 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)
84
77
85
78
instance decodeJsonNonEmptyList :: (DecodeJson a ) => DecodeJson (NonEmpty List a ) where
86
79
decodeJson
87
80
= 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)
89
82
90
83
instance decodeJsonChar :: DecodeJson CodePoint where
91
84
decodeJson j =
0 commit comments