Last active
November 17, 2020 02:14
-
-
Save evanrelf/e1f922bc82627177d385031a5116195e to your computer and use it in GitHub Desktop.
Invented by Matt Parsons (https://www.parsonsmatt.org/2020/02/04/mirror_mirror.html), implemented in `deriving-aeson` (https://hackage.haskell.org/package/deriving-aeson)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE DeriveGeneric #-} | |
{-# LANGUAGE DerivingVia #-} | |
{-# LANGUAGE InstanceSigs #-} | |
{-# LANGUAGE KindSignatures #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE TypeApplications #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
module Data.Aeson.StripPrefix (StripPrefix (..)) where | |
import Data.Proxy (Proxy (..)) | |
import GHC.Generics (Generic) | |
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal) | |
import Numeric.Natural (Natural) | |
import qualified Data.Aeson as Aeson | |
import qualified Data.Aeson.Types as Aeson | |
import qualified Data.Char as Char | |
import qualified Data.List as List | |
import qualified GHC.Generics as Generics | |
newtype StripPrefix (prefix :: Symbol) a = StripPrefix { unStripPrefix :: a } | |
deriving stock Generic | |
stripPrefix :: String -> String -> String | |
stripPrefix prefix string = | |
case List.stripPrefix prefix string of | |
Just (x : xs) -> Char.toLower x : xs | |
_ -> string | |
instance | |
( KnownSymbol prefix | |
, Generic a | |
, Aeson.GFromJSON Aeson.Zero (Generics.Rep a) | |
) => Aeson.FromJSON (StripPrefix prefix a) where | |
parseJSON :: Aeson.Value -> Aeson.Parser (StripPrefix prefix a) | |
parseJSON = fmap StripPrefix . Aeson.genericParseJSON options | |
where | |
options = Aeson.defaultOptions | |
{ Aeson.fieldLabelModifier = stripPrefix (symbolVal (Proxy @prefix)) } | |
instance | |
( KnownSymbol prefix | |
, Generic a | |
, Aeson.GToJSON Aeson.Zero (Generics.Rep a) | |
) => Aeson.ToJSON (StripPrefix prefix a) where | |
toJSON :: StripPrefix prefix a -> Aeson.Value | |
toJSON = Aeson.genericToJSON options . unStripPrefix | |
where | |
options = Aeson.defaultOptions | |
{ Aeson.fieldLabelModifier = stripPrefix (symbolVal (Proxy @prefix)) } | |
--------------------------------------------------------------------------------- | |
-- EXAMPLE | |
--------------------------------------------------------------------------------- | |
data Person = Person | |
{ personName :: String | |
, personAge :: Natural | |
} deriving stock (Generic, Show) | |
deriving (Aeson.FromJSON, Aeson.ToJSON) via StripPrefix "person" Person | |
-- nix-shell --packages 'haskellPackages.ghcWithPackages (p: with p; [ aeson ])' | |
-- ghcid --allow-eval encoding-via.hs | |
-- | |
-- $> :set -XOverloadedStrings -XTypeApplications | |
-- | |
-- $> Aeson.encode (Person "Evan" 23) | |
-- "{\"age\":23,\"name\":\"Evan\"}" | |
-- | |
-- $> Aeson.decode @Person "{\"age\":23,\"name\":\"Evan\"}" | |
-- Just (Person {personName = "Evan", personAge = 23}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment