lab01: impl json
This commit is contained in:
49
lab01/JSON.hs
Normal file
49
lab01/JSON.hs
Normal file
@@ -0,0 +1,49 @@
|
||||
---SOLUTION
|
||||
-- A JSON library written in Haskell, adapted from an example
|
||||
-- from "Real World Haskell".
|
||||
|
||||
-- Note that the module name must match the name of the file.
|
||||
module JSON (
|
||||
JValue(..), --The (..) means that we are exporting the type and all of its constructors
|
||||
toString,
|
||||
isNull,
|
||||
jsonRightTriangles)
|
||||
where
|
||||
|
||||
data JValue = JString String
|
||||
| JNumber Double
|
||||
| JBool Bool
|
||||
| JNull
|
||||
| JObject [(String, JValue)]
|
||||
| JArray [JValue]
|
||||
deriving (Eq, Ord, Show)
|
||||
|
||||
|
||||
--The type annotation indicates that the function takes a JValue and returns a String
|
||||
toString :: JValue -> String
|
||||
toString (JString s) = "\"" ++ s ++ "\""
|
||||
toString (JNumber n) = show n
|
||||
toString (JBool True) = "true"
|
||||
toString (JBool False) = "false"
|
||||
toString JNull = "null"
|
||||
toString (JArray lst) = "[" ++ list2str lst ++ "]"
|
||||
toString (JObject obj) = "{\n" ++ obj2str obj ++ "\n}"
|
||||
|
||||
list2str :: [JValue] -> String
|
||||
list2str [] = ""
|
||||
list2str (x:[]) = (toString x)
|
||||
list2str (x:xs) = (toString x) ++ ",\n " ++ (list2str xs)
|
||||
|
||||
obj2str :: [(String, JValue)] -> String
|
||||
obj2str [] = ""
|
||||
obj2str ((k,v):[]) = k ++ ":" ++ toString v
|
||||
obj2str ((k,v):xs) = k ++ ":" ++ toString v ++ "," ++ obj2str xs
|
||||
|
||||
isNull JNull = True
|
||||
isNull _ = False
|
||||
|
||||
jsonRightTriangles = JArray [ JObject [("a",JNumber a), ("b",JNumber b), ("c",JNumber c)] |
|
||||
a<-[1..10],
|
||||
b<-[1..10],
|
||||
c<-[1..10],
|
||||
a^2 + b^2 == c^2]
|
||||
Reference in New Issue
Block a user