Last active
December 22, 2015 01:39
-
-
Save cosmo0920/6397940 to your computer and use it in GitHub Desktop.
Aeson-Lens
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
% ./dist/build/test-aeson-lens/test-aeson-lens | |
"127.0.0.1" | |
"27017" |
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
-- Initial test-aeson.cabal generated by cabal init. For further | |
-- documentation, see http://haskell.org/cabal/users-guide/ | |
name: test-aeson-lens | |
version: 0.1.0.0 | |
-- synopsis: | |
-- description: | |
license: MIT | |
-- license-file: LICENSE | |
author: cosmo0920 | |
-- maintainer: | |
-- copyright: | |
category: Text | |
build-type: Simple | |
cabal-version: >=1.8 | |
executable test-aeson-lens | |
main-is: test-aeson.hs | |
-- other-modules: | |
build-depends: base >=4.5 && < 5 | |
, aeson >= 0.6 | |
, lens >= 3.9 && < 4 | |
, aeson-lens >= 0.5 | |
, text >= 0.11 | |
, bytestring >= 0.9 | |
extensions: OverloadedStrings |
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
import Data.Aeson (decode, Value) | |
import Data.Maybe | |
import qualified Data.ByteString.Lazy as B | |
import qualified Data.Text as T | |
import Control.Lens | |
import Data.Aeson.Lens | |
main :: IO () | |
main = do | |
fstr <- B.readFile "test.json" | |
let v = decode fstr :: Maybe Value | |
let hostVal = v ^. key (T.pack "host") :: Maybe String | |
portVal = v ^. key (T.pack "port") :: Maybe String | |
putStrLn $ show $ fromJust hostVal | |
putStrLn $ show $ fromJust portVal |
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
{"host":"127.0.0.1","port":"27017"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment