stock-aeson: Derive ToJSON / FromJSON via the stock plugin

[ bsd3, library, type-system ] [ Propose Tags ] [ Report a vulnerability ]

The stock plugin provides a newtype Stock for deriving and synthesising instances at compile time. stock-aeson extends it to support ToJSON, FromJSON, and their higher-kinded variants.

{-# options_ghc -fplugin Stock #-}

{-# language DerivingVia #-}

import Stock
import Stock.Aeson

data Person = P { name :: String, age :: Int }
  deriving (ToJSON, FromJSON) via
    Stock Person

The wire format reproduces aeson's genericToJSON / genericParseJSON defaultOptions. stock-aeson is a drop-in for deriving anyclass (ToJSON, FromJSON).

stock-aeson provides six instances, that signal to the plugin how to derive ToJSON, FromJSON.

instance DeriveStock  ToJSON    ..
instance DeriveStock1 ToJSON1   ..
instance DeriveStock2 ToJSON2   ..
instance DeriveStock  FromJSON  ..
instance DeriveStock1 FromJSON1 ..
instance DeriveStock2 FromJSON2 ..

The lifted variants derive the same zero-cost way (no Generic at runtime): parameter fields route through the supplied per-parameter encoders/parsers, so the bytes still match aeson's generic deriving.

data F a   = MkF a [a]   deriving (ToJSON1, FromJSON1) via Stock1 F
data P a b = MkP a b [b] deriving (ToJSON2, FromJSON2) via Stock2 P

stock companion packages include:

Modules

[Index] [Quick Jump]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Dependencies aeson (>=2 && <3), base (>=4.18 && <5), ghc (>=9.6 && <9.16), stock (>=0.1 && <0.2) [details]
Tested with ghc ==9.8.1, ghc ==9.10.3, ghc ==9.12.4, ghc ==9.14.1
License BSD-3-Clause
Author Baldur Blöndal
Maintainer baldur.blondal@iohk.io
Uploaded by BaldurBlondal at 2026-06-23T15:43:51Z
Revised Revision 5 made by BaldurBlondal at 2026-06-24T01:45:15Z
Category Type System
Source repo head: git clone https://github.com/Icelandjack/stock.git(aeson)
Distributions
Downloads 3 total (3 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2026-06-23 [all 1 reports]