Safe Haskell | None |
---|---|
Language | Haskell2010 |
- type Table = HashMap Text Node
- emptyTable :: Table
- type VTArray = Vector Table
- type VArray = Vector Node
- data Node
- data Explicitness
- isExplicit :: Explicitness -> Bool
- insert :: TomlM m => Explicitness -> ([Text], Node) -> Table -> Parser m Table
- class ToJSON a where
- class ToBsJSON a where
- type Toml = State (Set [Text])
- type TomlM m = MonadState (Set [Text]) m
- type Parser m a = MonadState (Set [Text]) m => ParsecT Void Text m a
Documentation
emptyTable :: Table Source #
Contruct an empty Table
.
VTable !Table | |
VTArray !VTArray | |
VString !Text | |
VInteger !Int64 | |
VFloat !Double | |
VBoolean !Bool | |
VDatetime !UTCTime | |
VArray !VArray |
Eq Node Source # | |
Show Node Source # | |
ToJSON Node Source # |
|
ToBsJSON Node Source # |
As seen in this function, BurntSushi's JSON encoding explicitly specifies the types of the values. |
data Explicitness Source #
To mark whether or not a Table
has been explicitly defined.
See: https://github.com/toml-lang/toml/issues/376
isExplicit :: Explicitness -> Bool Source #
Convenience function to get a boolean value.
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 writeText
literals. {-# LANGUAGE OverloadedStrings #-} data Coord = Coord { x :: Double, y :: Double } instanceToJSON
Coord 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 Generic
ToJSON
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 } derivingGeneric
instanceToJSON
Coord wheretoEncoding
=genericToEncoding
defaultOptions
If on the other hand you wish to customize the generic decoding, you have to implement both methods:
customOptions =defaultOptions
{fieldLabelModifier
=map
toUpper
} instanceToJSON
Coord wheretoJSON
=genericToJSON
customOptionstoEncoding
=genericToEncoding
customOptions
Previous versions of this library only had the toJSON
method. Adding
toEncoding
had to reasons:
- toEncoding is more efficient for the common case that the output of
toJSON
is 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
toEncoding
still compile and have the correct semantics. This is ensured by making the default implementation oftoEncoding
usetoJSON
. 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 Coord
would be sufficient as a generically decoding instance, but there probably exists no good reason to not specifytoEncoding
in new instances.)
Convert a Haskell value to a JSON-friendly intermediate type.
toEncoding :: a -> Encoding #
Encode a Haskell value as JSON.
The default implementation of this method creates an
intermediate Value
using toJSON
. This provides
source-level compatibility for people upgrading from older
versions of this library, but obviously offers no performance
advantage.
To benefit from direct encoding, you must provide an
implementation for this method. The easiest way to do so is by
having your types implement Generic
using the DeriveGeneric
extension, and then have GHC generate a method body as follows.
instanceToJSON
Coord wheretoEncoding
=genericToEncoding
defaultOptions
toJSONList :: [a] -> Value #
toEncodingList :: [a] -> Encoding #
class ToBsJSON a where Source #
Type class for conversion to BurntSushi-style JSON.
BurntSushi has made a language agnostic test suite available that
this library uses. This test suit expects that values are encoded
as JSON objects with a 'type' and a value
member.
ToBsJSON Node Source # |
As seen in this function, BurntSushi's JSON encoding explicitly specifies the types of the values. |
ToBsJSON a => ToBsJSON (Vector a) Source # | |
ToBsJSON v => ToBsJSON (HashMap Text v) Source # | Provide a |