Data.Aeson.Micro

microaeson-0.1.0.2: A tiny JSON library with light dependency footprint

Safe Haskell | None
Language | Haskell2010

Data.Aeson.Micro

Contents

- Core JSON types
  - Constructors
  - Accessors
- Encoding and decoding
- Prism-style parsers
- Type conversion

Description

Minimal JavaScript Object Notation (JSON) support as per RFC 8259.

This API provides a subset (with a couple of divergences; see below) of aeson API but puts the emphasis on simplicity rather than performance and features.

The `ToJSON` and `FromJSON` instances are intended to have an encoding compatible with `aeson`'s encoding.

## Limitations and divergences from `aeson`'s API

In order to reduce the dependency footprint and keep the code simpler, the following divergences from the `aeson` API have to be made:

- There are no `FromJSON`/`ToJSON` instances for `Char` & `String`.
- The type synonym (& the constructor of the same name) `Object` uses `containers`'s `Map` rather than a `HashMap` `unordered-containers`.
- `Array` is represented by an ordinary list rather than a `Vector` from the `vector` package.
- `Number` uses `Double` instead of `Scientific`

Synopsis

- data Value
  - = Object !Object
  - | Array [Value]
  - | String !Text
  - | Number !Double
  - | Bool !Bool
  - | Null
- type Object = Map Text Value
- type Pair = (Text, Value)
- (.=) :: ToJSON v => Text -> v -> Pair
- object :: [Pair] -> Value
- emptyArray :: Value
- emptyObject :: Value
- (.:) :: FromJSON a => Object -> Text -> Parser a
- (.:?) :: FromJSON a => Object -> Text -> Parser (Maybe a)
- (.:!) :: FromJSON a => Object -> Text -> Parser (Maybe a)
- (.!=) :: Parser (Maybe a) -> a -> Parser a
- encode :: ToJSON a => a -> ByteString
- encodeStrict :: ToJSON a => a -> ByteString
- encodeToBuilder :: ToJSON a => a -> Builder
- decodeStrict :: FromJSON a => ByteString -> Maybe a
- decode :: FromJSON a => ByteString -> Maybe a
- decodeStrictN :: FromJSON a => ByteString -> Maybe [a]
- withObject :: String -> (Object -> Parser a) -> Value -> Parser a
- withText :: String -> (Text -> Parser a) -> Value -> Parser a
- withArray :: String -> ([Value] -> Parser a) -> Value -> Parser a
- withNumber :: String -> (Double -> Parser a) -> Value -> Parser a
- withBool :: String -> (Bool -> Parser a) -> Value -> Parser a
- class FromJSON a where
  - parseJSON :: Value -> Parser a
- data Parser a
- parseMaybe :: (a -> Parser b) -> a -> Maybe b
- class ToJSON a where
  - toJSON :: a -> Value

# Core JSON types

data Value #

A JSON value represented as a Haskell value.

Constructors

Object !Object |
Array [Value] |
String !Text |
Number !Double |
Bool !Bool |
Null |

#### Instances

Instances details

NFData Value # |
Instance details Defined in Data.Aeson.Micro Methods rnf :: Value -> () #
Data Value # |
Instance details Defined in Data.Aeson.Micro Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Value -> c Value # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Value # toConstr :: Value -> Constr # dataTypeOf :: Value -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Value) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Value) # gmapT :: (forall b. Data b => b -> b) -> Value -> Value # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Value -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Value -> r # gmapQ :: (forall d. Data d => d -> u) -> Value -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Value -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Value -> m Value # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Value -> m Value # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Value -> m Value #
IsString Value # |
Instance details Defined in Data.Aeson.Micro Methods fromString :: String -> Value #
Generic Value # |
Instance details Defined in Data.Aeson.Micro Associated Types
type Rep Value |
Instance details Defined in Data.Aeson.Micro type Rep Value = D1 ('MetaData "Value" "Data.Aeson.Micro" "microaeson-0.1.0.2-Lmv7Zspqj4CIFgvoPCMZ1j" 'False) ((C1 ('MetaCons "Object" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Object)) :+: (C1 ('MetaCons "Array" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [Value])) :+: C1 ('MetaCons "String" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)))) :+: (C1 ('MetaCons "Number" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double)) :+: (C1 ('MetaCons "Bool" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool)) :+: C1 ('MetaCons "Null" 'PrefixI 'False) (U1 :: Type -> Type))))
Methods from :: Value -> Rep Value x # to :: Rep Value x -> Value #
Read Value # |
Instance details Defined in Data.Aeson.Micro Methods readsPrec :: Int -> ReadS Value # readList :: ReadS [Value] # readPrec :: ReadPrec Value # readListPrec :: ReadPrec [Value] #
Show Value # |
Instance details Defined in Data.Aeson.Micro Methods showsPrec :: Int -> Value -> ShowS # show :: Value -> String # showList :: [Value] -> ShowS #
Eq Value # |
Instance details Defined in Data.Aeson.Micro Methods (==) :: Value -> Value -> Bool # (/=) :: Value -> Value -> Bool #
FromJSON Value # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Value #
ToJSON Value # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Value -> Value #
type Rep Value # |
Instance details Defined in Data.Aeson.Micro type Rep Value = D1 ('MetaData "Value" "Data.Aeson.Micro" "microaeson-0.1.0.2-Lmv7Zspqj4CIFgvoPCMZ1j" 'False) ((C1 ('MetaCons "Object" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Object)) :+: (C1 ('MetaCons "Array" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [Value])) :+: C1 ('MetaCons "String" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)))) :+: (C1 ('MetaCons "Number" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double)) :+: (C1 ('MetaCons "Bool" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool)) :+: C1 ('MetaCons "Null" 'PrefixI 'False) (U1 :: Type -> Type))))

type Object = Map Text Value #

A JSON "object" (key/value map).

type Pair = (Text, Value) #

A key/value pair for an `Object`

## Constructors

(.=) :: ToJSON v => Text -> v -> Pair infixr 8 #

A key-value pair for encoding a JSON object.

object :: [Pair] -> Value #

Create a `Value` from a list of name/value `Pair`s.

emptyArray :: Value #

The empty JSON `Array` (i.e. `[]`).

emptyObject :: Value #

The empty JSON `Object` (i.e. `{}`).

## Accessors

(.:) :: FromJSON a => Object -> Text -> Parser a #

(.:?) :: FromJSON a => Object -> Text -> Parser (Maybe a) #

(.:!) :: FromJSON a => Object -> Text -> Parser (Maybe a) #

(.!=) :: Parser (Maybe a) -> a -> Parser a #

# Encoding and decoding

encode :: ToJSON a => a -> ByteString #

Serialise value as JSON/UTF-8-encoded lazy `ByteString`

encodeStrict :: ToJSON a => a -> ByteString #

Serialise value as JSON/UTF-8-encoded strict `ByteString`

encodeToBuilder :: ToJSON a => a -> Builder #

Serialise value as JSON/UTF8-encoded `Builder`

decodeStrict :: FromJSON a => ByteString -> Maybe a #

Decode a single JSON document

decode :: FromJSON a => ByteString -> Maybe a #

Decode a single JSON document

decodeStrictN :: FromJSON a => ByteString -> Maybe [a] #

Decode multiple concatenated JSON documents

# Prism-style parsers

withObject :: String -> (Object -> Parser a) -> Value -> Parser a #

withText :: String -> (Text -> Parser a) -> Value -> Parser a #

withArray :: String -> ([Value] -> Parser a) -> Value -> Parser a #

withNumber :: String -> (Double -> Parser a) -> Value -> Parser a #

withBool :: String -> (Bool -> Parser a) -> Value -> Parser a #

# Type conversion

class FromJSON a where #

A type that JSON can be deserialised into

Methods

parseJSON :: Value -> Parser a #

Decode a JSON value into a native Haskell type

#### Instances

Instances details

FromJSON Int16 # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Int16 #
FromJSON Int32 # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Int32 #
FromJSON Int64 # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Int64 #
FromJSON Int8 # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Int8 #
FromJSON Word16 # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Word16 #
FromJSON Word32 # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Word32 #
FromJSON Word64 # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Word64 #
FromJSON Word8 # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Word8 #
FromJSON Ordering # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Ordering #
FromJSON Value # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Value #
FromJSON Text # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Text #
FromJSON Text # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Text #
FromJSON Integer # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Integer #
FromJSON () # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser () #
FromJSON Bool # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Bool #
FromJSON Double # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Double #
FromJSON Float # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Float #
FromJSON Int # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Int #
FromJSON Word # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser Word #
FromJSON a => FromJSON (Maybe a) # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser (Maybe a) #
FromJSON a => FromJSON [a] # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser [a] #
FromJSON v => FromJSON (Map Text v) # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser (Map Text v) #
(FromJSON a, FromJSON b) => FromJSON (a, b) # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser (a, b) #
(FromJSON a, FromJSON b, FromJSON c) => FromJSON (a, b, c) # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser (a, b, c) #
(FromJSON a, FromJSON b, FromJSON c, FromJSON d) => FromJSON (a, b, c, d) # |
Instance details Defined in Data.Aeson.Micro Methods parseJSON :: Value -> Parser (a, b, c, d) #

data Parser a #

JSON Parser `Monad` used by `FromJSON`

#### Instances

Instances details

Applicative Parser # |
Instance details Defined in Data.Aeson.Micro Methods pure :: a -> Parser a # (<*>) :: Parser (a -> b) -> Parser a -> Parser b # liftA2 :: (a -> b -> c) -> Parser a -> Parser b -> Parser c # (*>) :: Parser a -> Parser b -> Parser b # (<*) :: Parser a -> Parser b -> Parser a #
Functor Parser # |
Instance details Defined in Data.Aeson.Micro Methods fmap :: (a -> b) -> Parser a -> Parser b # (<$) :: a -> Parser b -> Parser a #
Monad Parser # |
Instance details Defined in Data.Aeson.Micro Methods (>>=) :: Parser a -> (a -> Parser b) -> Parser b # (>>) :: Parser a -> Parser b -> Parser b # return :: a -> Parser a #
MonadFail Parser # |
Instance details Defined in Data.Aeson.Micro Methods fail :: String -> Parser a #

parseMaybe :: (a -> Parser b) -> a -> Maybe b #

Run `Parser`.

A common use-case is ``parseMaybe` `parseJSON``.

class ToJSON a where #

A type that can be converted to JSON.

Methods

toJSON :: a -> Value #

Convert a Haskell value to a JSON-friendly intermediate type.

#### Instances

Instances details

ToJSON Int16 # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Int16 -> Value #
ToJSON Int32 # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Int32 -> Value #
ToJSON Int64 # | Possibly lossy due to conversion to `Double`
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Int64 -> Value #
ToJSON Int8 # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Int8 -> Value #
ToJSON Word16 # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Word16 -> Value #
ToJSON Word32 # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Word32 -> Value #
ToJSON Word64 # | Possibly lossy due to conversion to `Double`
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Word64 -> Value #
ToJSON Word8 # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Word8 -> Value #
ToJSON Value # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Value -> Value #
ToJSON Text # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Text -> Value #
ToJSON Text # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Text -> Value #
ToJSON Integer # | Possibly lossy due to conversion to `Double`
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Integer -> Value #
ToJSON () # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: () -> Value #
ToJSON Bool # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Bool -> Value #
ToJSON Double # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Double -> Value #
ToJSON Float # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Float -> Value #
ToJSON Int # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Int -> Value #
ToJSON Word # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Word -> Value #
ToJSON a => ToJSON (Maybe a) # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Maybe a -> Value #
ToJSON a => ToJSON [a] # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: [a] -> Value #
ToJSON v => ToJSON (Map Text v) # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: Map Text v -> Value #
(ToJSON a, ToJSON b) => ToJSON (a, b) # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: (a, b) -> Value #
(ToJSON a, ToJSON b, ToJSON c) => ToJSON (a, b, c) # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: (a, b, c) -> Value #
(ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON (a, b, c, d) # |
Instance details Defined in Data.Aeson.Micro Methods toJSON :: (a, b, c, d) -> Value #