| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Deriving.Aeson.Stock
Contents
Synopsis
- type Prefixed str = CustomJSON '[FieldLabelModifier (StripPrefix str)]
- type PrefixedSnake str = CustomJSON '[FieldLabelModifier '[StripPrefix str, CamelToSnake]]
- type Snake = CustomJSON '[FieldLabelModifier CamelToSnake]
- type Vanilla = CustomJSON ('[] :: [Type])
- newtype CustomJSON t a = CustomJSON {
- unCustomJSON :: a
- class FromJSON a
- class ToJSON a
- class Generic a
Documentation
type Prefixed str = CustomJSON '[FieldLabelModifier (StripPrefix str)] Source #
Field names are prefixed by str; strip them from JSON representation
type PrefixedSnake str = CustomJSON '[FieldLabelModifier '[StripPrefix str, CamelToSnake]] Source #
Strip str prefices and convert from CamelCase to snake_case
type Snake = CustomJSON '[FieldLabelModifier CamelToSnake] Source #
Convert from CamelCase to snake_case
type Vanilla = CustomJSON ('[] :: [Type]) Source #
No customisation
Reexports
newtype CustomJSON t a Source #
A newtype wrapper which gives FromJSON/ToJSON instances with modified options.
Constructors
| CustomJSON | |
Fields
| |
Instances
| (AesonOptions t, Generic a, GFromJSON Zero (Rep a)) => FromJSON (CustomJSON t a) Source # | |
Defined in Deriving.Aeson Methods parseJSON :: Value -> Parser (CustomJSON t a) Source # parseJSONList :: Value -> Parser [CustomJSON t a] Source # | |
| (AesonOptions t, Generic a, GToJSON Zero (Rep a), GToEncoding Zero (Rep a)) => ToJSON (CustomJSON t a) Source # | |
Defined in Deriving.Aeson Methods toJSON :: CustomJSON t a -> Value Source # toEncoding :: CustomJSON t a -> Encoding Source # toJSONList :: [CustomJSON t a] -> Value Source # toEncodingList :: [CustomJSON t a] -> Encoding Source # | |
A type that can be converted from JSON, with the possibility of failure.
In many cases, you can get the compiler to generate parsing code for you (see below). To begin, let's cover writing an instance by hand.
There are various reasons a conversion could fail. For example, an
Object could be missing a required key, an Array could be of
the wrong size, or a value could be of an incompatible type.
The basic ways to signal a failed conversion are as follows:
failyields a custom error message: it is the recommended way of reporting a failure;empty(ormzero) is uninformative: use it when the error is meant to be caught by some(;<|>)typeMismatchcan be used to report a failure when the encountered value is not of the expected JSON type;unexpectedis an appropriate alternative when more than one type may be expected, or to keep the expected type implicit.
prependFailure (or modifyFailure) add more information to a parser's
error messages.
An example type and instance using typeMismatch and prependFailure:
-- Allow ourselves to writeTextliterals. {-# LANGUAGE OverloadedStrings #-} data Coord = Coord { x :: Double, y :: Double } instanceFromJSONCoord whereparseJSON(Objectv) = Coord<$>v.:"x"<*>v.:"y" -- We do not expect a non-Objectvalue here. -- We could useemptyto fail, buttypeMismatch-- gives a much more informative error message.parseJSONinvalid =prependFailure"parsing Coord failed, " (typeMismatch"Object" invalid)
For this common case of only being concerned with a single
type of JSON value, the functions withObject, withScientific, etc.
are provided. Their use is to be preferred when possible, since
they are more terse. Using withObject, we can rewrite the above instance
(assuming the same language extension and data type) as:
instanceFromJSONCoord whereparseJSON=withObject"Coord" $ \v -> Coord<$>v.:"x"<*>v.:"y"
Instead of manually writing your FromJSON instance, there are two options
to do it automatically:
- Data.Aeson.TH provides Template Haskell functions which will derive an instance at compile time. The generated instance is optimized for your type so it will probably be more efficient than the following option.
- The compiler can provide a default generic implementation for
parseJSON.
To use the second, simply add a deriving clause to your
datatype and declare a GenericFromJSON instance for your datatype without giving
a definition for parseJSON.
For example, the previous example can be simplified to just:
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data Coord = Coord { x :: Double, y :: Double } deriving Generic
instance FromJSON Coord
or using the DerivingVia extension
deriving viaGenericallyCoord instanceFromJSONCoord
The default implementation will be equivalent to
parseJSON = ; if you need different
options, you can customize the generic decoding by defining:genericParseJSON defaultOptions
customOptions =defaultOptions{fieldLabelModifier=maptoUpper} instanceFromJSONCoord whereparseJSON=genericParseJSONcustomOptions
Instances
| FromJSON Key | |
| FromJSON DotNetTime | |
Defined in Data.Aeson.Types.FromJSON Methods parseJSON :: Value -> Parser DotNetTime Source # parseJSONList :: Value -> Parser [DotNetTime] Source # | |
| FromJSON Value | |
| FromJSON Version | |
| FromJSON CTime | |
| FromJSON Void | |
| FromJSON Int16 | |
| FromJSON Int32 | |
| FromJSON Int64 | |
| FromJSON Int8 | |
| FromJSON Word16 | |
| FromJSON Word32 | |
| FromJSON Word64 | |
| FromJSON Word8 | |
| FromJSON IntSet | |
| FromJSON Ordering | |
| FromJSON Scientific | |
| FromJSON Text | |
| FromJSON Text | |
| FromJSON ShortText | Since: aeson-2.0.2.0 |
| FromJSON CalendarDiffDays | |
Defined in Data.Aeson.Types.FromJSON Methods parseJSON :: Value -> Parser CalendarDiffDays Source # parseJSONList :: Value -> Parser [CalendarDiffDays] Source # | |
| FromJSON Day | |
| FromJSON Month | |
| FromJSON Quarter | |
| FromJSON QuarterOfYear | |
Defined in Data.Aeson.Types.FromJSON Methods parseJSON :: Value -> Parser QuarterOfYear Source # parseJSONList :: Value -> Parser [QuarterOfYear] Source # | |
| FromJSON DayOfWeek | |
| FromJSON DiffTime | This instance includes a bounds check to prevent maliciously
large inputs to fill up the memory of the target system. You can
newtype |
| FromJSON NominalDiffTime | This instance includes a bounds check to prevent maliciously
large inputs to fill up the memory of the target system. You can
newtype |
Defined in Data.Aeson.Types.FromJSON Methods parseJSON :: Value -> Parser NominalDiffTime Source # parseJSONList :: Value -> Parser [NominalDiffTime] Source # | |
| FromJSON SystemTime | |
Defined in Data.Aeson.Types.FromJSON Methods parseJSON :: Value -> Parser SystemTime Source # parseJSONList :: Value -> Parser [SystemTime] Source # | |
| FromJSON UTCTime | |
| FromJSON CalendarDiffTime | |
Defined in Data.Aeson.Types.FromJSON Methods parseJSON :: Value -> Parser CalendarDiffTime Source # parseJSONList :: Value -> Parser [CalendarDiffTime] Source # | |
| FromJSON LocalTime | |
| FromJSON TimeOfDay | |
| FromJSON ZonedTime | Supported string formats:
The first space may instead be a |
| FromJSON UUID | |
| FromJSON Integer | This instance includes a bounds check to prevent maliciously
large inputs to fill up the memory of the target system. You can
newtype |
| FromJSON Natural | |
| FromJSON () | |
| FromJSON Bool | |
| FromJSON Char | |
| FromJSON Double | |
| FromJSON Float | |
| FromJSON Int | |
| FromJSON Word | |
| FromJSON v => FromJSON (KeyMap v) | Since: aeson-2.0.1.0 |
| FromJSON a => FromJSON (Identity a) | |
| FromJSON a => FromJSON (First a) | |
| FromJSON a => FromJSON (Last a) | |
| FromJSON a => FromJSON (First a) | |
| FromJSON a => FromJSON (Last a) | |
| FromJSON a => FromJSON (Max a) | |
| FromJSON a => FromJSON (Min a) | |
| FromJSON a => FromJSON (WrappedMonoid a) | |
Defined in Data.Aeson.Types.FromJSON Methods parseJSON :: Value -> Parser (WrappedMonoid a) Source # parseJSONList :: Value -> Parser [WrappedMonoid a] Source # | |
| FromJSON a => FromJSON (Dual a) | |
| FromJSON a => FromJSON (NonEmpty a) | |
| (Generic a, GFromJSON Zero (Rep a)) => FromJSON (Generically a) | Since: aeson-2.1.0.0 |
Defined in Data.Aeson.Types.FromJSON Methods parseJSON :: Value -> Parser (Generically a) Source # parseJSONList :: Value -> Parser [Generically a] Source # | |
| (FromJSON a, Integral a) => FromJSON (Ratio a) | |
| FromJSON a => FromJSON (IntMap a) | |
| FromJSON a => FromJSON (Seq a) | |
| (Ord a, FromJSON a) => FromJSON (Set a) | |
| FromJSON v => FromJSON (Tree v) | |
| FromJSON1 f => FromJSON (Fix f) | Since: aeson-1.5.3.0 |
| (FromJSON1 f, Functor f) => FromJSON (Mu f) | Since: aeson-1.5.3.0 |
| (FromJSON1 f, Functor f) => FromJSON (Nu f) | Since: aeson-1.5.3.0 |
| FromJSON a => FromJSON (DNonEmpty a) | Since: aeson-1.5.3.0 |
| FromJSON a => FromJSON (DList a) | |
| FromJSON a => FromJSON (Array a) | |
| (Prim a, FromJSON a) => FromJSON (PrimArray a) | |
| FromJSON a => FromJSON (SmallArray a) | |
| FromJSON a => FromJSON (Maybe a) | Since: aeson-1.5.3.0 |
| (Eq a, Hashable a, FromJSON a) => FromJSON (HashSet a) | |
| FromJSON a => FromJSON (Vector a) | |
| (Prim a, FromJSON a) => FromJSON (Vector a) | |
| (Storable a, FromJSON a) => FromJSON (Vector a) | |
| (Vector Vector a, FromJSON a) => FromJSON (Vector a) | |
| FromJSON a => FromJSON (Maybe a) | |
| FromJSON a => FromJSON (a) | Since: aeson-2.0.2.0 |
| FromJSON a => FromJSON [a] | |
| (FromJSON a, FromJSON b) => FromJSON (Either a b) | |
| HasResolution a => FromJSON (Fixed a) | This instance includes a bounds check to prevent maliciously
large inputs to fill up the memory of the target system. You can
newtype |
| FromJSON (Proxy a) | |
| (FromJSONKey k, Ord k, FromJSON v) => FromJSON (Map k v) | |
| (FromJSON a, FromJSON b) => FromJSON (Either a b) | Since: aeson-1.5.3.0 |
| (FromJSON a, FromJSON b) => FromJSON (These a b) | Since: aeson-1.5.3.0 |
| (FromJSON a, FromJSON b) => FromJSON (Pair a b) | Since: aeson-1.5.3.0 |
| (FromJSON a, FromJSON b) => FromJSON (These a b) | Since: aeson-1.5.1.0 |
| (FromJSON v, FromJSONKey k, Eq k, Hashable k) => FromJSON (HashMap k v) | |
| (FromJSON a, FromJSON b) => FromJSON (a, b) | |
| FromJSON a => FromJSON (Const a b) | |
| (AesonOptions t, Generic a, GFromJSON Zero (Rep a)) => FromJSON (CustomJSON t a) Source # | |
Defined in Deriving.Aeson Methods parseJSON :: Value -> Parser (CustomJSON t a) Source # parseJSONList :: Value -> Parser [CustomJSON t a] Source # | |
| FromJSON b => FromJSON (Tagged a b) | |
| (FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (These1 f g a) | Since: aeson-1.5.1.0 |
| (FromJSON a, FromJSON b, FromJSON c) => FromJSON (a, b, c) | |
| (FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (Product f g a) | |
| (FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (Sum f g a) | |
| (FromJSON a, FromJSON b, FromJSON c, FromJSON d) => FromJSON (a, b, c, d) | |
| (FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (Compose f g a) | |
| (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e) => FromJSON (a, b, c, d, e) | |
| (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f) => FromJSON (a, b, c, d, e, f) | |
| (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g) => FromJSON (a, b, c, d, e, f, g) | |
| (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h) => FromJSON (a, b, c, d, e, f, g, h) | |
| (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i) => FromJSON (a, b, c, d, e, f, g, h, i) | |
| (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j) => FromJSON (a, b, c, d, e, f, g, h, i, j) | |
| (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k) => FromJSON (a, b, c, d, e, f, g, h, i, j, k) | |
| (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l) | |
| (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l, m) | |
| (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m, FromJSON n) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l, m, n) | |
| (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m, FromJSON n, FromJSON o) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) | |
A type that can be converted to JSON.
Instances in general must specify toJSON and should (but don't need
to) specify toEncoding.
An example type and instance:
-- Allow ourselves to writeTextliterals. {-# LANGUAGE OverloadedStrings #-} data Coord = Coord { x :: Double, y :: Double } instanceToJSONCoord wheretoJSON(Coord x y) =object["x".=x, "y".=y]toEncoding(Coord x y) =pairs("x".=x<>"y".=y)
Instead of manually writing your ToJSON instance, there are two options
to do it automatically:
- Data.Aeson.TH provides Template Haskell functions which will derive an instance at compile time. The generated instance is optimized for your type so it will probably be more efficient than the following option.
- The compiler can provide a default generic implementation for
toJSON.
To use the second, simply add a deriving clause to your
datatype and declare a GenericToJSON instance. If you require nothing other than
defaultOptions, it is sufficient to write (and this is the only
alternative where the default toJSON implementation is sufficient):
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data Coord = Coord { x :: Double, y :: Double } deriving Generic
instance ToJSON Coord where
toEncoding = genericToEncoding defaultOptions
or more conveniently using the DerivingVia extension
deriving viaGenericallyCoord instanceToJSONCoord
If on the other hand you wish to customize the generic decoding, you have to implement both methods:
customOptions =defaultOptions{fieldLabelModifier=maptoUpper} instanceToJSONCoord wheretoJSON=genericToJSONcustomOptionstoEncoding=genericToEncodingcustomOptions
Previous versions of this library only had the toJSON method. Adding
toEncoding had two reasons:
toEncodingis more efficient for the common case that the output oftoJSONis directly serialized to aByteString. Further, expressing either method in terms of the other would be non-optimal.- The choice of defaults allows a smooth transition for existing users:
Existing instances that do not define
toEncodingstill compile and have the correct semantics. This is ensured by making the default implementation oftoEncodingusetoJSON. This produces correct results, but since it performs an intermediate conversion to aValue, it will be less efficient than directly emitting anEncoding. (this also means that specifying nothing more thaninstance ToJSON Coordwould be sufficient as a generically decoding instance, but there probably exists no good reason to not specifytoEncodingin new instances.)
Instances
Representable types of kind *.
This class is derivable in GHC with the DeriveGeneric flag on.
A Generic instance must satisfy the following laws:
from.to≡idto.from≡id