Skip to content

Instantly share code, notes, and snippets.

@qnikst
Created November 8, 2017 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qnikst/5aa8ae1c1ebe17dd0e580ce1a60e8ac8 to your computer and use it in GitHub Desktop.
Save qnikst/5aa8ae1c1ebe17dd0e580ce1a60e8ac8 to your computer and use it in GitHub Desktop.
play with parse
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
import Data.Aeson
import Data.Text
import GHC.TypeLits
import GHC.Generics
data Key a where
KeyA :: Key A
KeyB :: Key B
data SomeKey = forall k . FromJSON k => SomeKey (Key k)
instance FromJSON SomeKey where
parseJSON (String s) = case s of
"A" -> pure $ SomeKey KeyA
"B" -> pure $ SomeKey KeyB
_ -> fail "bar"
parseJSON _ = fail "foo"
data P = forall k . P (Key k) k
instance FromJSON P where
parseJSON = withObject "p" $ \o -> do
tag <- o .: "tag"
case tag of
SomeKey x -> P x <$> parseJSON (Object o)
data A = A deriving (Generic)
instance ToJSON A
instance FromJSON A
data B = B deriving (Generic)
instance ToJSON B
instance FromJSON B
{-
data T (k::Symbol) = T
instance FromJSON (T k) where
parseJSON (String x) = case someSymbolpure T
parseJSON _ = fail "foo"
data family Tag a
newtype instance Tag A = AT (T "A") deriving (FromJSON)
newtype instance Tag B = BT (T "B") deriving (FromJSON)
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment