Last active
January 25, 2016 04:19
-
-
Save hlian/1d294a0e87736d5aaf26 to your computer and use it in GitHub Desktop.
use JSON files as if they were Haskell values / live life maximally with simple language semantics
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 characters
{ | |
"hello": "world", | |
"this": { | |
"can": { | |
"be": "nested" | |
} | |
} | |
} |
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 characters
#!/usr/bin/env stack | |
-- stack --resolver lts-4.2 --install-ghc runghc --package file-embed --package aeson --package lens --package lens-aeson | |
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE TemplateHaskell #-} | |
module JSONBaby where | |
import Control.Lens | |
import Data.Aeson | |
import Data.Aeson.Lens | |
import Data.FileEmbed | |
value :: Maybe Value | |
value = (decode . view lazy) $(embedFile "a.json") | |
main :: IO () | |
main = do | |
print value | |
print (value ^? _Just . key "hello") | |
print (value ^? _Just . key "this" . key "can" . key "be") |
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 characters
Just (Object (fromList [("hello",String "world"),("this",Object (fromList [("can",Object (fromList [("be",String "nested")]))]))])) | |
Just (String "world") | |
Just (String "nested") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment