Last active
August 5, 2020 14:24
Revisions
-
danidiaz revised this gist
Feb 25, 2015 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,8 @@ -- Example of a dynamically generated FromJSON instance. -- -- Can be useful when one needs to use a function with a -- FromJSON constraint, but some detail about the -- conversion from JSON is not known until runtime. {-# LANGUAGE Rank2Types #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE UndecidableInstances #-} -
danidiaz revised this gist
Feb 24, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -35,7 +35,7 @@ newtype J a s = J { runJ :: a } -- use reflect to recover the function and implement -- our FromJSON instance for J. instance Reifies s (Object -> Parser a) => FromJSON (J a s) where parseJSON (Object v) = J <$> reflect (Proxy :: Proxy s) v -- Convince the compiler that the phantom type in the proxy -- supplied by reify is the same as the phantom type in J. -
danidiaz revised this gist
Feb 24, 2015 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -18,8 +18,6 @@ import Control.Applicative import Control.Lens (preview) -- from lens import Data.Aeson.Lens (_Value,_String) -- form lens-aeson data Foo = Foo { field1 :: Int -
danidiaz revised this gist
Feb 24, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -54,7 +54,7 @@ exampleJSON = maybe Null id (preview _Value str) main :: IO () main = do putStrLn "Enter prefix for the fields: " -- "zz" must be entered for the parse to succeed prefix <- fmap pack getLine -- fromJSON uses the dynamically generated FromJSON instance -
danidiaz revised this gist
Feb 24, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ import Data.Aeson.Types (Parser) import Control.Applicative -- These imports are only for constructing the example value import Control.Lens (preview) -- from lens import Data.Aeson.Lens (_Value,_String) -- form lens-aeson -
danidiaz revised this gist
Feb 24, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ import Data.Aeson.Types (Parser) import Control.Applicative -- These imports only for constructing the example value import Control.Lens (preview) -- from lens import Data.Aeson.Lens (_Value,_String) -- form lens-aeson -
danidiaz revised this gist
Feb 24, 2015 . 1 changed file with 6 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,3 @@ {-# LANGUAGE Rank2Types #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE UndecidableInstances #-} @@ -10,16 +7,16 @@ import Data.Reflection -- from reflection import Data.Monoid -- from base import Data.Proxy -- from tagged import Data.Text -- from text import Data.Monoid import Data.Aeson -- from aeson import Data.Aeson.Types (Parser) import Control.Applicative -- These only for constructing the example value import Control.Lens (preview) -- from lens import Data.Aeson.Lens (_Value,_String) -- form lens-aeson import System.IO -
danidiaz revised this gist
Feb 23, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -43,7 +43,7 @@ instance Reifies s (Object -> Parser a) => FromJSON (J a s) where parseJSON (Object v) = (fmap J . reflect (Proxy :: Proxy s)) v -- Convince the compiler that the phantom type in the proxy -- supplied by reify is the same as the phantom type in J. -- -- Otherwise the FromJSON instance for J won't kick in. asProxyJ :: Proxy s -> J a s -> J a s -
danidiaz revised this gist
Feb 23, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -63,7 +63,8 @@ main = do -- fromJSON uses the dynamically generated FromJSON instance let result = reify (fooParser prefix) $ \proxy -> -- We must eliminate the J newtype before returning -- because, thanks to parametricity, -- the phantom type cannot escape the callback. runJ . asProxyJ proxy <$> fromJSON exampleJSON putStrLn (show (result :: Result Foo)) -
danidiaz revised this gist
Feb 23, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -45,7 +45,7 @@ instance Reifies s (Object -> Parser a) => FromJSON (J a s) where -- Convince the compiler that the phantom type in the proxy -- is the same as the phantom type in J. -- -- Otherwise the FromJSON instance for J won't kick in. asProxyJ :: Proxy s -> J a s -> J a s asProxyJ _ = id -
danidiaz revised this gist
Feb 23, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,8 @@ fooParser prefix o = do newtype J a s = J { runJ :: a } -- If the phantom type s reifies the parsing function, we can -- use reflect to recover the function and implement -- our FromJSON instance for J. instance Reifies s (Object -> Parser a) => FromJSON (J a s) where parseJSON (Object v) = (fmap J . reflect (Proxy :: Proxy s)) v -
danidiaz revised this gist
Feb 23, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,7 @@ fooParser prefix o = do newtype J a s = J { runJ :: a } -- If the phantom type s reifies the parsing function, we can -- recover the function to implement our FromJSON instance for J. instance Reifies s (Object -> Parser a) => FromJSON (J a s) where parseJSON (Object v) = (fmap J . reflect (Proxy :: Proxy s)) v -
danidiaz revised this gist
Feb 23, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,7 @@ fooParser prefix o = do newtype J a s = J { runJ :: a } -- If the phantom type s reifies the parsing function, we can -- recover the function to implement our JSON instance for J. instance Reifies s (Object -> Parser a) => FromJSON (J a s) where parseJSON (Object v) = (fmap J . reflect (Proxy :: Proxy s)) v -
danidiaz revised this gist
Feb 23, 2015 . 1 changed file with 16 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -35,26 +35,34 @@ fooParser prefix o = do -- A wrapper over Foo carrying a phantom type s newtype J a s = J { runJ :: a } -- If the phantom type s reifies the parsing function, we can -- recover the function to implement our JSON instance. instance Reifies s (Object -> Parser a) => FromJSON (J a s) where parseJSON (Object v) = (fmap J . reflect (Proxy :: Proxy s)) v -- Convince the compiler that the phantom type in the proxy -- is the same as the phantom type in J. -- -- Otherwise our FromJSON instance won't kick in. asProxyJ :: Proxy s -> J a s -> J a s asProxyJ _ = id exampleJSON :: Value exampleJSON = maybe Null id (preview _Value str) where str = "{ \"zzfield1\" : 5, \"zzfield2\" : 7 }"::Text main :: IO () main = do putStrLn "Enter prefix for the fields: " -- better input "zz" or it won't parse! prefix <- fmap pack getLine -- fromJSON uses the dynamically generated FromJSON instance let result = reify (fooParser prefix) $ \proxy -> -- We must eliminate the J newtype before returning -- because the phantom type cannot escape the callback. runJ . asProxyJ proxy <$> fromJSON exampleJSON putStrLn (show (result :: Result Foo)) -
danidiaz revised this gist
Feb 22, 2015 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,6 @@ exampleJSON = maybe Null id (preview _Value str) where str = "{ \"zzfield1\" : 5, \"zzfield2\" : 7 }"::Text main :: IO () main = do putStrLn "Enter prefix for the fields: " -
danidiaz revised this gist
Feb 22, 2015 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -38,7 +38,6 @@ newtype J a s = J { runJ :: a } instance Reifies s (Object -> Parser a) => FromJSON (J a s) where parseJSON (Object v) = (fmap J . reflect (Proxy :: Proxy s)) v exampleJSON :: Value exampleJSON = maybe Null id (preview _Value str) -
danidiaz revised this gist
Feb 22, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -52,7 +52,7 @@ main = do -- better input "zz" or it won't parse! prefix <- fmap pack getLine -- fromJSON uses the dynamically generated FromJSON instance let result = reify (fooParser prefix) $ \proxy -> fmap runJ (fromJSON exampleJSON `asProxyOf` proxy) -
danidiaz created this gist
Feb 22, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,62 @@ -- Required packages: -- aeson reflection lens lens-aeson -- {-# LANGUAGE Rank2Types #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE OverloadedStrings #-} import Data.Reflection -- from reflection import Data.Monoid -- from base import Data.Proxy -- from tagged import Data.Text import Data.Monoid import Data.Aeson import Data.Aeson.Types (Parser) import Control.Applicative -- This only for constructing the example value import Control.Lens (preview) import Data.Aeson.Lens (_Value,_String) import System.IO data Foo = Foo { field1 :: Int , field2 :: Int } deriving (Show) fooParser :: Text -> Object -> Parser Foo fooParser prefix o = do Foo <$> o .: (prefix <> "field1") <*> o .: (prefix <> "field2") -- A wrapper over Foo carrying a phantom type s newtype J a s = J { runJ :: a } instance Reifies s (Object -> Parser a) => FromJSON (J a s) where parseJSON (Object v) = (fmap J . reflect (Proxy :: Proxy s)) v parseJSON _ = error "oops" exampleJSON :: Value exampleJSON = maybe Null id (preview _Value str) where str = "{ \"zzfield1\" : 5, \"zzfield2\" : 7 }"::Text main :: IO () main = do putStrLn "Enter prefix for the fields: " -- better input "zz" or it won't parse! prefix <- fmap pack getLine -- fromJSON uses the dynamically created FromJSON instance let result = reify (fooParser prefix) $ \proxy -> fmap runJ (fromJSON exampleJSON `asProxyOf` proxy) asProxyOf :: Result (J a s) -> Proxy s -> Result (J a s) asProxyOf r _ = r putStrLn (show (result :: Result Foo))