aboutsummaryrefslogtreecommitdiff
path: root/ch05/SimpleJSON.hs
diff options
context:
space:
mode:
authorJan Sucan <jan@jansucan.com>2023-03-28 17:09:27 +0200
committerJan Sucan <jan@jansucan.com>2023-04-10 16:52:19 +0200
commit2c4e32dbcd6a74a93d6121212c5a44461160a38f (patch)
tree2640fd4b4218787bb7b7ac93f52b63933b4da38a /ch05/SimpleJSON.hs
parent742195819304c1e009fa20a8c6f387f61d1975fd (diff)
ch05: Copy needed files from the examples
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 --}