module Database.CouchDB.JSON
( jsonString
, jsonInt
, jsonObject
, jsonField
, jsonBool
, jsonIsTrue
) where
import Text.JSON
import Data.Ratio (numerator,denominator)
jsonString :: JSValue -> Result String
jsonString (JSString s) = return (fromJSString s)
jsonString _ = fail "expected a string"
jsonInt :: (Integral n) => JSValue -> Result n
jsonInt (JSRational _ r) = case (numerator r, denominator r) of
(n,1) -> return (fromIntegral n)
otherwise -> fail "expected an integer; got a rational"
jsonInt _ = fail "expected an integer"
jsonObject :: JSValue -> Result [(String,JSValue)]
jsonObject (JSObject obj) = return (fromJSObject obj)
jsonObject v = fail $ "expected an object, got " ++ (show v)
jsonBool :: JSValue -> Result Bool
jsonBool (JSBool b) = return b
jsonBool v = fail $ "expected a boolean value, got " ++ show v
jsonField :: JSON a => String -> [(String,JSValue)] -> Result a
jsonField field obj = case lookup field obj of
Just v -> readJSON v
Nothing -> fail $ "could not find the field " ++ field
jsonIsTrue :: String -> [(String,JSValue)] -> Result Bool
jsonIsTrue field obj = case lookup field obj of
Just (JSBool True) -> return True
otherwise -> return False