You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
39
39
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:
41
41
42
42
```purescript
43
43
import Control.Category ((>>>))
@@ -110,6 +110,33 @@ codec =
110
110
})
111
111
```
112
112
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
+
113
140
### Sum types and variants
114
141
115
142
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