Portability | portable |
---|---|
Stability | development |
Maintainer | mariusz@scrive.com |
Safe Haskell | None |
Interface for extracting data from JSValue.
- class FromJSValue a where
- fromJSValue :: JSValue -> Maybe a
- fromJSValueM :: (JSValueContainer c, MonadReader c m) => m (Maybe a)
- class FromJSValueWithUpdate a where
- fromJSValueWithUpdate :: Maybe a -> JSValue -> Maybe a
- fromJSValueWithUpdateM :: (JSValueContainer c, MonadReader c m) => Maybe a -> m (Maybe a)
- class MatchWithJSValue a where
- matchesWithJSValue :: a -> JSValue -> Bool
- matchesWithJSValueM :: (JSValueContainer c, MonadReader c m) => a -> m Bool
- jsValueField :: (JSValueContainer c, MonadReader c m, FromJSValue a) => String -> m (Maybe (Maybe a))
- fromJSValueField :: (JSValueContainer c, MonadReader c m, FromJSValue a) => String -> m (Maybe a)
- fromJSValueFieldBase64 :: (JSValueContainer c, MonadReader c m) => String -> m (Maybe ByteString)
- fromJSValueFieldCustom :: (JSValueContainer c, MonadReader c m) => String -> m (Maybe a) -> m (Maybe a)
- fromJSValueCustomMany :: (JSValueContainer c, MonadReader c m) => m (Maybe a) -> m (Maybe [a])
- fromJSValueCustomList :: (JSValueContainer c, MonadReader c m) => [m (Maybe a)] -> m (Maybe [a])
- fromJSValueManyWithUpdate :: (JSValueContainer c, MonadReader c m, FromJSValueWithUpdate a, MatchWithJSValue a) => [a] -> m (Maybe [a])
- withJSValue :: Monad m => JSValue -> ReaderT JSValue m a -> m a
Basic Parsing
class FromJSValue a whereSource
Structures that can be parsed
from JSON. Instances must declare
either fromJSValue
(parse directly from JSValue
) or
fromJSValueM
(uses MonadReader
)
fromJSValue :: JSValue -> Maybe aSource
fromJSValueM :: (JSValueContainer c, MonadReader c m) => m (Maybe a)Source
FromJSValue Bool | |
FromJSValue Double | |
FromJSValue Float | |
FromJSValue Int | |
FromJSValue Integer | |
FromJSValue String | |
FromJSValue ByteString | |
FromJSValue JSValue | |
FromJSValue a => FromJSValue [a] | |
FromJSValue a => FromJSValue (Maybe a) | Parsing any Maybe always returns Just |
(FromJSValue a, FromJSValue b) => FromJSValue (a, b) | |
(FromJSValue a, FromJSValue b, FromJSValue c) => FromJSValue (a, b, c) | |
(FromJSValue a, FromJSValue b, FromJSValue c, FromJSValue d) => FromJSValue (a, b, c, d) | |
(FromJSValue a, FromJSValue b, FromJSValue c, FromJSValue d, FromJSValue e) => FromJSValue (a, b, c, d, e) | |
(FromJSValue a, FromJSValue b, FromJSValue c, FromJSValue d, FromJSValue e, FromJSValue f) => FromJSValue (a, b, c, d, e, f) |
class FromJSValueWithUpdate a whereSource
Structures that can be parsed
from JSON if some structure for
update is provided
fromJSValueWithUpdate :: Maybe a -> JSValue -> Maybe aSource
fromJSValueWithUpdateM :: (JSValueContainer c, MonadReader c m) => Maybe a -> m (Maybe a)Source
class MatchWithJSValue a whereSource
Structures that can be matched with JSValue
matchesWithJSValue :: a -> JSValue -> BoolSource
matchesWithJSValueM :: (JSValueContainer c, MonadReader c m) => a -> m BoolSource
Data Extraction
jsValueField :: (JSValueContainer c, MonadReader c m, FromJSValue a) => String -> m (Maybe (Maybe a))Source
Reading the value that is on some field. Returns Nothing
if
JSON is not an object or field is present but cannot be parsed,
'Just Nothing' if absent, and 'Just (Just a)' otherwise
fromJSValueField :: (JSValueContainer c, MonadReader c m, FromJSValue a) => String -> m (Maybe a)Source
Reading the value that is on a field. Semantics are a bit involved, example GHCi session should clarify:
Prelude> :set -XNoMonomorphismRestriction Prelude> let x = withJSValue (JSObject (toJSObject [(key,JSString $ toJSString value)])) Prelude> x (fromJSValueField key) :: IO (Maybe Int) Nothing Prelude> x (fromJSValueField key) :: IO (Maybe (Maybe Int)) Just Nothing Prelude> x (fromJSValueField key) :: IO (Maybe (Maybe (Maybe Int))) Just (Just Nothing) Prelude> x (fromJSValueField key) :: IO (Maybe String) Just value Prelude> x (fromJSValueField key) :: IO (Maybe (Maybe String)) Just (Just value) Prelude> x (fromJSValueField key) :: IO (Maybe (Maybe (Maybe String))) Just (Just (Just value)) Prelude> let x = withJSValue (JSArray []) Prelude> x (fromJSValueField key) :: IO (Maybe String) Nothing Prelude> x (fromJSValueField key) :: IO (Maybe (Maybe String)) Nothing Prelude> x (fromJSValueField key) :: IO (Maybe (Maybe (Maybe String))) Nothing
fromJSValueFieldBase64 :: (JSValueContainer c, MonadReader c m) => String -> m (Maybe ByteString)Source
Version of fromJSValueField
for Base64 encoded data to avoid
memory leak.
fromJSValueFieldCustom :: (JSValueContainer c, MonadReader c m) => String -> m (Maybe a) -> m (Maybe a)Source
Generalization of fromJSValueField
. Does not use FromJSValue
instances.
fromJSValueCustomMany :: (JSValueContainer c, MonadReader c m) => m (Maybe a) -> m (Maybe [a])Source
Runs parser on each element of underlaying json. Returns Just iff JSON is array.
fromJSValueCustomList :: (JSValueContainer c, MonadReader c m) => [m (Maybe a)] -> m (Maybe [a])Source
Generalization of fromJSValueCustomMany
, where each element of
array can have different parser.
fromJSValueManyWithUpdate :: (JSValueContainer c, MonadReader c m, FromJSValueWithUpdate a, MatchWithJSValue a) => [a] -> m (Maybe [a])Source
Runs parser on each element of underlying json. Returns Just
iff
JSON is an array.
Note: This method has quadratic complexity. It is better to write less general matching algorithms that use Maps.