aboutsummaryrefslogtreecommitdiff
path: root/ch05/SimpleJSON.hs
diff options
context:
space:
mode:
Diffstat (limited to 'ch05/SimpleJSON.hs')
-rw-r--r--ch05/SimpleJSON.hs48
1 files changed, 48 insertions, 0 deletions
diff --git a/ch05/SimpleJSON.hs b/ch05/SimpleJSON.hs
new file mode 100644
index 0000000..d692168
--- /dev/null
+++ b/ch05/SimpleJSON.hs
@@ -0,0 +1,48 @@
+{-- snippet module --}
+module SimpleJSON
+ (
+ JValue(..)
+ , getString
+ , getInt
+ , getDouble
+ , getBool
+ , getObject
+ , getArray
+ , isNull
+ ) where
+{-- /snippet module --}
+
+{-- snippet JValue --}
+data JValue = JString String
+ | JNumber Double
+ | JBool Bool
+ | JNull
+ | JObject [(String, JValue)]
+ | JArray [JValue]
+ deriving (Eq, Ord, Show)
+{-- /snippet JValue --}
+
+{-- snippet getString --}
+getString :: JValue -> Maybe String
+getString (JString s) = Just s
+getString _ = Nothing
+{-- /snippet getString --}
+
+{-- snippet getters --}
+getInt (JNumber n) = Just (truncate n)
+getInt _ = Nothing
+
+getDouble (JNumber n) = Just n
+getDouble _ = Nothing
+
+getBool (JBool b) = Just b
+getBool _ = Nothing
+
+getObject (JObject o) = Just o
+getObject _ = Nothing
+
+getArray (JArray a) = Just a
+getArray _ = Nothing
+
+isNull v = v == JNull
+{-- /snippet getters --}