Skip to content

Instantly share code, notes, and snippets.

@cosmo0920
Last active December 22, 2015 01:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosmo0920/6397940 to your computer and use it in GitHub Desktop.
Save cosmo0920/6397940 to your computer and use it in GitHub Desktop.
Aeson-Lens
% ./dist/build/test-aeson-lens/test-aeson-lens
"127.0.0.1"
"27017"
-- 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
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
{"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