module Text.JSON.FromJSValue
(
FromJSValue(..)
, FromJSValueWithUpdate(..)
, MatchWithJSValue(..)
, jsValueField
, fromJSValueField
, fromJSValueFieldBase64
, fromJSValueFieldCustom
, fromJSValueCustomMany
, fromJSValueCustomList
, fromJSValueManyWithUpdate
, withJSValue
)
where
import Text.JSON
import Control.Monad
import Control.Monad.Reader
import qualified Data.ByteString.UTF8 as BS
import qualified Data.ByteString.Base64 as BASE64
import Control.Applicative
import Control.Monad.Identity
import Data.Int
import Data.List
class FromJSValue a where
fromJSValue :: JSValue -> Maybe a
fromJSValue j = runIdentity $ withJSValue j $ liftM fromJSValueM ask
fromJSValueM :: (MonadReader JSValue m) => m (Maybe a)
fromJSValueM = liftM fromJSValue ask
class FromJSValueWithUpdate a where
fromJSValueWithUpdate :: Maybe a -> JSValue -> Maybe a
fromJSValueWithUpdate ma j = runIdentity $ withJSValue j $ liftM (fromJSValueWithUpdateM ma) ask
fromJSValueWithUpdateM :: (MonadReader JSValue m) => Maybe a -> m (Maybe a)
fromJSValueWithUpdateM ma = liftM (fromJSValueWithUpdate ma) ask
class MatchWithJSValue a where
matchesWithJSValue :: a -> JSValue -> Bool
matchesWithJSValue a j = runIdentity $ withJSValue j $ liftM (matchesWithJSValueM a) ask
matchesWithJSValueM :: (MonadReader JSValue m) => a -> m Bool
matchesWithJSValueM a = liftM (matchesWithJSValue a) ask
instance FromJSValue JSValue where
fromJSValue = Just
instance FromJSValue String where
fromJSValue (JSString string) = Just $ fromJSString string
fromJSValue _ = Nothing
instance FromJSValue BS.ByteString where
fromJSValue s = fmap BS.fromString (fromJSValue s)
instance FromJSValue Integer where
fromJSValue (JSRational _ r) = Just $ round r
fromJSValue _ = Nothing
instance FromJSValue Int where
fromJSValue j = liftM fromIntegral (fromJSValue j :: Maybe Integer)
instance FromJSValue Int16 where
fromJSValue j = fromIntegral <$> (fromJSValue j :: Maybe Integer)
instance FromJSValue Int32 where
fromJSValue j = fromIntegral <$> (fromJSValue j :: Maybe Integer)
instance FromJSValue Int64 where
fromJSValue j = fromIntegral <$> (fromJSValue j :: Maybe Integer)
instance FromJSValue Bool where
fromJSValue (JSBool v) = Just $ v
fromJSValue _ = Nothing
instance FromJSValue Double where
fromJSValue (JSRational _ r) = Just $ fromRational r
fromJSValue _ = Nothing
instance FromJSValue Float where
fromJSValue (JSRational _ r) = Just $ fromRational r
fromJSValue _ = Nothing
instance (FromJSValue a) => FromJSValue [a] where
fromJSValue (JSArray list) = mapM fromJSValue list
fromJSValue _ = Nothing
instance (FromJSValue a) => FromJSValue (Maybe a) where
fromJSValue = Just . fromJSValue
instance (FromJSValue a, FromJSValue b) => FromJSValue (a,b) where
fromJSValue (JSArray [a,b]) = do
a' <- fromJSValue a
b' <- fromJSValue b
return (a',b')
fromJSValue _ = Nothing
instance (FromJSValue a, FromJSValue b, FromJSValue c) => FromJSValue (a,b,c) where
fromJSValue (JSArray [a,b,c]) = do
a' <- fromJSValue a
b' <- fromJSValue b
c' <- fromJSValue c
return (a',b',c')
fromJSValue _ = Nothing
instance (FromJSValue a, FromJSValue b, FromJSValue c, FromJSValue d) => FromJSValue (a,b,c,d) where
fromJSValue (JSArray [a,b,c,d]) = do
a' <- fromJSValue a
b' <- fromJSValue b
c' <- fromJSValue c
d' <- fromJSValue d
return (a',b',c',d')
fromJSValue _ = Nothing
instance (FromJSValue a, FromJSValue b, FromJSValue c,
FromJSValue d, FromJSValue e) => FromJSValue (a,b,c,d,e) where
fromJSValue (JSArray [a,b,c,d,e]) = do
a' <- fromJSValue a
b' <- fromJSValue b
c' <- fromJSValue c
d' <- fromJSValue d
e' <- fromJSValue e
return (a',b',c',d',e')
fromJSValue _ = Nothing
instance (FromJSValue a, FromJSValue b, FromJSValue c,
FromJSValue d, FromJSValue e, FromJSValue f) => FromJSValue (a,b,c,d,e,f) where
fromJSValue (JSArray [a,b,c,d,e,f]) = do
a' <- fromJSValue a
b' <- fromJSValue b
c' <- fromJSValue c
d' <- fromJSValue d
e' <- fromJSValue e
f' <- fromJSValue f
return (a',b',c',d',e',f')
fromJSValue _ = Nothing
jsValueField :: (MonadReader JSValue m, FromJSValue a) => String -> m (Maybe (Maybe a))
jsValueField s = ask >>= fromObject
where
fromObject (JSObject object) =
case lookup s (fromJSObject object) of
Nothing -> return (Just Nothing)
Just a -> return (Just `fmap` fromJSValue a)
fromObject _ = return Nothing
fromJSValueField :: (MonadReader JSValue m, FromJSValue a) => String -> m (Maybe a)
fromJSValueField s = liftM fromObject ask
where
fromObject (JSObject object) = join (fmap fromJSValue (lookup s $ fromJSObject object))
fromObject _ = Nothing
fromJSValueFieldBase64 :: (MonadReader JSValue m) => String -> m (Maybe BS.ByteString)
fromJSValueFieldBase64 s = liftM dc (fromJSValueField s)
where dc s' = case fmap BASE64.decode s' of
Just (Right r) -> Just r
_ -> Nothing
fromJSValueFieldCustom :: (MonadReader JSValue m) => String -> m (Maybe a) -> m (Maybe a)
fromJSValueFieldCustom s digger = do
mobj <- fromJSValueField s
case mobj of
Just obj -> local (const obj) (digger)
Nothing -> return Nothing
fromJSValueCustomMany :: (MonadReader JSValue m) => m (Maybe a) -> m (Maybe [a])
fromJSValueCustomMany digger = fromJSValueCustomList (repeat digger)
fromJSValueCustomList :: (MonadReader JSValue m) => [m (Maybe a)] -> m (Maybe [a])
fromJSValueCustomList diggers = do
mlist <- fromJSValueM
case mlist of
Nothing -> return Nothing
Just list -> runDiggers list diggers
where
runDiggers (j:js) (d:ds) = do
mres <- local (const j) d
case mres of
Just res -> do
mress <- runDiggers js ds
case mress of
Just ress -> return $ Just (res:ress)
_ -> return Nothing
_ -> return Nothing
runDiggers _ _ = return $ Just []
fromJSValueManyWithUpdate :: (MonadReader JSValue m, FromJSValueWithUpdate a, MatchWithJSValue a) => [a] -> m (Maybe [a])
fromJSValueManyWithUpdate values = do
mjs <- fromJSValueM
case mjs of
Nothing -> return Nothing
Just js -> runFromJSValueAndUpdate js
where
runFromJSValueAndUpdate (j:js) = do
mres <- local (const j) (fromJSValueWithUpdateM (find (\v -> matchesWithJSValue v j) values))
case mres of
Just res -> do
mress <- runFromJSValueAndUpdate js
case mress of
Just ress -> return $ Just (res:ress)
_ -> return Nothing
_ -> return Nothing
runFromJSValueAndUpdate [] = return $ Just []
withJSValue :: (Monad m) => JSValue -> ReaderT JSValue m a -> m a
withJSValue j a = runReaderT a j