@hackage / stock-aeson

Derive ToJSON / FromJSON via the stock plugin

Latest0.1.0.0

About

Metadata

  • Last updated , by BaldurBlondal
  • License BSD-3-Clause
  • Categories Type System
  • Maintained by: baldur.blondal@iohk.io

  • Lottery factor: 0

Links

Installation

Tested Compilers

  1. 9.10.3
  2. 9.8.1

Readme

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: