https://github.com/jonathanjouty/aeson-possible
Three-valued types for use with aeson. Useful for use in PATCH endpoints.
https://github.com/jonathanjouty/aeson-possible
aeson
Last synced: over 1 year ago
JSON representation
Three-valued types for use with aeson. Useful for use in PATCH endpoints.
- Host: GitHub
- URL: https://github.com/jonathanjouty/aeson-possible
- Owner: jonathanjouty
- License: bsd-3-clause
- Created: 2023-12-19T19:30:52.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-23T13:40:14.000Z (over 2 years ago)
- Last Synced: 2024-04-25T23:02:21.070Z (about 2 years ago)
- Topics: aeson
- Language: Haskell
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# aeson-possible
[](https://github.com/jonathanjouty/aeson-possible/actions/workflows/ci-haskell.yml)
Three-valued possible types for use with `aeson`.
Useful for use in PATCH endpoints: use in records which have `ToJSON` and
`FromJSON` instances.
Inspired by the [`possible`](https://hackage.haskell.org/package/possible)
package, but additionally provides To/FromJSON instances using `aeson >= 2.2`'s
`omitField` and `omittedField` machinery.
## Usage
Use `Possible a` in your records
```hs
data MyRecord = MyRecord
{ myBool :: Possible Bool
, myInt :: Possible Int
, myStr :: Possible Text
}
deriving (Generic)
```
and then make sure to use the correct options if you are generically deriving
your To/FromJSON instances:
```hs
instance ToJSON MyRecord where
toJSON = genericToJSON $ defaultOptions{omitNothingFields = True}
instance FromJSON MyRecord where
parseJSON = genericParseJSON $ defaultOptions{allowOmittedFields = True}
```
Note that `omitNothingFields` affects `ToJSON`, and `allowOmittedFields`
affects `FromJSON`. You can, of course, also set both to `True`.
If you are creating instances any other way, see `aeson`'s documentation for
how to make use of `omitField` and `omittedField`.
_Caveat:_ if you are using `Possible` outside a record, even a `Missing` value
will likely be encoded as `null` (_e.g._ if you have a list of `Possible`
values).