@@ -15,6 +15,8 @@ import Data.List (List, fromFoldable)
15
15
import Data.List as L
16
16
import Data.List.NonEmpty (NonEmptyList )
17
17
import Data.List.NonEmpty as NEL
18
+ import Data.String.NonEmpty (NonEmptyString )
19
+ import Data.String.NonEmpty as NonEmptyString
18
20
import Data.Map as M
19
21
import Data.Maybe (maybe , Maybe (..))
20
22
import Data.NonEmpty (NonEmpty , (:|))
@@ -25,14 +27,14 @@ import Data.TraversableWithIndex (traverseWithIndex)
25
27
import Data.Tuple (Tuple (..))
26
28
import Foreign.Object as FO
27
29
28
- decodeIdentity
30
+ decodeIdentity
29
31
:: forall a
30
32
. (Json -> Either JsonDecodeError a )
31
33
-> Json
32
34
-> Either JsonDecodeError (Identity a )
33
35
decodeIdentity decoder json = Identity <$> decoder json
34
36
35
- decodeMaybe
37
+ decodeMaybe
36
38
:: forall a
37
39
. (Json -> Either JsonDecodeError a )
38
40
-> Json
@@ -41,26 +43,26 @@ decodeMaybe decoder json
41
43
| isNull json = pure Nothing
42
44
| otherwise = Just <$> decoder json
43
45
44
- decodeTuple
46
+ decodeTuple
45
47
:: forall a b
46
- . (Json -> Either JsonDecodeError a )
48
+ . (Json -> Either JsonDecodeError a )
47
49
-> (Json -> Either JsonDecodeError b )
48
- -> Json
50
+ -> Json
49
51
-> Either JsonDecodeError (Tuple a b )
50
52
decodeTuple decoderA decoderB json = decodeArray Right json >>= f
51
53
where
52
54
f :: Array Json -> Either JsonDecodeError (Tuple a b )
53
- f = case _ of
55
+ f = case _ of
54
56
[a, b] -> Tuple <$> decoderA a <*> decoderB b
55
57
_ -> Left $ TypeMismatch " Tuple"
56
58
57
- decodeEither
59
+ decodeEither
58
60
:: forall a b
59
61
. (Json -> Either JsonDecodeError a )
60
62
-> (Json -> Either JsonDecodeError b )
61
63
-> Json
62
64
-> Either JsonDecodeError (Either a b )
63
- decodeEither decoderA decoderB json =
65
+ decodeEither decoderA decoderB json =
64
66
lmap (Named " Either" ) $ decodeJObject json >>= \obj -> do
65
67
tag <- note (AtKey " tag" MissingValue ) $ FO .lookup " tag" obj
66
68
val <- note (AtKey " value" MissingValue ) $ FO .lookup " value" obj
@@ -84,61 +86,66 @@ decodeInt = note (TypeMismatch "Integer") <<< fromNumber <=< decodeNumber
84
86
decodeString :: Json -> Either JsonDecodeError String
85
87
decodeString = caseJsonString (Left $ TypeMismatch " String" ) Right
86
88
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
88
95
:: forall a
89
96
. (Json -> Either JsonDecodeError a )
90
97
-> Json
91
98
-> 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" )
97
104
<<< Arr .uncons
98
105
<=< decodeJArray
99
106
100
- decodeNonEmptyArray
107
+ decodeNonEmptyArray
101
108
:: forall a
102
109
. (Json -> Either JsonDecodeError a )
103
- -> Json
110
+ -> Json
104
111
-> Either JsonDecodeError (NonEmptyArray a )
105
112
decodeNonEmptyArray decoder =
106
113
lmap (Named " NonEmptyArray" )
107
- <<< traverse decoder
114
+ <<< traverse decoder
108
115
<=< map (\x -> NEA .cons' x.head x.tail)
109
- <<< note (TypeMismatch " NonEmptyArray" )
116
+ <<< note (TypeMismatch " NonEmptyArray" )
110
117
<<< Arr .uncons
111
118
<=< decodeJArray
112
119
113
- decodeNonEmpty_List
120
+ decodeNonEmpty_List
114
121
:: forall a
115
122
. (Json -> Either JsonDecodeError a )
116
123
-> Json
117
124
-> Either JsonDecodeError (NonEmpty List a )
118
125
decodeNonEmpty_List decoder =
119
126
lmap (Named " NonEmpty List" )
120
- <<< traverse decoder
127
+ <<< traverse decoder
121
128
<=< map (\x -> x.head :| x.tail)
122
129
<<< note (TypeMismatch " NonEmpty List" )
123
130
<<< L .uncons
124
131
<=< map (map fromFoldable) decodeJArray
125
132
126
- decodeNonEmptyList
133
+ decodeNonEmptyList
127
134
:: forall a
128
135
. (Json -> Either JsonDecodeError a )
129
136
-> Json
130
137
-> Either JsonDecodeError (NonEmptyList a )
131
138
decodeNonEmptyList decoder =
132
139
lmap (Named " NonEmptyList" )
133
- <<< traverse decoder
140
+ <<< traverse decoder
134
141
<=< map (\x -> NEL .cons' x.head x.tail)
135
142
<<< note (TypeMismatch " NonEmptyList" )
136
143
<<< L .uncons
137
144
<=< map (map fromFoldable) decodeJArray
138
145
139
146
decodeCodePoint :: Json -> Either JsonDecodeError CodePoint
140
- decodeCodePoint json =
141
- note (Named " CodePoint" $ UnexpectedValue json)
147
+ decodeCodePoint json =
148
+ note (Named " CodePoint" $ UnexpectedValue json)
142
149
=<< map (codePointAt 0 ) (decodeString json)
143
150
144
151
decodeForeignObject
@@ -147,47 +154,47 @@ decodeForeignObject
147
154
-> Json
148
155
-> Either JsonDecodeError (FO.Object a )
149
156
decodeForeignObject decoder =
150
- lmap (Named " ForeignObject" )
151
- <<< traverse decoder
157
+ lmap (Named " ForeignObject" )
158
+ <<< traverse decoder
152
159
<=< decodeJObject
153
160
154
- decodeArray
161
+ decodeArray
155
162
:: forall a
156
163
. (Json -> Either JsonDecodeError a )
157
164
-> Json
158
165
-> Either JsonDecodeError (Array a )
159
166
decodeArray decoder =
160
167
lmap (Named " Array" )
161
- <<< traverseWithIndex (\i -> lmap (AtIndex i) <<< decoder)
168
+ <<< traverseWithIndex (\i -> lmap (AtIndex i) <<< decoder)
162
169
<=< decodeJArray
163
170
164
- decodeList
171
+ decodeList
165
172
:: forall a
166
173
. (Json -> Either JsonDecodeError a )
167
174
-> Json
168
175
-> Either JsonDecodeError (List a )
169
176
decodeList decoder =
170
177
lmap (Named " List" )
171
- <<< traverse decoder
178
+ <<< traverse decoder
172
179
<=< map (map fromFoldable) decodeJArray
173
180
174
- decodeSet
181
+ decodeSet
175
182
:: forall a
176
- . Ord a
183
+ . Ord a
177
184
=> (Json -> Either JsonDecodeError a )
178
185
-> Json
179
186
-> Either JsonDecodeError (S.Set a )
180
- decodeSet decoder =
187
+ decodeSet decoder =
181
188
map (S .fromFoldable :: List a -> S.Set a ) <<< decodeList decoder
182
189
183
- decodeMap
190
+ decodeMap
184
191
:: forall a b
185
- . Ord a
192
+ . Ord a
186
193
=> (Json -> Either JsonDecodeError a )
187
194
-> (Json -> Either JsonDecodeError b )
188
195
-> Json
189
196
-> Either JsonDecodeError (M.Map a b )
190
- decodeMap decoderA decoderB =
197
+ decodeMap decoderA decoderB =
191
198
map (M .fromFoldable :: List (Tuple a b ) -> M.Map a b )
192
199
<<< decodeList (decodeTuple decoderA decoderB)
193
200
@@ -200,7 +207,7 @@ decodeJArray = note (TypeMismatch "Array") <<< toArray
200
207
decodeJObject :: Json -> Either JsonDecodeError (FO.Object Json )
201
208
decodeJObject = note (TypeMismatch " Object" ) <<< toObject
202
209
203
- getField
210
+ getField
204
211
:: forall a
205
212
. (Json -> Either JsonDecodeError a )
206
213
-> FO.Object Json
@@ -212,7 +219,7 @@ getField decoder obj str =
212
219
(lmap (AtKey str) <<< decoder)
213
220
(FO .lookup str obj)
214
221
215
- getFieldOptional
222
+ getFieldOptional
216
223
:: forall a
217
224
. (Json -> Either JsonDecodeError a )
218
225
-> FO.Object Json
@@ -223,7 +230,7 @@ getFieldOptional decoder obj str =
223
230
where
224
231
decode = lmap (AtKey str) <<< decoder
225
232
226
- getFieldOptional'
233
+ getFieldOptional'
227
234
:: forall a
228
235
. (Json -> Either JsonDecodeError a )
229
236
-> FO.Object Json
@@ -232,8 +239,8 @@ getFieldOptional'
232
239
getFieldOptional' decoder obj str =
233
240
maybe (pure Nothing ) decode (FO .lookup str obj)
234
241
where
235
- decode json =
236
- if isNull json then
242
+ decode json =
243
+ if isNull json then
237
244
pure Nothing
238
- else
245
+ else
239
246
Just <$> (lmap (AtKey str) <<< decoder) json
0 commit comments