-
Notifications
You must be signed in to change notification settings - Fork 19
/
JsonParsing.hs
66 lines (59 loc) · 2.63 KB
/
JsonParsing.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module JsonParsing where
import Char
import Safe
import Text.ParserCombinators.Parsec.Error
import Data.JSON2
import Data.JSON2.Parser
import Data.JSON2.Query
import Control.Applicative ((<$>))
data GitHubKey = Key {
key :: String,
title :: String,
id :: Int
} deriving (Show)
-- printRequest :: String -> String -> IO()
-- printRequest "" jsonres = putStrLn resultat
-- where resultat = case parseJson jsonres of
-- Left e -> show e
-- Right p -> case getFromArr p of
-- [] -> pprint p
-- tab@(x:_) -> concat (fmap pprint tab)
-- r -> pprint r -- concat (pprint <$> p)
-- printRequest key jsonres = putStrLn ((map toUpper key) ++ ":" ++ "\n" ++ printedValues)
-- where printedValues = case parseJson jsonres of
-- Left e -> show e
-- Right p -> case (getFromArr p) of
-- [] -> pprint (getFromKey key p)
-- tab@(x:_) -> concat (displayValues . (getFromKey key) <$> tab)
-- r -> pprint (getFromKey key p)
getStrVal :: String -> String -> [String]
getStrVal key jsonres = case parseJson jsonres of
Left e -> [show e]
Right p
| ((getFromArr p) == []) && (key == "") -> [pprint p] -- pas tab, pas k
| ((getFromArr p) == []) -> pprint <$> (getFromKey key p) -- pas tab, k
| key == "" -> map (suppressApostroph . pprint) (getFromArr p) -- tab, pas k
| otherwise -> (suppressApostroph . pprint) <$> (concat ((getFromKey key) <$> tab)) --tab, k
where tab = getFromArr p
-- temporary solution until I master regex in Haskell
suppressApostroph :: String -> String
suppressApostroph s
| (head s == '\"') && (last s == '\"') = init (tail s)
| otherwise = s
jsonToKeys :: String -> [GitHubKey]
jsonToKeys jsonres = case parseJson jsonres of
Left e -> [Key "" "" (-1)]
Right p
| (isArr p) == [] -> [createAKeyFromJson p]
| otherwise -> map createAKeyFromJson (getFromArr p)
where createAKeyFromJson j = Key
(readValue "key" j)
(readValue "title" j)
(read (readValue "id" j)::Int)
where readValue s js = (getFromKey s js) >>= pprint
--where readValue = pprint . head . getFromKey
-- displayTuples :: [(String,Json)] -> String
-- displayTuples [] = ""
-- displayTuples ((k,v):ts) = (map toUpper k) ++ ":" ++ (pprint v) ++ "\n" ++ displayTuples ts
-- displayValues :: [Json] -> String
-- displayValues = foldr (\ x a -> (pprint x) ++ "\n" ++ a) ""