aeson-flat-0.1.1: Tools for creating flat JSON serializations

Safe HaskellNone
LanguageHaskell2010

Data.Aeson.Flat

Synopsis

Documentation

merge :: [Value] -> Value Source #

Merge values together. Useful for creating compound JSON structures that should be parsed as one object

data A = A { one :: String, two :: String } deriving (Show, Eq, Generic)
instance ToJSON A
instance FromJSON A

data B = B { three :: String } deriving (Show, Eq, Generic)
instance ToJSON B
instance FromJSON B

data AB = AB A B deriving (Show, Eq)

instance ToJSON AB where
  toJSON (AB a b) = merge [toJSON a, toJSON b]

instance FromJSON AB where
  parseJSON o = do
    a <- parseJSON o
    b <- parseJSON o
    return $ AB a b

flatToJSON :: (Generic a, GToJSON Zero (Rep a)) => Text -> a -> Value Source #

Serialize a sumtype to a flat object, rather than to "tag" and "contents"

data C = CA A | CB B deriving (Show, Eq, Generic)

instance ToJSON C where
  toJSON x = flatToJSON "c" x

{"c": CA, "one": "value", "two": "value"} {"c": CB, "three": "value"}

flatParseJSON :: (Generic a, GFromJSON Zero (Rep a)) => Text -> Value -> Parser a Source #

Deserialize a sumtype from a flat object > > instance FromJSON C where > parseJSON x = flatParseJSON "c" x