aeson-deriving: data types for compositional, type-directed serialization

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Please see the README on GitHub at https://github.com/fieldstrength/aeson-deriving#readme


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.0.0, 0.1.1, 0.1.1.1, 0.1.1.2
Change log ChangeLog.md
Dependencies aeson (>=1.2 && <1.5), base (>=4.7 && <5), regex-tdfa, text, unordered-containers [details]
License MIT
Copyright 2020
Author Cliff Harvey
Maintainer cs.hbar+hs@gmail.com
Category Serialization
Home page https://github.com/fieldstrength/aeson-deriving#readme
Bug tracker https://github.com/fieldstrength/aeson-deriving/issues
Source repo head: git clone https://github.com/fieldstrength/aeson-deriving
Uploaded by Cliff_Harvey at 2020-07-26T18:03:32Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for aeson-deriving-0.1.0.0

[back to package description]

aeson-deriving

Build Status Hackage

Define JSON encoding and decoding behavior in a unified way with DerivingVia. This ensures the instances for the two aeson type classes stay in sync and eliminates much needless boilerplate, besides supporting many extra features.

Uses and examples

Basic encoding options & common patterns

Aeson's generics support governs the basic mapping between Haskell definitions and the JSON format. This functionality, along with its tunable parameters, can be specified with the GenericEncoded newtype.

type MyEncoding = GenericEncoded
  '[ ConstructorTagModifier := SnakeCase  -- extensible function support
  , FieldLabelModifier :=
      [ SnakeCase, DropSuffix "_" ]       -- functions can be composed
  , SumEncoding := TaggedObject "type" "contents"
  ]


data User = User
  { firstName :: Text
  , id_       :: UserId        
  , companyId :: CompanyId
  }
  deriving stock (Generic, Show)
  deriving (FromJSON, ToJSON)
    via MyEncoding User

data Document = Document
  { name      :: Text
  , id_       :: Int64
  , companyId :: CompanyId
  , parts     :: [SubDocument]
  }
  deriving stock (Generic, Show)
  deriving (FromJSON, ToJSON)
    via MyEncoding Document

-- >>> encode (User "jake" 1 29)
-- { "type": "user", "first_name": "jake", "id": 1, "company_id": 29}

Modifier newtypes

Constrant Fields

data Transaction = Transaction
  { transactionId :: UUID }
  deriving stock (Generic, Show)
  deriving (FromJSON, ToJSON) via
    WithConstantFieldsOut
      '[ "version" := "1.0"
      , "system_info" := "👍" ]
        MyEncoding Transaction

Note: Some newtypes that modify the instances come in an inbound and outbound variant. For example WithConstantFields is defined as the composition of WithConstantFieldsIn and WithConstantFieldsOut.

Apply arbitrary functions before encoding/decoding

Example: Special treatment for magic values

data Feedback = Feedback
  { comment :: Text }
  deriving stock (Generic, Show)
  deriving (FromJSON, ToJSON) via
    ModifyFieldIn "comment"
      ("booo!" ==> "boo-urns!")
        MyEncoding Feedback


-- x ==> y  maps the value x to y and leaves others unchanged
-- Implement your own instances of `KnownJSONFunction` for other behavior

Preventing infinite loops

Newtypes that modify an inner type class instance must be careful not to do so in an infinitely recursive way. Here the inner type should use the generic-based instance, rather than reference the instance being defined.

This package employs a custom compiler error to prevent this very easy mistake.

Improved error messages for sums of records

See RecordSumEncoded documentation.

To be expanded...