Safe Haskell | None |
---|---|
Language | Haskell2010 |
A type for wrapping a single type in a record when encoding to and from JSON.
See SingleField
for this type.
Synopsis
- newtype SingleField (field :: Symbol) (a :: *) = SingleField {
- getSingleField :: a
Documentation
newtype SingleField (field :: Symbol) (a :: *) Source #
When interacting with or producing JSON, it is common to wrap a single field in
an object with a single field. This can be a bit awkward to use from Haskell - we
tend to write a single ToJSON
and FromJSON
instance for each type, making this
extra wrapping cumbersome.
This newtype wrapper can helper with this. SingleField field a
contains a single
value of a
. The difference is that the ToJSON
and FromJSON
instances will wrap
the a
in an object with a single field field
. field
is a type level string,
using data kinds.
>>>
encode (SingleField @"myField" 3)
"{\"myField\":3}"
>>>
getSingleField <$> decode @(SingleField "myField" Int) (encode $ SingleField @"myField" 123)
Just 123