aeson-single-field-0.1.0.0: Conveniently wrap a single value in a record when encoding to and from JSON
Safe HaskellNone
LanguageHaskell2010

Data.Aeson.SingleField

Description

A type for wrapping a single type in a record when encoding to and from JSON. See SingleField for this type.

Synopsis

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

Constructors

SingleField 

Fields

Instances

Instances details
Functor (SingleField field) Source # 
Instance details

Defined in Data.Aeson.SingleField

Methods

fmap :: (a -> b) -> SingleField field a -> SingleField field b #

(<$) :: a -> SingleField field b -> SingleField field a #

Eq a => Eq (SingleField field a) Source # 
Instance details

Defined in Data.Aeson.SingleField

Methods

(==) :: SingleField field a -> SingleField field a -> Bool #

(/=) :: SingleField field a -> SingleField field a -> Bool #

Show a => Show (SingleField field a) Source # 
Instance details

Defined in Data.Aeson.SingleField

Methods

showsPrec :: Int -> SingleField field a -> ShowS #

show :: SingleField field a -> String #

showList :: [SingleField field a] -> ShowS #

(ToJSON a, KnownSymbol field) => ToJSON (SingleField field a) Source # 
Instance details

Defined in Data.Aeson.SingleField

Methods

toJSON :: SingleField field a -> Value #

toEncoding :: SingleField field a -> Encoding #

toJSONList :: [SingleField field a] -> Value #

toEncodingList :: [SingleField field a] -> Encoding #

(FromJSON a, KnownSymbol field) => FromJSON (SingleField field a) Source # 
Instance details

Defined in Data.Aeson.SingleField

Methods

parseJSON :: Value -> Parser (SingleField field a) #

parseJSONList :: Value -> Parser [SingleField field a] #