From 7dabac6cbc1703d67ad6f65817a8cc510ff4be5e Mon Sep 17 00:00:00 2001 From: Rnhmjoj Date: Sat, 27 Dec 2014 16:34:29 +0100 Subject: [PATCH] Refactor JSON utilities module --- Json.hs | 46 ++++++++++++++++++---------------------------- Main.hs | 4 ++-- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/Json.hs b/Json.hs index 5d908e7..fab4612 100644 --- a/Json.hs +++ b/Json.hs @@ -1,9 +1,4 @@ -module Json -( (|.) -, (|:) -, repr -, jprint -) where +module Json where import Data.Aeson import Data.Aeson.Types (parse) @@ -20,29 +15,24 @@ obj |. key = case parse (.: key) obj of Error err -> toJSON err -- Get the String value of a key - (|:) :: Object -> Text -> String -obj |: key = repr' (obj |. key) 0 +obj |: key = repr (obj |. key) --- Create a String representation of a JSON +-- Create a String representation of a JSON value repr :: Value -> String -repr obj = drop 1 $ repr' obj 0 +repr obj = repr' obj 0 where + repr' val lev = + case val of + Array x -> intercalate ", " $ mapl (\i -> repr' i lev) x + Object x -> drop 1 $ concat $ map (dump x lev) $ H.keys x + String x -> unpack x + Number x -> show x + Bool x -> show x + Null -> "null" + mapl f v = V.toList $ V.map f v + indent l = '\n' : (concat . replicate l) " " + dump o l k = concat [indent l, unpack k, ": ", repr' (o |. k) (l+1)] --- 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 - --- Print a representation of a JSON Value -jprint :: Value -> IO () -jprint = putStrLn . repr \ No newline at end of file +-- Pretty print a JSON value +pprint :: Value -> IO () +pprint = putStrLn . repr \ No newline at end of file diff --git a/Main.hs b/Main.hs index c77719e..9fa26d1 100644 --- a/Main.hs +++ b/Main.hs @@ -54,8 +54,8 @@ handleLocal name block = do out <- readProcess "namecoind" ["name_show", name] "" case decode (C.pack out) of Just res -> do - jprint $ reparse (res |: "value") - if block then jprint extra else return () + pprint $ reparse (res |: "value") + if block then pprint extra else return () where reparse = fromJust . decode . C.pack extra = toJSON $ delete "value" res