miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.JSON

Description

Overview

Miso.JSON is a JSON library tailored to MisoString, modelled after aeson and inspired by microaeson. It provides encoding, decoding, and a Generic-deriving mechanism that mirrors aeson's defaults, making it straightforward to reuse existing aeson-compatible type class instances.

Platform behaviour

  • Client (WASM / GHC JS backend) — encode calls JSON.stringify() and decode calls JSON.parse() via FFI for maximum performance.
  • Server (-fssr / 1 build) — a pure Haskell lexer/parser pipeline (Miso.JSON.Lexer + Miso.JSON.Parser) is used instead, with no JavaScript dependency.

The same type class instances work on both platforms; only the underlying serialisation primitive differs.

Quick start

{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics (Generic)
import Miso.JSON
import Miso.String (MisoString)

data Person = Person
  { name :: MisoString
  , age  :: Int
  } deriving (Generic, Show, Eq)

instance ToJSON   Person
instance FromJSON Person

-- Encode to a JSON string:
encoded :: MisoString
encoded = encode (Person "Alice" 30)
-- Result: "{\"name\":\"Alice\",\"age\":30}"

-- Decode from a JSON string:
decoded :: Maybe Person
decoded = decode encoded

Constructing JSON values

Use object and .= to build Value trees without defining a type:

point :: Value
point = object [ "x" .= (10 :: Int), "y" .= (20 :: Int) ]

Writing instances by hand

data Color = Red | Green | Blue

instance ToJSON Color where
  toJSON Red   = String "red"
  toJSON Green = String "green"
  toJSON Blue  = String "blue"

instance FromJSON Color where
  parseJSON = withText "Color" $ \case
    "red"   -> pure Red
    "green" -> pure Green
    "blue"  -> pure Blue
    t       -> typeMismatch "Color" (String t)

Generic encoding options

Generic instances follow aeson's default strategy. Customise with Options:

myOptions :: Options
myOptions = defaultOptions { fieldLabelModifier = camelTo2 '_' }

instance ToJSON Person where
  toJSON = genericToJSON myOptions

-- { "person_name": "Alice", "person_age": 30 }

API groups

See also

Synopsis

JSON

Core JSON types

data Value Source #

Instances

Instances details
IsString Value Source # 
Instance details

Defined in Miso.JSON.Types

Methods

fromString :: String -> Value #

Show Value Source # 
Instance details

Defined in Miso.JSON.Types

Methods

showsPrec :: Int -> Value -> ShowS #

show :: Value -> String #

showList :: [Value] -> ShowS #

Eq Value Source # 
Instance details

Defined in Miso.JSON.Types

Methods

(==) :: Value -> Value -> Bool #

(/=) :: Value -> Value -> Bool #

FromJSVal Value Source # 
Instance details

Defined in Miso.DSL

ToJSVal Value Source # 
Instance details

Defined in Miso.DSL

Methods

toJSVal :: Value -> IO JSVal Source #

FromJSON Value Source # 
Instance details

Defined in Miso.JSON

ToJSON Value Source # 
Instance details

Defined in Miso.JSON

FromMisoString Value Source # 
Instance details

Defined in Miso.JSON

ToMisoString Value Source # 
Instance details

Defined in Miso.JSON

data Result a Source #

Constructors

Success a 
Error MisoString 

Instances

Instances details
Alternative Result Source # 
Instance details

Defined in Miso.JSON.Types

Methods

empty :: Result a #

(<|>) :: Result a -> Result a -> Result a #

some :: Result a -> Result [a] #

many :: Result a -> Result [a] #

Applicative Result Source # 
Instance details

Defined in Miso.JSON.Types

Methods

pure :: a -> Result a #

(<*>) :: Result (a -> b) -> Result a -> Result b #

liftA2 :: (a -> b -> c) -> Result a -> Result b -> Result c #

(*>) :: Result a -> Result b -> Result b #

(<*) :: Result a -> Result b -> Result a #

Functor Result Source # 
Instance details

Defined in Miso.JSON.Types

Methods

fmap :: (a -> b) -> Result a -> Result b #

(<$) :: a -> Result b -> Result a #

Monad Result Source # 
Instance details

Defined in Miso.JSON.Types

Methods

(>>=) :: Result a -> (a -> Result b) -> Result b #

(>>) :: Result a -> Result b -> Result b #

return :: a -> Result a #

MonadPlus Result Source # 
Instance details

Defined in Miso.JSON.Types

Methods

mzero :: Result a #

mplus :: Result a -> Result a -> Result a #

MonadFail Result Source # 
Instance details

Defined in Miso.JSON.Types

Methods

fail :: String -> Result a #

Foldable Result Source # 
Instance details

Defined in Miso.JSON.Types

Methods

fold :: Monoid m => Result m -> m #

foldMap :: Monoid m => (a -> m) -> Result a -> m #

foldMap' :: Monoid m => (a -> m) -> Result a -> m #

foldr :: (a -> b -> b) -> b -> Result a -> b #

foldr' :: (a -> b -> b) -> b -> Result a -> b #

foldl :: (b -> a -> b) -> b -> Result a -> b #

foldl' :: (b -> a -> b) -> b -> Result a -> b #

foldr1 :: (a -> a -> a) -> Result a -> a #

foldl1 :: (a -> a -> a) -> Result a -> a #

toList :: Result a -> [a] #

null :: Result a -> Bool #

length :: Result a -> Int #

elem :: Eq a => a -> Result a -> Bool #

maximum :: Ord a => Result a -> a #

minimum :: Ord a => Result a -> a #

sum :: Num a => Result a -> a #

product :: Num a => Result a -> a #

Traversable Result Source # 
Instance details

Defined in Miso.JSON.Types

Methods

traverse :: Applicative f => (a -> f b) -> Result a -> f (Result b) #

sequenceA :: Applicative f => Result (f a) -> f (Result a) #

mapM :: Monad m => (a -> m b) -> Result a -> m (Result b) #

sequence :: Monad m => Result (m a) -> m (Result a) #

Monoid (Result a) Source # 
Instance details

Defined in Miso.JSON.Types

Methods

mempty :: Result a #

mappend :: Result a -> Result a -> Result a #

mconcat :: [Result a] -> Result a #

Semigroup (Result a) Source # 
Instance details

Defined in Miso.JSON.Types

Methods

(<>) :: Result a -> Result a -> Result a #

sconcat :: NonEmpty (Result a) -> Result a #

stimes :: Integral b => b -> Result a -> Result a #

Show a => Show (Result a) Source # 
Instance details

Defined in Miso.JSON.Types

Methods

showsPrec :: Int -> Result a -> ShowS #

show :: Result a -> String #

showList :: [Result a] -> ShowS #

Eq a => Eq (Result a) Source # 
Instance details

Defined in Miso.JSON.Types

Methods

(==) :: Result a -> Result a -> Bool #

(/=) :: Result a -> Result a -> Bool #

Constructors

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

Construct a JSON key/value Pair. Infix alias for \k v -> (k, toJSON v).

object [ "name" .= ("Alice" :: MisoString), "age" .= (30 :: Int) ]

object :: [Pair] -> Value Source #

Create a Value from a list of name/value Pairs.

emptyArray :: Value Source #

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

emptyObject :: Value Source #

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

Accessors

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

Look up a required key in a JSON Object. Fails with a parse error if the key is absent.

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

Look up an optional key in a JSON Object. Returns Nothing if the key is absent; delegates to parseJSON if present.

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

Like .:? but always wraps a present value in Just, so a key with a null JSON value decodes to Just Null rather than Nothing. Useful when you need to distinguish a missing key from an explicit null.

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

Provide a default when a Parser produces Nothing. Typically chained after .:?:

o .:? "count" .!= 0

Encoding and decoding

encode :: ToJSON a => a -> MisoString Source #

Encode a value as a JSON MisoString.

On the client (WASM / GHC JS backend) calls JSON.stringify() via FFI for maximum performance. On the server (1) falls back to encodePure.

encodePure :: ToJSON a => a -> MisoString Source #

Relies on the pure implementation of JSON parsing / serialization.

This can be used on the server or the client, it is more efficient to use encode on the client (since it relies on JSON.stringify()).

decode :: FromJSON a => MisoString -> Maybe a Source #

Decode a JSON MisoString into a Haskell value, returning Nothing on failure.

On the client calls JSON.parse() via FFI. On the server uses the pure Haskell parser. For a human-readable error message on failure use eitherDecode.

Prism-style parsers

withObject Source #

Arguments

:: MisoString

Expected type name used in the error message

-> (Object -> Parser a)

Continuation receiving the Object key/value map

-> Value

JSON value to inspect

-> Parser a 

Succeed only when the Value is a JSON object; fail with typeMismatch otherwise. The inner parser receives the Object map for key lookups with .: etc.

withText Source #

Arguments

:: MisoString

Expected type name used in the error message

-> (MisoString -> Parser a)

Continuation receiving the unwrapped string

-> Value

JSON value to inspect

-> Parser a 

Succeed only when the Value is a JSON string (MisoString); fail with typeMismatch otherwise.

withArray Source #

Arguments

:: MisoString

Expected type name used in the error message

-> ([Value] -> Parser a)

Continuation receiving the list of array elements

-> Value

JSON value to inspect

-> Parser a 

Succeed only when the Value is a JSON array; fail with typeMismatch otherwise. The inner parser receives the list of elements.

withNumber Source #

Arguments

:: MisoString

Expected type name used in the error message

-> (Double -> Parser a)

Continuation receiving the numeric value as a Double

-> Value

JSON value to inspect

-> Parser a 

Succeed only when the Value is a JSON number; fail with typeMismatch otherwise. The inner parser receives the underlying Double.

withBool Source #

Arguments

:: MisoString

Expected type name used in the error message (e.g. "MyType")

-> (Bool -> Parser a)

Continuation receiving the unwrapped Bool

-> Value

JSON value to inspect

-> Parser a 

Succeed only when the Value is a Bool; fail with typeMismatch otherwise.

Type conversion

class FromJSON a where Source #

A type that can be deserialised from a JSON Value.

Instances for the most common Haskell types are provided. Derive via Generics for product/sum types, or write instances by hand:

-- Generic derivation:
data Point = Point { x :: Double, y :: Double }
  deriving (Generic, Show, Eq)
instance FromJSON Point

-- Manual instance:
instance FromJSON Point where
  parseJSON = withObject "Point" $ \o ->
    Point <$> o .: "x" <*> o .: "y"

Minimal complete definition

Nothing

Methods

parseJSON :: Value -> Parser a Source #

Parse a JSON Value into a, failing with a descriptive error message via Parser on a type mismatch.

default parseJSON :: (Generic a, GFromJSON (Rep a)) => Value -> Parser a Source #

Instances

Instances details
FromJSON Int16 Source # 
Instance details

Defined in Miso.JSON

FromJSON Int32 Source # 
Instance details

Defined in Miso.JSON

FromJSON Int64 Source # 
Instance details

Defined in Miso.JSON

FromJSON Int8 Source # 
Instance details

Defined in Miso.JSON

FromJSON Word16 Source # 
Instance details

Defined in Miso.JSON

FromJSON Word32 Source # 
Instance details

Defined in Miso.JSON

FromJSON Word64 Source # 
Instance details

Defined in Miso.JSON

FromJSON Word8 Source # 
Instance details

Defined in Miso.JSON

FromJSON Ordering Source # 
Instance details

Defined in Miso.JSON

FromJSON Checked Source # 
Instance details

Defined in Miso.Event.Types

FromJSON KeyCode Source # 
Instance details

Defined in Miso.Event.Types

FromJSON PointerType Source # 
Instance details

Defined in Miso.Event.Types

FromJSON Value Source # 
Instance details

Defined in Miso.JSON

FromJSON MisoString Source # 
Instance details

Defined in Miso.JSON

FromJSON URI Source # 
Instance details

Defined in Miso.Router

FromJSON Text Source # 
Instance details

Defined in Miso.JSON

FromJSON Integer Source # 
Instance details

Defined in Miso.JSON

FromJSON Natural Source # 
Instance details

Defined in Miso.JSON

FromJSON String Source # 
Instance details

Defined in Miso.JSON

FromJSON () Source # 
Instance details

Defined in Miso.JSON

Methods

parseJSON :: Value -> Parser () Source #

FromJSON Bool Source # 
Instance details

Defined in Miso.JSON

FromJSON Char Source # 
Instance details

Defined in Miso.JSON

FromJSON Double Source # 
Instance details

Defined in Miso.JSON

FromJSON Float Source # 
Instance details

Defined in Miso.JSON

FromJSON Int Source # 
Instance details

Defined in Miso.JSON

FromJSON Word Source # 
Instance details

Defined in Miso.JSON

FromJSON a => FromJSON (Maybe a) Source # 
Instance details

Defined in Miso.JSON

Methods

parseJSON :: Value -> Parser (Maybe a) Source #

FromJSON a => FromJSON [a] Source # 
Instance details

Defined in Miso.JSON

Methods

parseJSON :: Value -> Parser [a] Source #

FromJSON v => FromJSON (Map MisoString v) Source # 
Instance details

Defined in Miso.JSON

(FromJSON a, FromJSON b) => FromJSON (a, b) Source # 
Instance details

Defined in Miso.JSON

Methods

parseJSON :: Value -> Parser (a, b) Source #

(FromJSON a, FromJSON b, FromJSON c) => FromJSON (a, b, c) Source # 
Instance details

Defined in Miso.JSON

Methods

parseJSON :: Value -> Parser (a, b, c) Source #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d) => FromJSON (a, b, c, d) Source # 
Instance details

Defined in Miso.JSON

Methods

parseJSON :: Value -> Parser (a, b, c, d) Source #

newtype Parser a Source #

A lightweight JSON parse monad. Wraps Either MisoString a so that parse failures carry a human-readable error message.

Parser is a Functor, Applicative, Monad, MonadFail, and Alternative. The Alternative instance tries the right branch only when the left branch fails — useful for decoding sum types with multiple valid shapes.

Combine with parseJSON and the accessor operators (.: etc.) to build composite decoders:

data Point = Point Double Double

instance FromJSON Point where
  parseJSON = withArray "Point" $ \xs ->
    Point <$> parseJSON (xs !! 0)
          <*> parseJSON (xs !! 1)

Constructors

Parser 

Fields

Instances

Instances details
Alternative Parser Source # 
Instance details

Defined in Miso.JSON

Methods

empty :: Parser a #

(<|>) :: Parser a -> Parser a -> Parser a #

some :: Parser a -> Parser [a] #

many :: Parser a -> Parser [a] #

Applicative Parser Source # 
Instance details

Defined in Miso.JSON

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 Source # 
Instance details

Defined in Miso.JSON

Methods

fmap :: (a -> b) -> Parser a -> Parser b #

(<$) :: a -> Parser b -> Parser a #

Monad Parser Source # 
Instance details

Defined in Miso.JSON

Methods

(>>=) :: Parser a -> (a -> Parser b) -> Parser b #

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

return :: a -> Parser a #

MonadPlus Parser Source # 
Instance details

Defined in Miso.JSON

Methods

mzero :: Parser a #

mplus :: Parser a -> Parser a -> Parser a #

MonadFail Parser Source # 
Instance details

Defined in Miso.JSON

Methods

fail :: String -> Parser a #

parseMaybe Source #

Arguments

:: (a -> Parser b)

Parser function to apply

-> a

Input value to parse

-> Maybe b 

Run a parser function, returning Nothing on failure instead of an error string.

class ToJSON a where Source #

A type that can be serialised to a JSON Value.

Instances for the most common Haskell types are provided. Derive via Generics for product/sum types, or write instances by hand for full control:

-- Generic derivation (mirrors aeson defaults):
data Point = Point { x :: Double, y :: Double }
  deriving (Generic, Show, Eq)
instance ToJSON Point

-- Manual instance:
instance ToJSON Point where
  toJSON (Point x y) = object ["x" .= x, "y" .= y]

Minimal complete definition

Nothing

Methods

toJSON :: a -> Value Source #

Convert a value to a JSON Value.

default toJSON :: (Generic a, GToJSON (Rep a)) => a -> Value Source #

Instances

Instances details
ToJSON Int16 Source # 
Instance details

Defined in Miso.JSON

ToJSON Int32 Source # 
Instance details

Defined in Miso.JSON

ToJSON Int64 Source #

Possibly lossy due to conversion to Double

Instance details

Defined in Miso.JSON

ToJSON Int8 Source # 
Instance details

Defined in Miso.JSON

ToJSON Word16 Source # 
Instance details

Defined in Miso.JSON

ToJSON Word32 Source # 
Instance details

Defined in Miso.JSON

ToJSON Word64 Source #

Possibly lossy due to conversion to Double

Instance details

Defined in Miso.JSON

ToJSON Word8 Source # 
Instance details

Defined in Miso.JSON

ToJSON Value Source # 
Instance details

Defined in Miso.JSON

ToJSON MisoString Source # 
Instance details

Defined in Miso.JSON

ToJSON Key Source # 
Instance details

Defined in Miso.Types

Methods

toJSON :: Key -> Value Source #

toJSONList :: [Key] -> Value

ToJSON URI Source # 
Instance details

Defined in Miso.Types

Methods

toJSON :: URI -> Value Source #

toJSONList :: [URI] -> Value

ToJSON Text Source # 
Instance details

Defined in Miso.JSON

ToJSON Integer Source #

Possibly lossy due to conversion to Double

Instance details

Defined in Miso.JSON

ToJSON Natural Source #

Possibly lossy due to conversion to Double

Instance details

Defined in Miso.JSON

ToJSON () Source # 
Instance details

Defined in Miso.JSON

Methods

toJSON :: () -> Value Source #

toJSONList :: [()] -> Value

ToJSON Bool Source # 
Instance details

Defined in Miso.JSON

ToJSON Char Source # 
Instance details

Defined in Miso.JSON

ToJSON Double Source # 
Instance details

Defined in Miso.JSON

ToJSON Float Source # 
Instance details

Defined in Miso.JSON

ToJSON Int Source # 
Instance details

Defined in Miso.JSON

Methods

toJSON :: Int -> Value Source #

toJSONList :: [Int] -> Value

ToJSON Word Source # 
Instance details

Defined in Miso.JSON

ToJSON a => ToJSON (Maybe a) Source # 
Instance details

Defined in Miso.JSON

Methods

toJSON :: Maybe a -> Value Source #

toJSONList :: [Maybe a] -> Value

ToJSON a => ToJSON [a] Source # 
Instance details

Defined in Miso.JSON

Methods

toJSON :: [a] -> Value Source #

toJSONList :: [[a]] -> Value

ToJSON v => ToJSON (Map MisoString v) Source # 
Instance details

Defined in Miso.JSON

(ToJSON a, ToJSON b) => ToJSON (a, b) Source # 
Instance details

Defined in Miso.JSON

Methods

toJSON :: (a, b) -> Value Source #

toJSONList :: [(a, b)] -> Value

(ToJSON a, ToJSON b, ToJSON c) => ToJSON (a, b, c) Source # 
Instance details

Defined in Miso.JSON

Methods

toJSON :: (a, b, c) -> Value Source #

toJSONList :: [(a, b, c)] -> Value

(ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON (a, b, c, d) Source # 
Instance details

Defined in Miso.JSON

Methods

toJSON :: (a, b, c, d) -> Value Source #

toJSONList :: [(a, b, c, d)] -> Value

Misc.

fromJSON :: FromJSON a => Value -> Result a Source #

Convert a JSON Value to a Haskell type, returning a Result. Useful when a Value is already in hand; use decode to parse from a string.

parseEither Source #

Arguments

:: (a -> Parser b)

Parser function to apply

-> a

Input value to parse

-> Either MisoString b 

Run a parser function, returning Left errMsg on failure.

eitherDecode :: FromJSON a => MisoString -> Either MisoString a Source #

Decode a JSON MisoString, returning Left errMsg on failure. Prefer decode when the error message is not needed.

typeMismatch Source #

Arguments

:: MisoString

Human-readable name of the expected type (e.g. "Int")

-> Value

The actual Value that was encountered

-> Parser a 

Produce a parse failure describing a type mismatch. Used by the with* combinators and useful in hand-written FromJSON instances.

Pretty

encodePretty :: ToJSON a => a -> MisoString Source #

Encode a value as indented (pretty-printed) JSON using defConfig (4-space indentation). Not available in the 1 build.

encodePretty' :: ToJSON a => Config -> a -> MisoString Source #

Like encodePretty but with a custom Config. Not available in the 1 build.

defConfig :: Config Source #

Default pretty-print config: 4-space indentation.

newtype Config Source #

Pretty-printing configuration for encodePretty and encodePretty'.

Constructors

Config 

Fields

  • spaces :: Int

    Number of spaces per indentation level.

Instances

Instances details
Show Config Source # 
Instance details

Defined in Miso.JSON

Eq Config Source # 
Instance details

Defined in Miso.JSON

Methods

(==) :: Config -> Config -> Bool #

(/=) :: Config -> Config -> Bool #

FFI

fromJSVal_Value :: JSVal -> IO (Maybe Value) Source #

Convert a Miso JSON Value to a raw JavaScript value via FFI.

Convert a raw JavaScript value to a Miso JSON Value via FFI. Returns Nothing if the JS value cannot be represented as a JSON Value.

jsonStringify :: JSVal -> IO MisoString Source #

Call JSON.stringify() on a JavaScript value, returning a JSON string.

jsonParse :: MisoString -> IO JSVal Source #

Call JSON.parse() on a JSON string, returning a raw JavaScript value.

Options

data Options Source #

Configuration for generic JSON encoding and decoding via genericToJSON and genericParseJSON. Mirrors the subset of aeson's Options that is relevant to miso's Generic machinery.

Construct with defaultOptions and override only the fields you need:

myOpts :: Options
myOpts = defaultOptions { fieldLabelModifier = camelTo2 '_' }

Constructors

Options 

Fields

defaultOptions :: Options Source #

Default encoding/decoding options, matching aeson's defaults: no field or constructor name transformation, allNullaryToStringTag enabled, omitNothingFields disabled.

Generics

class GToJSON (f :: Type -> Type) where Source #

Top-level generic encoding class.

Encoding rules match aeson's defaults:

  • All-nullary sum + allNullaryToStringTag: "C"
  • Single-constructor record: {"field1": v1, ...}
  • Single-constructor positional: v (1 field), [v1,v2,...] (n>1), [] (0)
  • Sum record constructor: {"tag": "C", "field1": v1, ...}
  • Sum nullary constructor: {"tag": "C"}
  • Sum positional constructor: {"tag": "C", "contents": v} or [...]

Methods

gToJSON :: Options -> f a -> Value Source #

Instances

Instances details
GToJSONRep f => GToJSON (D1 m f) Source # 
Instance details

Defined in Miso.JSON

Methods

gToJSON :: Options -> D1 m f a -> Value Source #

class GToFields (f :: Type -> Type) where Source #

Collect a constructor's fields into Fields.

Methods

gToFields :: Options -> f a -> Fields Source #

Instances

Instances details
GToFields (U1 :: Type -> Type) Source # 
Instance details

Defined in Miso.JSON

Methods

gToFields :: Options -> U1 a -> Fields Source #

GToFields (V1 :: Type -> Type) Source # 
Instance details

Defined in Miso.JSON

Methods

gToFields :: Options -> V1 a -> Fields Source #

(GToFields f, GToFields g) => GToFields (f :*: g) Source # 
Instance details

Defined in Miso.JSON

Methods

gToFields :: Options -> (f :*: g) a -> Fields Source #

ToJSON a => GToFields (K1 r a :: Type -> Type) Source # 
Instance details

Defined in Miso.JSON

Methods

gToFields :: Options -> K1 r a a0 -> Fields Source #

(Selector m, ToJSON a) => GToFields (S1 m (K1 r (Maybe a) :: Type -> Type)) Source #

Special GToFields instance for Maybe a fields that honours omitNothingFields: when the option is True and the value is Nothing, the field is omitted from the encoded object entirely.

Instance details

Defined in Miso.JSON

Methods

gToFields :: Options -> S1 m (K1 r (Maybe a) :: Type -> Type) a0 -> Fields Source #

(Selector m, GToFields f) => GToFields (S1 m f) Source # 
Instance details

Defined in Miso.JSON

Methods

gToFields :: Options -> S1 m f a -> Fields Source #

class GToJSONSum (f :: Type -> Type) where Source #

Encode sum constructors with a "tag" key.

Methods

gToJSONSum :: Options -> f a -> Value Source #

Instances

Instances details
(GToJSONSum f, GToJSONSum g) => GToJSONSum (f :+: g) Source # 
Instance details

Defined in Miso.JSON

Methods

gToJSONSum :: Options -> (f :+: g) a -> Value Source #

(Constructor m, GToFields f) => GToJSONSum (C1 m f) Source # 
Instance details

Defined in Miso.JSON

Methods

gToJSONSum :: Options -> C1 m f a -> Value Source #

class GAllNullary (f :: Type -> Type) where Source #

Determine at the type level whether every constructor of a sum type is nullary (has no fields). Used to implement allNullaryToStringTag.

Instances

Instances details
GAllNullary (U1 :: Type -> Type) Source # 
Instance details

Defined in Miso.JSON

(GAllNullary f, GAllNullary g) => GAllNullary (f :*: g) Source # 
Instance details

Defined in Miso.JSON

(GAllNullary f, GAllNullary g) => GAllNullary (f :+: g) Source # 
Instance details

Defined in Miso.JSON

GAllNullary f => GAllNullary (C1 m f) Source # 
Instance details

Defined in Miso.JSON

GAllNullary (K1 r a :: Type -> Type) Source # 
Instance details

Defined in Miso.JSON

GAllNullary f => GAllNullary (S1 m f) Source # 
Instance details

Defined in Miso.JSON

data Fields Source #

Intermediate representation of a constructor's fields after encoding.

RecordFields is produced when every selector has a name (record syntax); PositionalFields is produced for all other constructors.

Constructors

RecordFields [(MisoString, Value)]

Named fields (record constructor)

PositionalFields [Value]

Positional fields (non-record constructor)

class GFromJSON (f :: Type -> Type) where Source #

Top-level generic decoding class. Symmetric with GToJSON.

Decoding rules match aeson's defaults (see Options and defaultOptions).

Methods

gParseJSON :: Options -> Value -> Parser (f a) Source #

Instances

Instances details
GFromJSONRep f => GFromJSON (D1 m f) Source # 
Instance details

Defined in Miso.JSON

Methods

gParseJSON :: Options -> Value -> Parser (D1 m f a) Source #

class GFromFields (f :: Type -> Type) where Source #

Field-level decoder. Knows whether the constructor is a record and how many fields it has; can decode from a JSON Object (record mode) or a positional '[Value]' list.

Methods

gIsRecord :: Bool Source #

Is this a record constructor (all selectors have names)?

gFieldCount :: Int Source #

Number of fields.

gFromRecord :: Options -> Object -> Parser (f a) Source #

Decode from a JSON Object (record mode: look up by field name).

gFromPositional :: Options -> [Value] -> Parser (f a) Source #

Decode from a positional list of Value.

Instances

Instances details
GFromFields (U1 :: Type -> Type) Source # 
Instance details

Defined in Miso.JSON

GFromFields (V1 :: Type -> Type) Source # 
Instance details

Defined in Miso.JSON

(GFromFields f, GFromFields g) => GFromFields (f :*: g) Source # 
Instance details

Defined in Miso.JSON

(Selector m, FromJSON a) => GFromFields (S1 m (K1 r (Maybe a) :: Type -> Type)) Source #

Selector with a Maybe field: uses .:? so missing keys decode as Nothing.

Instance details

Defined in Miso.JSON

(Selector m, FromJSON a) => GFromFields (S1 m (K1 r a :: Type -> Type)) Source #

General selector.

Instance details

Defined in Miso.JSON

Methods

gIsRecord :: Bool Source #

gFieldCount :: Int Source #

gFromRecord :: Options -> Object -> Parser (S1 m (K1 r a :: Type -> Type) a0) Source #

gFromPositional :: Options -> [Value] -> Parser (S1 m (K1 r a :: Type -> Type) a0) Source #

class GFromJSONSum (f :: Type -> Type) where Source #

Parse sum constructors, trying each branch left-to-right.

Methods

gFromJSONSum :: Options -> Value -> Parser (f a) Source #

Instances

Instances details
(GFromJSONSum f, GFromJSONSum g) => GFromJSONSum (f :+: g) Source # 
Instance details

Defined in Miso.JSON

Methods

gFromJSONSum :: Options -> Value -> Parser ((f :+: g) a) Source #

(Constructor m, GFromFields f) => GFromJSONSum (C1 m f) Source # 
Instance details

Defined in Miso.JSON

Methods

gFromJSONSum :: Options -> Value -> Parser (C1 m f a) Source #

genericToJSON Source #

Arguments

:: (Generic a, GToJSON (Rep a)) 
=> Options

Encoding options (field/constructor name modifiers, etc.)

-> a

Value to encode

-> Value 

Derive toJSON via Generics with custom Options. Called by the default ToJSON implementation using defaultOptions.

genericParseJSON Source #

Arguments

:: (Generic a, GFromJSON (Rep a)) 
=> Options

Decoding options (field/constructor name modifiers, etc.)

-> Value

JSON Value to decode

-> Parser a 

Derive parseJSON via Generics with custom Options. Called by the default FromJSON implementation using defaultOptions.

Modifiers

camelTo2 Source #

Arguments

:: Char

Delimiter character to insert between words (e.g. '_' or '-')

-> String

camelCase identifier to transform

-> String 

Convert a camelCase identifier to a separated form using the given delimiter.

camelTo2 '_' "camelCaseField" == "camel_case_field"

Commonly used as fieldLabelModifier in a custom Options.

Orphan instances