From 2c4e32dbcd6a74a93d6121212c5a44461160a38f Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Tue, 28 Mar 2023 17:09:27 +0200 Subject: ch05: Copy needed files from the examples --- ch05/SimpleJSON.hs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 ch05/SimpleJSON.hs (limited to 'ch05/SimpleJSON.hs') 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 --} -- cgit v1.2.3