commit e11d6f11e485154fc73b9ac5cd54e3eabc52064c Author: Rnhmjoj Date: Thu Nov 20 21:39:25 2014 +0100 Initial commit diff --git a/Json.hs b/Json.hs new file mode 100644 index 0000000..941663b --- /dev/null +++ b/Json.hs @@ -0,0 +1,34 @@ +module Json where + +import Data.Aeson +import Data.Aeson.Types (parse) +import Data.Text (Text, unpack) +import Data.List (intercalate) + +import qualified Data.Vector as V +import qualified Data.HashMap.Strict as H + +-- Get the JSON value of a key +(|.) :: Object -> Text -> Value +obj |. key = case parse (.: key) obj of + Success val -> val + Error err -> toJSON err + +-- Get the String value of a key +(|:) :: Object -> Text -> String +obj |: key = repr (obj |. key) 0 + +-- Create a String representation of a JSON Value +repr :: Value -> Int -> String +repr val lev = + case val of + Array x -> intercalate ", " $ mapl (\i -> repr i lev) x + Object x -> concat $ map (dump x) $ H.keys x + String x -> unpack x + Number x -> show x + Bool x -> show x + Null -> "null" + where + indent = '\n' : (concat . replicate lev) " " + dump o k = concat [indent, unpack k, ": ", repr (o |. k) (lev+1)] + mapl f v = V.toList $ V.map f v diff --git a/Main.hs b/Main.hs new file mode 100644 index 0000000..689e7dd --- /dev/null +++ b/Main.hs @@ -0,0 +1,10 @@ +import Network.Wreq +import Control.Lens +import Data.Aeson +import Json + +main = do + req <- get "http://namecoin.dns:8088/id/rnhmjoj" + case decode (req ^. responseBody) of + Just res -> putStrLn $ repr res 0 + Nothing -> putStrLn "Error parsing data"