Safe Haskell | None |
---|
JSON utility functions.
Synopsis
- type JSField = (String, JSValue)
- type JSRecord = [JSField]
- readJSONWithDesc :: JSON a => String -> JSValue -> Result a
- fromJResult :: (Monad m, MonadFail m) => String -> Result a -> m a
- fromJResultE :: (Error e, MonadError e m) => String -> Result a -> m a
- readEitherString :: MonadFail m => JSValue -> m String
- loadJSArray :: MonadFail m => String -> String -> m [JSObject JSValue]
- buildNoKeyError :: JSRecord -> String -> String
- fromObj :: (JSON a, MonadFail m) => JSRecord -> String -> m a
- maybeFromObj :: (JSON a, MonadFail m) => JSRecord -> String -> m (Maybe a)
- fromObjWithDefault :: (JSON a, MonadFail m) => JSRecord -> String -> a -> m a
- arrayMaybeFromJVal :: (JSON a, Monad m, MonadFail m) => JSValue -> m [Maybe a]
- arrayMaybeFromObj :: (JSON a, MonadFail m) => JSRecord -> String -> m [Maybe a]
- tryArrayMaybeFromObj :: JSON a => String -> JSRecord -> String -> Result [Maybe a]
- fromKeyValue :: (JSON a, MonadFail m) => String -> JSValue -> m a
- fromJVal :: (Monad m, MonadFail m, JSON a) => JSValue -> m a
- fromJValE :: (Error e, MonadError e m, JSON a) => JSValue -> m a
- jsonHead :: JSON b => [a] -> (a -> b) -> JSValue
- getMaybeJsonHead :: JSON b => [a] -> (a -> Maybe b) -> JSValue
- getMaybeJsonElem :: JSON b => [a] -> Int -> (a -> Maybe b) -> JSValue
- asJSObject :: (Monad m, MonadFail m) => JSValue -> m (JSObject JSValue)
- asObjectList :: (Monad m, MonadFail m) => [JSValue] -> m [JSObject JSValue]
- tryFromObj :: JSON a => String -> JSRecord -> String -> Result a
- toArray :: (Monad m, MonadFail m) => JSValue -> m [JSValue]
- optionalJSField :: JSON a => String -> Maybe a -> Maybe JSField
- optFieldsToObj :: [Maybe JSField] -> JSValue
- class HasStringRepr a where
- fromStringRepr :: MonadFail m => String -> m a
- toStringRepr :: a -> String
- newtype GenericContainer a b = GenericContainer {
- fromContainer :: Map a b
- emptyContainer :: GenericContainer a b
- type Container = GenericContainer ByteString
- containerFromList :: Ord a => [(a, b)] -> GenericContainer a b
- lookupContainer :: (Monad m, Ord a) => m b -> a -> GenericContainer a b -> m b
- alterContainerL :: (Functor f, Ord a) => a -> (Maybe b -> f (Maybe b)) -> GenericContainer a b -> f (GenericContainer a b)
- readContainer :: (MonadFail m, HasStringRepr a, Ord a, JSON b) => JSObject JSValue -> m (GenericContainer a b)
- showContainer :: (HasStringRepr a, JSON b) => GenericContainer a b -> JSValue
- newtype UsedKeys = UsedKeys (Maybe (Set Text))
- mkUsedKeys :: Set Text -> UsedKeys
- allUsedKeys :: UsedKeys
- class DictObject a where
- toDict :: a -> [(String, JSValue)]
- fromDictWKeys :: [(String, JSValue)] -> WriterT UsedKeys Result a
- fromDict :: [(String, JSValue)] -> Result a
- showJSONtoDict :: DictObject a => a -> JSValue
- readJSONfromDict :: DictObject a => JSValue -> Result a
- class ArrayObject a where
- toJSArray :: a -> [JSValue]
- fromJSArray :: [JSValue] -> Result a
- newtype MaybeForJSON a = MaybeForJSON {
- unMaybeForJSON :: Maybe a
- newtype TimeAsDoubleJSON = TimeAsDoubleJSON {
- unTimeAsDoubleJSON :: ClockTime
- newtype Tuple5 a b c d e = Tuple5 {
- unTuple5 :: (a, b, c, d, e)
- nestedAccessByKey :: [String] -> JSValue -> Result JSValue
- nestedAccessByKeyDotted :: String -> JSValue -> Result JSValue
- branchOnField :: String -> (JSValue -> Result a) -> (JSValue -> Result a) -> JSValue -> Result a
- addField :: (String, JSValue) -> JSValue -> JSValue
- maybeParseMap :: JSON a => JSValue -> Maybe (Map String a)
JSON-related functions
:: JSON a | |
=> String | description of |
-> JSValue | input value |
-> Result a |
Annotate readJSON
error messages with descriptions of what
is being parsed into what.
fromJResult :: (Monad m, MonadFail m) => String -> Result a -> m a Source #
Converts a JSON Result into a monadic value.
fromJResultE :: (Error e, MonadError e m) => String -> Result a -> m a Source #
Converts a JSON Result into a MonadError value.
readEitherString :: MonadFail m => JSValue -> m String Source #
Tries to read a string from a JSON value.
In case the value was not a string, we fail the read (in the context of the current monad.
:: MonadFail m | |
=> String | Operation description (for error reporting) |
-> String | Input message |
-> m [JSObject JSValue] |
Converts a JSON message into an array of JSON objects.
buildNoKeyError :: JSRecord -> String -> String Source #
Helper function for missing-key errors
fromObj :: (JSON a, MonadFail m) => JSRecord -> String -> m a Source #
Reads the value of a key in a JSON object.
maybeFromObj :: (JSON a, MonadFail m) => JSRecord -> String -> m (Maybe a) Source #
Reads the value of an optional key in a JSON object. Missing
keys, or keys that have a 'null' value, will be returned as
Nothing
, otherwise we attempt deserialisation and return a Just
value.
fromObjWithDefault :: (JSON a, MonadFail m) => JSRecord -> String -> a -> m a Source #
Reads the value of a key in a JSON object with a default if missing. Note that both missing keys and keys with value 'null' will cause the default value to be returned.
arrayMaybeFromJVal :: (JSON a, Monad m, MonadFail m) => JSValue -> m [Maybe a] Source #
arrayMaybeFromObj :: (JSON a, MonadFail m) => JSRecord -> String -> m [Maybe a] Source #
Reads an array of optional items
:: JSON a | |
=> String | Textual "owner" in error messages |
-> JSRecord | The object array |
-> String | The desired key from the object |
-> Result [Maybe a] |
Wrapper for arrayMaybeFromObj with better diagnostic
:: (JSON a, MonadFail m) | |
=> String | The key name |
-> JSValue | The value to read |
-> m a |
Reads a JValue, that originated from an object key.
fromJValE :: (Error e, MonadError e m, JSON a) => JSValue -> m a Source #
Small wrapper over readJSON
for MonadError
.
jsonHead :: JSON b => [a] -> (a -> b) -> JSValue Source #
Helper function that returns Null or first element of the list.
getMaybeJsonHead :: JSON b => [a] -> (a -> Maybe b) -> JSValue Source #
Helper for extracting Maybe values from a possibly empty list.
getMaybeJsonElem :: JSON b => [a] -> Int -> (a -> Maybe b) -> JSValue Source #
Helper for extracting Maybe values from a list that might be too short.
asJSObject :: (Monad m, MonadFail m) => JSValue -> m (JSObject JSValue) Source #
Converts a JSON value into a JSON object.
asObjectList :: (Monad m, MonadFail m) => [JSValue] -> m [JSObject JSValue] Source #
Coneverts a list of JSON values into a list of JSON objects.
:: JSON a | |
=> String | Textual "owner" in error messages |
-> JSRecord | The object array |
-> String | The desired key from the object |
-> Result a |
Try to extract a key from an object with better error reporting than fromObj.
toArray :: (Monad m, MonadFail m) => JSValue -> m [JSValue] Source #
Ensure a given JSValue is actually a JSArray.
optionalJSField :: JSON a => String -> Maybe a -> Maybe JSField Source #
Creates a Maybe JSField. If the value string is Nothing, the JSField will be Nothing as well.
optFieldsToObj :: [Maybe JSField] -> JSValue Source #
Creates an object with all the non-Nothing fields of the given list.
Container type (special type for JSON serialisation)
class HasStringRepr a where Source #
Class of types that can be converted from Strings. This is
similar to the Read
class, but it's using a different
serialisation format, so we have to define a separate class. Mostly
useful for custom key types in JSON dictionaries, which have to be
backed by strings.
fromStringRepr :: MonadFail m => String -> m a Source #
toStringRepr :: a -> String Source #
Instances
HasStringRepr String # | Trivial instance |
Defined in Ganeti.JSON fromStringRepr :: MonadFail m => String -> m String Source # toStringRepr :: String -> String Source # | |
HasStringRepr ByteString # | |
Defined in Ganeti.JSON fromStringRepr :: MonadFail m => String -> m ByteString Source # toStringRepr :: ByteString -> String Source # | |
HasStringRepr DiskTemplate # | |
Defined in Ganeti.Types fromStringRepr :: MonadFail m => String -> m DiskTemplate Source # toStringRepr :: DiskTemplate -> String Source # | |
HasStringRepr Hypervisor # | |
Defined in Ganeti.Types fromStringRepr :: MonadFail m => String -> m Hypervisor Source # toStringRepr :: Hypervisor -> String Source # | |
HasStringRepr SSKey # | |
Defined in Ganeti.Ssconf fromStringRepr :: MonadFail m => String -> m SSKey Source # toStringRepr :: SSKey -> String Source # |
newtype GenericContainer a b Source #
The container type, a wrapper over Data.Map
GenericContainer | |
|
Instances
emptyContainer :: GenericContainer a b Source #
The empty container.
type Container = GenericContainer ByteString Source #
Type alias for string keys.
containerFromList :: Ord a => [(a, b)] -> GenericContainer a b Source #
Creates a GenericContainer from a list of key-value pairs.
lookupContainer :: (Monad m, Ord a) => m b -> a -> GenericContainer a b -> m b Source #
Looks up a value in a container with a default value.
If a key has no value, a given monadic default is returned.
This allows simple error handling, as the default can be
mzero
, failError
etc.
alterContainerL :: (Functor f, Ord a) => a -> (Maybe b -> f (Maybe b)) -> GenericContainer a b -> f (GenericContainer a b) Source #
Updates a value inside a container. The signature of the function is crafted so that it can be directly used as a lens.
readContainer :: (MonadFail m, HasStringRepr a, Ord a, JSON b) => JSObject JSValue -> m (GenericContainer a b) Source #
Container loader.
showContainer :: (HasStringRepr a, JSON b) => GenericContainer a b -> JSValue Source #
Container dumper.
Types that (de)serialize in a special form of JSON
mkUsedKeys :: Set Text -> UsedKeys Source #
class DictObject a where Source #
Class of objects that can be converted from and to JSObject
lists-format.
toDict :: a -> [(String, JSValue)] Source #
fromDictWKeys :: [(String, JSValue)] -> WriterT UsedKeys Result a Source #
Instances
showJSONtoDict :: DictObject a => a -> JSValue Source #
A default implementation of showJSON
using toDict
.
readJSONfromDict :: DictObject a => JSValue -> Result a Source #
class ArrayObject a where Source #
Class of objects that can be converted from and to [JSValue]
with
a fixed length and order.
Instances
General purpose data types for working with JSON
newtype MaybeForJSON a Source #
A Maybe newtype that allows for serialization more appropriate to the semantics of Maybe and JSON in our calls. Does not produce needless and confusing dictionaries.
In particular, JSNull
corresponds to Nothing
.
This also means that this `Maybe a` newtype should not be used with a
values that themselves can serialize to null
.
MaybeForJSON | |
|
Instances
newtype TimeAsDoubleJSON Source #
TimeAsDoubleJSON | |
|
Instances
nestedAccessByKey :: [String] -> JSValue -> Result JSValue Source #
Look up a value in a JSON object. Accessing ["a", "b", "c"]
on an
object is equivalent as accessing myobject.a.b.c
on a JavaScript object.
An error is returned if the object doesn't have such an accessor or if any value during the nested access is not an object at all.
nestedAccessByKeyDotted :: String -> JSValue -> Result JSValue Source #
Same as nestedAccessByKey
, but accessing with a dotted string instead
(like nestedAccessByKeyDotted "a.b.c"
).
:: String | fieldname to branch on |
-> (JSValue -> Result a) | decoding function if field is present and |
-> (JSValue -> Result a) | decoding function otherwise |
-> JSValue | |
-> Result a |
Branch decoding on a field in a JSON object.
addField :: (String, JSValue) -> JSValue -> JSValue Source #
Add a field to a JSON object; to nothing, if the argument is not an object.
maybeParseMap :: JSON a => JSValue -> Maybe (Map String a) Source #
Maybe obtain a map from a JSON object.