-
-
Save qnikst/5aa8ae1c1ebe17dd0e580ce1a60e8ac8 to your computer and use it in GitHub Desktop.
play with parse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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