Skip to content

Commit 976ff9c

Browse files
committed
Improve some docs/code examples in the main module
1 parent e5c9e10 commit 976ff9c

File tree

1 file changed

+69
-20
lines changed

1 file changed

+69
-20
lines changed

src/Data/Codec/Argonaut.purs

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,15 @@ jarray = jsonPrimCodec "Array" J.toArray J.fromArray
137137
jobject JsonCodec (FO.Object J.Json)
138138
jobject = jsonPrimCodec "Object" J.toObject J.fromObject
139139

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+
-- | ```
146149
array a. JsonCodec a JsonCodec (Array a)
147150
array codec = GCodec dec enc
148151
where
@@ -162,16 +165,20 @@ type JIndexedCodec a =
162165

163166
-- | A codec for types that are encoded as an array with a specific layout.
164167
-- |
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:
167170
-- |
168171
-- | ```purescript
172+
-- | import Data.Codec ((~))
173+
-- | import Data.Codec.Argonaut as CA
174+
-- |
169175
-- | type Person = { name ∷ String, age ∷ Int }
170176
-- |
171-
-- | JA.indexedArray "Test Object" $
177+
-- | codecPerson ∷ CA.JsonCodec Person
178+
-- | codecPerson = CA.indexedArray "Test Object" $
172179
-- | { 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
175182
-- | ```
176183
indexedArray a. String JIndexedCodec a JsonCodec a
177184
indexedArray name =
@@ -198,6 +205,9 @@ type JPropCodec a =
198205
a a
199206

200207
-- | 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.
201211
object a. String JPropCodec a JsonCodec a
202212
object name =
203213
bihoistGCodec
@@ -220,13 +230,23 @@ prop key codec = GCodec dec enc
220230
-- | provides a convenient method for defining codecs for record types that
221231
-- | encode into JSON objects of the same shape.
222232
-- |
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:
224235
-- | ```
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
229246
-- | ```
247+
-- |
248+
-- | See also `Data.Codec.Argonaut.Record.object` for a more commonly useful
249+
-- | version of this function.
230250
record JPropCodec {}
231251
record = GCodec (pure {}) (Star \val → writer (Tuple val L.Nil))
232252

@@ -271,7 +291,27 @@ jsonPrimCodec
271291
jsonPrimCodec ty f =
272292
basicCodec (maybe (Left (TypeMismatch ty)) pure <<< f)
273293

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+
-- | ```
275315
fix a. (JsonCodec a JsonCodec a) JsonCodec a
276316
fix f =
277317
basicCodec
@@ -285,9 +325,15 @@ fix f =
285325
-- | This function is named as such as the pair of functions it accepts
286326
-- | correspond with the `preview` and `view` functions of a `Prism`-style lens.
287327
-- |
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:
291337
-- |
292338
-- | ```purescript
293339
-- | data Direction = North | South | West | East
@@ -308,6 +354,9 @@ fix f =
308354
-- | West -> "W"
309355
-- | East -> "E"
310356
-- | ```
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`.
311360
prismaticCodec a b. (a Maybe b) (b a) JsonCodec a JsonCodec b
312361
prismaticCodec f g orig =
313362
basicCodec

0 commit comments

Comments
 (0)