@@ -18,6 +18,10 @@ import Data.Either (Either(..))
18
18
import Data.Maybe (Maybe (..), fromMaybe , maybe )
19
19
import Foreign.Object as FO
20
20
21
+ -- | Attempt to get the value for a given key on an `Object Json`.
22
+ -- |
23
+ -- | Use this accessor if the key and value *must* be present in your object.
24
+ -- | If the key and value are optional, use `getFieldOptional'` (`.:?`) instead.
21
25
getField :: forall a . DecodeJson a => FO.Object Json -> String -> Either String a
22
26
getField o s =
23
27
maybe
@@ -27,17 +31,13 @@ getField o s =
27
31
28
32
infix 7 getField as .:
29
33
30
- getFieldOptional :: forall a . DecodeJson a => FO.Object Json -> String -> Either String (Maybe a )
31
- getFieldOptional o s =
32
- maybe
33
- (pure Nothing )
34
- decode
35
- (FO .lookup s o)
36
- where
37
- decode json = Just <$> (elaborateFailure s <<< decodeJson) json
38
-
39
- infix 7 getFieldOptional as .:!
40
-
34
+ -- | Attempt to get the value for a given key on an `Object Json`.
35
+ -- |
36
+ -- | The result will be `Right Nothing` if the key and value are not present,
37
+ -- | or if the key is present and the value is `null`.
38
+ -- |
39
+ -- | Use this accessor if the key and value are optional in your object.
40
+ -- | If the key and value are mandatory, use `getField` (`.:`) instead.
41
41
getFieldOptional' :: forall a . DecodeJson a => FO.Object Json -> String -> Either String (Maybe a )
42
42
getFieldOptional' o s =
43
43
maybe
@@ -52,6 +52,44 @@ getFieldOptional' o s =
52
52
53
53
infix 7 getFieldOptional' as .:?
54
54
55
+ -- | Attempt to get the value for a given key on an `Object Json`.
56
+ -- |
57
+ -- | The result will be `Right Nothing` if the key and value are not present,
58
+ -- | but will fail if the key is present but the value cannot be converted to the right type.
59
+ -- |
60
+ -- | This function will treat `null` as a value and attempt to decode it into your desired type.
61
+ -- | If you would like to treat `null` values the same as absent values, use
62
+ -- | `getFieldOptional` (`.:?`) instead.
63
+ getFieldOptional :: forall a . DecodeJson a => FO.Object Json -> String -> Either String (Maybe a )
64
+ getFieldOptional o s =
65
+ maybe
66
+ (pure Nothing )
67
+ decode
68
+ (FO .lookup s o)
69
+ where
70
+ decode json = Just <$> (elaborateFailure s <<< decodeJson) json
71
+
72
+ infix 7 getFieldOptional as .:!
73
+
74
+ -- | Helper for use in combination with `.:?` to provide default values for optional
75
+ -- | `Object Json` fields.
76
+ -- |
77
+ -- | Example usage:
78
+ -- | ```purescript
79
+ -- | newtype MyType = MyType
80
+ -- | { foo :: String
81
+ -- | , bar :: Maybe Int
82
+ -- | , baz :: Boolean
83
+ -- | }
84
+ -- |
85
+ -- | instance decodeJsonMyType :: DecodeJson MyType where
86
+ -- | decodeJson json = do
87
+ -- | x <- decodeJson json
88
+ -- | foo <- x .: "foo" -- mandatory field
89
+ -- | bar <- x .:? "bar" -- optional field
90
+ -- | baz <- x .:? "baz" .!= false -- optional field with default value of `false`
91
+ -- | pure $ MyType { foo, bar, baz }
92
+ -- | ```
55
93
defaultField :: forall a . Either String (Maybe a ) -> a -> Either String a
56
94
defaultField parser default = fromMaybe default <$> parser
57
95
0 commit comments