@@ -15,6 +15,8 @@ import Data.List (List, fromFoldable)
1515import Data.List as L
1616import Data.List.NonEmpty (NonEmptyList )
1717import Data.List.NonEmpty as NEL
18+ import Data.String.NonEmpty (NonEmptyString )
19+ import Data.String.NonEmpty as NonEmptyString
1820import Data.Map as M
1921import Data.Maybe (maybe , Maybe (..))
2022import Data.NonEmpty (NonEmpty , (:|))
@@ -25,14 +27,14 @@ import Data.TraversableWithIndex (traverseWithIndex)
2527import Data.Tuple (Tuple (..))
2628import Foreign.Object as FO
2729
28- decodeIdentity
30+ decodeIdentity
2931 :: forall a
3032 . (Json -> Either JsonDecodeError a )
3133 -> Json
3234 -> Either JsonDecodeError (Identity a )
3335decodeIdentity decoder json = Identity <$> decoder json
3436
35- decodeMaybe
37+ decodeMaybe
3638 :: forall a
3739 . (Json -> Either JsonDecodeError a )
3840 -> Json
@@ -41,26 +43,26 @@ decodeMaybe decoder json
4143 | isNull json = pure Nothing
4244 | otherwise = Just <$> decoder json
4345
44- decodeTuple
46+ decodeTuple
4547 :: forall a b
46- . (Json -> Either JsonDecodeError a )
48+ . (Json -> Either JsonDecodeError a )
4749 -> (Json -> Either JsonDecodeError b )
48- -> Json
50+ -> Json
4951 -> Either JsonDecodeError (Tuple a b )
5052decodeTuple decoderA decoderB json = decodeArray Right json >>= f
5153 where
5254 f :: Array Json -> Either JsonDecodeError (Tuple a b )
53- f = case _ of
55+ f = case _ of
5456 [a, b] -> Tuple <$> decoderA a <*> decoderB b
5557 _ -> Left $ TypeMismatch " Tuple"
5658
57- decodeEither
59+ decodeEither
5860 :: forall a b
5961 . (Json -> Either JsonDecodeError a )
6062 -> (Json -> Either JsonDecodeError b )
6163 -> Json
6264 -> Either JsonDecodeError (Either a b )
63- decodeEither decoderA decoderB json =
65+ decodeEither decoderA decoderB json =
6466 lmap (Named " Either" ) $ decodeJObject json >>= \obj -> do
6567 tag <- note (AtKey " tag" MissingValue ) $ FO .lookup " tag" obj
6668 val <- note (AtKey " value" MissingValue ) $ FO .lookup " value" obj
@@ -84,61 +86,66 @@ decodeInt = note (TypeMismatch "Integer") <<< fromNumber <=< decodeNumber
8486decodeString :: Json -> Either JsonDecodeError String
8587decodeString = caseJsonString (Left $ TypeMismatch " String" ) Right
8688
87- decodeNonEmpty_Array
89+ decodeNonEmptyString :: Json -> Either JsonDecodeError NonEmptyString
90+ decodeNonEmptyString json =
91+ note (Named " NonEmptyString" $ UnexpectedValue json)
92+ =<< map (NonEmptyString .fromString) (decodeString json)
93+
94+ decodeNonEmpty_Array
8895 :: forall a
8996 . (Json -> Either JsonDecodeError a )
9097 -> Json
9198 -> Either JsonDecodeError (NonEmpty Array a )
92- decodeNonEmpty_Array decoder =
93- lmap (Named " NonEmpty Array" )
94- <<< traverse decoder
95- <=< map (\x -> x.head :| x.tail)
96- <<< note (TypeMismatch " NonEmpty Array" )
99+ decodeNonEmpty_Array decoder =
100+ lmap (Named " NonEmpty Array" )
101+ <<< traverse decoder
102+ <=< map (\x -> x.head :| x.tail)
103+ <<< note (TypeMismatch " NonEmpty Array" )
97104 <<< Arr .uncons
98105 <=< decodeJArray
99106
100- decodeNonEmptyArray
107+ decodeNonEmptyArray
101108 :: forall a
102109 . (Json -> Either JsonDecodeError a )
103- -> Json
110+ -> Json
104111 -> Either JsonDecodeError (NonEmptyArray a )
105112decodeNonEmptyArray decoder =
106113 lmap (Named " NonEmptyArray" )
107- <<< traverse decoder
114+ <<< traverse decoder
108115 <=< map (\x -> NEA .cons' x.head x.tail)
109- <<< note (TypeMismatch " NonEmptyArray" )
116+ <<< note (TypeMismatch " NonEmptyArray" )
110117 <<< Arr .uncons
111118 <=< decodeJArray
112119
113- decodeNonEmpty_List
120+ decodeNonEmpty_List
114121 :: forall a
115122 . (Json -> Either JsonDecodeError a )
116123 -> Json
117124 -> Either JsonDecodeError (NonEmpty List a )
118125decodeNonEmpty_List decoder =
119126 lmap (Named " NonEmpty List" )
120- <<< traverse decoder
127+ <<< traverse decoder
121128 <=< map (\x -> x.head :| x.tail)
122129 <<< note (TypeMismatch " NonEmpty List" )
123130 <<< L .uncons
124131 <=< map (map fromFoldable) decodeJArray
125132
126- decodeNonEmptyList
133+ decodeNonEmptyList
127134 :: forall a
128135 . (Json -> Either JsonDecodeError a )
129136 -> Json
130137 -> Either JsonDecodeError (NonEmptyList a )
131138decodeNonEmptyList decoder =
132139 lmap (Named " NonEmptyList" )
133- <<< traverse decoder
140+ <<< traverse decoder
134141 <=< map (\x -> NEL .cons' x.head x.tail)
135142 <<< note (TypeMismatch " NonEmptyList" )
136143 <<< L .uncons
137144 <=< map (map fromFoldable) decodeJArray
138145
139146decodeCodePoint :: Json -> Either JsonDecodeError CodePoint
140- decodeCodePoint json =
141- note (Named " CodePoint" $ UnexpectedValue json)
147+ decodeCodePoint json =
148+ note (Named " CodePoint" $ UnexpectedValue json)
142149 =<< map (codePointAt 0 ) (decodeString json)
143150
144151decodeForeignObject
@@ -147,47 +154,47 @@ decodeForeignObject
147154 -> Json
148155 -> Either JsonDecodeError (FO.Object a )
149156decodeForeignObject decoder =
150- lmap (Named " ForeignObject" )
151- <<< traverse decoder
157+ lmap (Named " ForeignObject" )
158+ <<< traverse decoder
152159 <=< decodeJObject
153160
154- decodeArray
161+ decodeArray
155162 :: forall a
156163 . (Json -> Either JsonDecodeError a )
157164 -> Json
158165 -> Either JsonDecodeError (Array a )
159166decodeArray decoder =
160167 lmap (Named " Array" )
161- <<< traverseWithIndex (\i -> lmap (AtIndex i) <<< decoder)
168+ <<< traverseWithIndex (\i -> lmap (AtIndex i) <<< decoder)
162169 <=< decodeJArray
163170
164- decodeList
171+ decodeList
165172 :: forall a
166173 . (Json -> Either JsonDecodeError a )
167174 -> Json
168175 -> Either JsonDecodeError (List a )
169176decodeList decoder =
170177 lmap (Named " List" )
171- <<< traverse decoder
178+ <<< traverse decoder
172179 <=< map (map fromFoldable) decodeJArray
173180
174- decodeSet
181+ decodeSet
175182 :: forall a
176- . Ord a
183+ . Ord a
177184 => (Json -> Either JsonDecodeError a )
178185 -> Json
179186 -> Either JsonDecodeError (S.Set a )
180- decodeSet decoder =
187+ decodeSet decoder =
181188 map (S .fromFoldable :: List a -> S.Set a ) <<< decodeList decoder
182189
183- decodeMap
190+ decodeMap
184191 :: forall a b
185- . Ord a
192+ . Ord a
186193 => (Json -> Either JsonDecodeError a )
187194 -> (Json -> Either JsonDecodeError b )
188195 -> Json
189196 -> Either JsonDecodeError (M.Map a b )
190- decodeMap decoderA decoderB =
197+ decodeMap decoderA decoderB =
191198 map (M .fromFoldable :: List (Tuple a b ) -> M.Map a b )
192199 <<< decodeList (decodeTuple decoderA decoderB)
193200
@@ -200,7 +207,7 @@ decodeJArray = note (TypeMismatch "Array") <<< toArray
200207decodeJObject :: Json -> Either JsonDecodeError (FO.Object Json )
201208decodeJObject = note (TypeMismatch " Object" ) <<< toObject
202209
203- getField
210+ getField
204211 :: forall a
205212 . (Json -> Either JsonDecodeError a )
206213 -> FO.Object Json
@@ -212,7 +219,7 @@ getField decoder obj str =
212219 (lmap (AtKey str) <<< decoder)
213220 (FO .lookup str obj)
214221
215- getFieldOptional
222+ getFieldOptional
216223 :: forall a
217224 . (Json -> Either JsonDecodeError a )
218225 -> FO.Object Json
@@ -223,7 +230,7 @@ getFieldOptional decoder obj str =
223230 where
224231 decode = lmap (AtKey str) <<< decoder
225232
226- getFieldOptional'
233+ getFieldOptional'
227234 :: forall a
228235 . (Json -> Either JsonDecodeError a )
229236 -> FO.Object Json
@@ -232,8 +239,8 @@ getFieldOptional'
232239getFieldOptional' decoder obj str =
233240 maybe (pure Nothing ) decode (FO .lookup str obj)
234241 where
235- decode json =
236- if isNull json then
242+ decode json =
243+ if isNull json then
237244 pure Nothing
238- else
245+ else
239246 Just <$> (lmap (AtKey str) <<< decoder) json
0 commit comments