Aeson provides FromJSON1
and ToJSON1
type classes. These are similar to the Eq1
and Show1
classes defined in the Data.Functor.Classes
module.
My understanding of the Eq1
and Show1
classes is that they are needed to be able to express constraints on arguments of transformers without using extensions like FlexibleContexts
and UndecidableInstances
.
The example from the documentation in the Data.Functor.Classes
module is as follows:
Assume we have a data type that acts as a transformer: T
. For an example, let's have it be isomorphic to IdentityT
:
data T f a = T (f a)
The kind of T
is as follows:
T :: (* -> *) -> * -> *
If there is an Eq1
instance for f
, it is possible to use it when writing the Eq1
instance for T f
:
instance Eq1 f => Eq1 (T f) where
liftEq :: (a -> b -> Bool) -> T f a -> T f b -> Bool
liftEq eq (T fa1) (T fa2) = liftEq eq fa1 fa2
If we have an Eq1
instance for f
, an Eq
instance for a
, and the Eq1
instance for T f
above is in scope, we can easily write the Eq
instance for T f a
:
instance (Eq1 f, Eq a) => Eq (T f a) where
(==) :: T f a -> T f a -> Bool
(==) = eq1
The type of eq1
is defined as follows:
eq1 :: (Eq1 h, Eq a) => h a -> h a -> Bool
In our instance above, h
becomes T f
, so the type of eq1
can be thought of as the following:
eq1 :: Eq a => T f a -> T f a -> Bool
Now, the Eq1
, Show1
, etc classes make sense. It seems like it makes it easier to write instances of Eq
, Show
, etc for transformers.
However, I'm wondering what types FromJSON1
and ToJSON1
are used for in Aeson? I rarely have transformers that I want to turn to and from JSON.
Most of the data types I end up changing to JSON are normal types (not type constructors). That is to say, types with the kind *
. I also uses types like Maybe
with a kind of * -> *
.
However, I don't think I often create ToJSON
or FromJSON
instances for transformers, like the T
above. What is a transformer that is often used to go to and from JSON? Am I missing out on some helpful transformers?