@@ -137,12 +137,15 @@ jarray = jsonPrimCodec "Array" J.toArray J.fromArray
137
137
jobject ∷ JsonCodec (FO.Object J.Json )
138
138
jobject = jsonPrimCodec " Object" J .toObject J .fromObject
139
139
140
- -- | A codec for `Array` values.
141
- -- |```purescript
142
- -- | decodeIntArray ∷ Json → Either JsonDecodeError (Array Int)
143
- -- | decodeIntArray = decode (array int)
144
- -- |```
145
-
140
+ -- | A codec for arbitrary length `Array`s where every item in the array
141
+ -- | shares the same type.
142
+ -- |
143
+ -- | ``` purescript
144
+ -- | import Data.Codec.Argonaut as CA
145
+ -- |
146
+ -- | codecIntArray ∷ CA.JsonCodec (Array Int)
147
+ -- | codecIntArray = CA.array CA.int
148
+ -- | ```
146
149
array ∷ ∀ a . JsonCodec a → JsonCodec (Array a )
147
150
array codec = GCodec dec enc
148
151
where
@@ -162,16 +165,20 @@ type JIndexedCodec a =
162
165
163
166
-- | A codec for types that are encoded as an array with a specific layout.
164
167
-- |
165
- -- | For example, given that we'd like to encode a Person as a 2-element array,
166
- -- | like so `[ "Karl ", 25 ]`, we could write the following codec:
168
+ -- | For example, if we'd like to encode a ` Person` as a 2-element array, like
169
+ -- | `["Rashida ", 37 ]`, we could write the following codec:
167
170
-- |
168
171
-- | ```purescript
172
+ -- | import Data.Codec ((~))
173
+ -- | import Data.Codec.Argonaut as CA
174
+ -- |
169
175
-- | type Person = { name ∷ String, age ∷ Int }
170
176
-- |
171
- -- | JA.indexedArray "Test Object" $
177
+ -- | codecPerson ∷ CA.JsonCodec Person
178
+ -- | codecPerson = CA.indexedArray "Test Object" $
172
179
-- | { name: _, age: _ }
173
- -- | <$> _.name ~ index 0 JA .string
174
- -- | <*> _.age ~ index 1 JA .int
180
+ -- | <$> _.name ~ CA. index 0 CA .string
181
+ -- | <*> _.age ~ CA. index 1 CA .int
175
182
-- | ```
176
183
indexedArray ∷ ∀ a . String → JIndexedCodec a → JsonCodec a
177
184
indexedArray name =
@@ -198,6 +205,9 @@ type JPropCodec a =
198
205
a a
199
206
200
207
-- | A codec for objects that are encoded with specific properties.
208
+ -- |
209
+ -- | See also `Data.Codec.Argonaut.Record.object` for a more commonly useful
210
+ -- | version of this function.
201
211
object ∷ ∀ a . String → JPropCodec a → JsonCodec a
202
212
object name =
203
213
bihoistGCodec
@@ -220,13 +230,23 @@ prop key codec = GCodec dec enc
220
230
-- | provides a convenient method for defining codecs for record types that
221
231
-- | encode into JSON objects of the same shape.
222
232
-- |
223
- -- | For example:
233
+ -- | For example, to encode a record as the JSON object
234
+ -- | `{ "name": "Karl", "age": 25 }` we would define a codec like this:
224
235
-- | ```
225
- -- | myRecordCodec =
226
- -- | object "MyRecord" $ record
227
- -- | # recordProp (SProxy :: SProxy "tag") tagCodec
228
- -- | # recordProp (SProxy :: SProxy "value") valueCodec
236
+ -- | import Data.Codec.Argonaut as CA
237
+ -- | import Data.Symbol (SProxy(..))
238
+ -- |
239
+ -- | type Person = { name ∷ String, age ∷ Int }
240
+ -- |
241
+ -- | codecPerson ∷ CA.JsonCodec Person
242
+ -- | codecPerson =
243
+ -- | CA.object "Person" $ CA.record
244
+ -- | # CA.recordProp (SProxy :: _ "name") CA.string
245
+ -- | # CA.recordProp (SProxy :: _ "age") CA.int
229
246
-- | ```
247
+ -- |
248
+ -- | See also `Data.Codec.Argonaut.Record.object` for a more commonly useful
249
+ -- | version of this function.
230
250
record ∷ JPropCodec { }
231
251
record = GCodec (pure {}) (Star \val → writer (Tuple val L.Nil ))
232
252
@@ -271,7 +291,27 @@ jsonPrimCodec
271
291
jsonPrimCodec ty f =
272
292
basicCodec (maybe (Left (TypeMismatch ty)) pure <<< f)
273
293
274
- -- | Helper function for defining recursive codecs.
294
+ -- | Helper function for defining recursive codecs in situations where the codec
295
+ -- | definition causes a _"The value of <codec> is undefined here"_ error.
296
+ -- |
297
+ -- | ```purescript
298
+ -- | import Data.Codec.Argonaut as CA
299
+ -- | import Data.Codec.Argonaut.Common as CAC
300
+ -- | import Data.Codec.Argonaut.Record as CAR
301
+ -- | import Data.Maybe (Maybe)
302
+ -- | import Data.Newtype (class Newtype)
303
+ -- | import Data.Profunctor (wrapIso)
304
+ -- |
305
+ -- | newtype IntList = IntList { cell ∷ Int, rest ∷ Maybe IntList }
306
+ -- |
307
+ -- | derive instance newtypeLoopyList ∷ Newtype IntList _
308
+ -- |
309
+ -- | codecIntList ∷ CA.JsonCodec IntList
310
+ -- | codecIntList =
311
+ -- | CA.fix \codec →
312
+ -- | wrapIso IntList $
313
+ -- | CAR.object "IntList" { cell: CA.int, rest: CAC.maybe codec }
314
+ -- | ```
275
315
fix ∷ ∀ a . (JsonCodec a → JsonCodec a ) → JsonCodec a
276
316
fix f =
277
317
basicCodec
@@ -285,9 +325,15 @@ fix f =
285
325
-- | This function is named as such as the pair of functions it accepts
286
326
-- | correspond with the `preview` and `view` functions of a `Prism`-style lens.
287
327
-- |
288
- -- | For example, in order to parse a mapping from an enum to strings, which
289
- -- | doesn't match up nicely with `Data.Codec.Argonaut.Sum.enumSum` we can use
290
- -- | prismaticCodec:
328
+ -- | An example of this would be a codec for `Data.String.NonEmpty.NonEmptyString`:
329
+ -- |
330
+ -- | ```purescript
331
+ -- | nonEmptyString ∷ CA.JsonCodec NES.NonEmptyString
332
+ -- | nonEmptyString = CA.prismaticCodec NES.fromString NES.toString CA.string
333
+ -- | ```
334
+ -- |
335
+ -- | Another example might be to handle a mapping from a small sum type to
336
+ -- | strings:
291
337
-- |
292
338
-- | ```purescript
293
339
-- | data Direction = North | South | West | East
@@ -308,6 +354,9 @@ fix f =
308
354
-- | West -> "W"
309
355
-- | East -> "E"
310
356
-- | ```
357
+ -- |
358
+ -- | Although for this latter case there are some other options too, in the form
359
+ -- | of `Data.Codec.Argonaut.Generic.nullarySum` and `Data.Codec.Argonaut.Sum.enumSum`.
311
360
prismaticCodec ∷ ∀ a b . (a → Maybe b ) → (b → a ) → JsonCodec a → JsonCodec b
312
361
prismaticCodec f g orig =
313
362
basicCodec
0 commit comments