aeson-possible: Possible values for aeson

[ bsd3, data, json, library, web ] [ Propose Tags ] [ Report a vulnerability ]

Three-valued possible types for use with aeson. Useful for use in PATCH endpoints.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1
Change log CHANGELOG.md
Dependencies aeson (>=2.2 && <2.3), base (>=4.17 && <5) [details]
Tested with ghc ==9.4 || ==9.6 || ==9.8
License BSD-3-Clause
Author Jonathan Jouty
Maintainer Jonathan Jouty <me@jonathanjouty.com>
Category Data, Web, JSON
Home page https://github.com/jonathanjouty/aeson-possible
Source repo head: git clone git@github.com:jonathanjouty/aeson-possible.git
Uploaded by jonathanjouty at 2024-06-27T14:25:27Z
Distributions
Downloads 105 total (11 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for aeson-possible-0.1.0.1

[back to package description]

aeson-possible

Build & Test

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 package, but additionally provides To/FromJSON instances using aeson >= 2.2's omitField and omittedField machinery.

Usage

Use Possible a in your records

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:

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).