-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonParser.hs
54 lines (37 loc) · 1.1 KB
/
JsonParser.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
module JsonParser (
parse
) where
import Prelude hiding (null)
import qualified Prelude (null)
import Control.Applicative
import Data.Char
import Json
import Parser hiding (string, number, boolean)
import qualified Parser (string, number, boolean)
instance Read Json.Value where
readsPrec _ str = case run value str of
Nothing -> []
Just a -> [a]
parse str = do
result <- run value str
return(fst result)
value = ws *> (null <|> boolean <|> number <|> string <|> array <|> object)
null = Parser.parserOfString "null" *> pure Null
boolean = Boolean <$> Parser.boolean
number = Number <$> Parser.number
string = String <$> Parser.string
array = parserOfChar '[' *> ws
*> (Array <$> elements)
<* ws <* parserOfChar ']'
where
elements = sepBy (ws *> sep <* ws) value
where
sep = parserOfChar ','
object = parserOfChar '{' *> ws
*> (Object <$> members)
<* ws <* parserOfChar '}'
where
members = sepBy (ws *> sep <* ws) member
where
sep = parserOfChar ','
member = (,) <$> (Parser.string <* ws <* parserOfChar ':') <*> (ws *> value)