@@ -4,12 +4,16 @@ import Prelude
4
4
5
5
import Data.Argonaut.Core (Json , isNull , caseJsonNull , caseJsonBoolean , caseJsonNumber , caseJsonString , toArray , toObject , toString , stringify )
6
6
import Data.Array as Arr
7
+ import Data.Array.NonEmpty (NonEmptyArray )
8
+ import Data.Array.NonEmpty as NEA
7
9
import Data.Bifunctor (lmap , rmap )
8
10
import Data.Either (Either (..), note )
9
11
import Data.Identity (Identity (..))
10
12
import Data.Int (fromNumber )
11
13
import Data.List (List (..), (:), fromFoldable )
12
14
import Data.List as L
15
+ import Data.List.NonEmpty (NonEmptyList )
16
+ import Data.List.NonEmpty as NEL
13
17
import Data.Map as M
14
18
import Data.Maybe (maybe , Maybe (..))
15
19
import Data.NonEmpty (NonEmpty , (:|))
@@ -63,49 +67,59 @@ instance decodeJsonNumber :: DecodeJson Number where
63
67
decodeJson = caseJsonNumber (Left " Value is not a Number" ) Right
64
68
65
69
instance decodeJsonInt :: DecodeJson Int where
66
- decodeJson
67
- = maybe (Left " Value is not an integer" ) Right
68
- <<< fromNumber
69
- <=< decodeJson
70
+ decodeJson =
71
+ maybe (Left " Value is not an integer" ) Right
72
+ <<< fromNumber
73
+ <=< decodeJson
70
74
71
75
instance decodeJsonString :: DecodeJson String where
72
76
decodeJson = caseJsonString (Left " Value is not a String" ) Right
73
77
74
78
instance decodeJsonJson :: DecodeJson Json where
75
79
decodeJson = Right
76
80
77
- instance decodeJsonNonEmptyArray :: (DecodeJson a ) => DecodeJson (NonEmpty Array a ) where
78
- decodeJson
79
- = lmap (" Couldn't decode NonEmpty Array: " <> _)
80
- <<< (traverse decodeJson <=< (lmap (" JSON Array" <> _) <<< rmap (\x -> x.head :| x.tail) <<< note " is empty" <<< Arr .uncons) <=< decodeJArray)
81
+ instance decodeJsonNonEmpty_Array :: (DecodeJson a ) => DecodeJson (NonEmpty Array a ) where
82
+ decodeJson =
83
+ lmap (" Couldn't decode NonEmpty Array: " <> _)
84
+ <<< (traverse decodeJson <=< (lmap (" JSON Array" <> _) <<< rmap (\x -> x.head :| x.tail) <<< note " is empty" <<< Arr .uncons) <=< decodeJArray)
81
85
82
- instance decodeJsonNonEmptyList :: (DecodeJson a ) => DecodeJson (NonEmpty List a ) where
83
- decodeJson
84
- = lmap (" Couldn't decode NonEmpty List: " <> _)
85
- <<< (traverse decodeJson <=< (lmap (" JSON Array" <> _) <<< rmap (\x -> x.head :| x.tail) <<< note " is empty" <<< L .uncons) <=< map (map fromFoldable) decodeJArray)
86
+ instance decodeJsonNonEmptyArray :: (DecodeJson a ) => DecodeJson (NonEmptyArray a ) where
87
+ decodeJson =
88
+ lmap (" Couldn't decode NonEmptyArray: " <> _)
89
+ <<< (traverse decodeJson <=< (lmap (" JSON Array" <> _) <<< rmap (\x -> NEA .cons' x.head x.tail) <<< note " is empty" <<< Arr .uncons) <=< decodeJArray)
90
+
91
+ instance decodeJsonNonEmpty_List :: (DecodeJson a ) => DecodeJson (NonEmpty List a ) where
92
+ decodeJson =
93
+ lmap (" Couldn't decode NonEmpty List: " <> _)
94
+ <<< (traverse decodeJson <=< (lmap (" JSON Array" <> _) <<< rmap (\x -> x.head :| x.tail) <<< note " is empty" <<< L .uncons) <=< map (map fromFoldable) decodeJArray)
95
+
96
+ instance decodeJsonNonEmptyList :: (DecodeJson a ) => DecodeJson (NonEmptyList a ) where
97
+ decodeJson =
98
+ lmap (" Couldn't decode NonEmptyList: " <> _)
99
+ <<< (traverse decodeJson <=< (lmap (" JSON Array" <> _) <<< rmap (\x -> NEL .cons' x.head x.tail) <<< note " is empty" <<< L .uncons) <=< map (map fromFoldable) decodeJArray)
86
100
87
101
instance decodeJsonChar :: DecodeJson CodePoint where
88
102
decodeJson j =
89
103
maybe (Left $ " Expected character but found: " <> stringify j) Right
90
104
=<< codePointAt 0 <$> decodeJson j
91
105
92
106
instance decodeForeignObject :: DecodeJson a => DecodeJson (FO.Object a ) where
93
- decodeJson
94
- = lmap (" Couldn't decode ForeignObject: " <> _)
95
- <<< (traverse decodeJson <=< decodeJObject)
107
+ decodeJson =
108
+ lmap (" Couldn't decode ForeignObject: " <> _)
109
+ <<< (traverse decodeJson <=< decodeJObject)
96
110
97
111
instance decodeArray :: DecodeJson a => DecodeJson (Array a ) where
98
- decodeJson
99
- = lmap (" Couldn't decode Array (" <> _)
100
- <<< (traverseWithIndex f <=< decodeJArray)
112
+ decodeJson =
113
+ lmap (" Couldn't decode Array (" <> _)
114
+ <<< (traverseWithIndex f <=< decodeJArray)
101
115
where
102
- msg i m = " Failed at index " <> show i <> " ): " <> m
103
- f i = lmap (msg i) <<< decodeJson
116
+ msg i m = " Failed at index " <> show i <> " ): " <> m
117
+ f i = lmap (msg i) <<< decodeJson
104
118
105
119
instance decodeList :: DecodeJson a => DecodeJson (List a ) where
106
- decodeJson
107
- = lmap (" Couldn't decode List: " <> _)
108
- <<< (traverse decodeJson <=< map (map fromFoldable) decodeJArray)
120
+ decodeJson =
121
+ lmap (" Couldn't decode List: " <> _)
122
+ <<< (traverse decodeJson <=< map (map fromFoldable) decodeJArray)
109
123
110
124
instance decodeSet :: (Ord a , DecodeJson a ) => DecodeJson (S.Set a ) where
111
125
decodeJson = map (S .fromFoldable :: List a -> S.Set a ) <<< decodeJson
@@ -127,7 +141,6 @@ instance decodeRecord
127
141
, RL.RowToList row list
128
142
)
129
143
=> DecodeJson (Record row ) where
130
-
131
144
decodeJson json =
132
145
case toObject json of
133
146
Just object -> gDecodeJson object (RLProxy :: RLProxy list )
@@ -147,12 +160,12 @@ instance gDecodeJsonCons
147
160
, Row.Lacks field rowTail
148
161
)
149
162
=> GDecodeJson row (RL.Cons field value tail ) where
150
-
151
163
gDecodeJson object _ = do
152
- let sProxy :: SProxy field
153
- sProxy = SProxy
164
+ let
165
+ sProxy :: SProxy field
166
+ sProxy = SProxy
154
167
155
- fieldName = reflectSymbol sProxy
168
+ fieldName = reflectSymbol sProxy
156
169
157
170
rest <- gDecodeJson object (RLProxy :: RLProxy tail )
158
171
0 commit comments