Skip to content

Commit d124f5a

Browse files
committed
Update README.md
1 parent 5c729f5 commit d124f5a

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ decodeStringArray ∷ J.Json → Either CA.JsonDecodeError (Array String)
3535
decodeStringArray = CA.decode codec
3636
```
3737

38-
To parse a serialized `String` into a `J.Json` structure use the [`Parser.jsonParser`](https://pursuit.purescript.org/packages/purescript-argonaut-core/5.1.0/docs/Data.Argonaut.Parser).
38+
To parse a serialized `String` into a `J.Json` structure use the [`Parser.jsonParser`](https://pursuit.purescript.org/packages/purescript-argonaut-core/docs/Data.Argonaut.Parser).
3939

40-
To /"stringify"/ (serialize) your `Array String` to a serialized JSON `String` we would use the [`stringify`](https://pursuit.purescript.org/packages/purescript-argonaut-core/5.1.0/docs/Data.Argonaut.Core#v:stringify) like so:
40+
To "stringify" (serialize) your `Array String` to a serialized JSON `String` we would use the [`stringify`](https://pursuit.purescript.org/packages/purescript-argonaut-core/docs/Data.Argonaut.Core#v:stringify) like so:
4141

4242
```purescript
4343
import Control.Category ((>>>))
@@ -110,6 +110,33 @@ codec =
110110
})
111111
```
112112

113+
#### Optional properties
114+
115+
Objects with optional properties can be defined using the [`CAR.optional`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut.Record#v:optional):
116+
117+
```purescript
118+
type Person =
119+
{ name ∷ String
120+
, age ∷ Int
121+
, active ∷ Boolean
122+
, email ∷ Maybe String
123+
}
124+
125+
codec ∷ CA.JsonCodec Person
126+
codec =
127+
CA.object "Person"
128+
(CAR.record
129+
{ name: CA.string
130+
, age: CA.int
131+
, active: CA.boolean
132+
, email: CAR.optional CA.string
133+
})
134+
```
135+
136+
If the value being decoded has no `email` field, the resulting `Person` will have `Nothing` for `email` now rather than failing to decode. When encoding, if an optional value is `Nothing`, the field will be omitted from the resulting JSON object.
137+
138+
This combinator only deals with entirely missing properties, so values like `null` will still need to be handled explicitly.
139+
113140
### Sum types and variants
114141

115142
This library comes with codec support for [`purescript-variant`](https://github.com/natefaubion/purescript-variant) out of the box and codecs for sums are often based on the variant codec.

0 commit comments

Comments
 (0)