ganeti

Safe HaskellNone

Ganeti.JSON

Contents

Description

JSON utility functions.

Synopsis

JSON-related functions

type JSField = (String, JSValue)Source

type JSRecord = [JSField]Source

A type alias for the list-based representation of J.JSObject.

readJSONWithDescSource

Arguments

:: JSON a 
=> String

description of a

-> JSValue

input value

-> Result a 

Annotate readJSON error messages with descriptions of what is being parsed into what.

fromJResult :: Monad m => String -> Result a -> m aSource

Converts a JSON Result into a monadic value.

fromJResultE :: (Error e, MonadError e m) => String -> Result a -> m aSource

Converts a JSON Result into a MonadError value.

readEitherString :: Monad m => JSValue -> m StringSource

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.

loadJSArraySource

Arguments

:: Monad 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 -> StringSource

fromObj :: (JSON a, Monad m) => JSRecord -> String -> m aSource

Reads the value of a key in a JSON object.

maybeFromObj :: (JSON a, Monad 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, Monad m) => JSRecord -> String -> a -> m aSource

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) => JSValue -> m [Maybe a]Source

arrayMaybeFromObj :: (JSON a, Monad m) => JSRecord -> String -> m [Maybe a]Source

tryArrayMaybeFromObjSource

Arguments

:: 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

fromKeyValueSource

Arguments

:: (JSON a, Monad m) 
=> String

The key name

-> JSValue

The value to read

-> m a 

Reads a JValue, that originated from an object key.

fromJVal :: (Monad m, JSON a) => JSValue -> m aSource

Small wrapper over readJSON.

fromJValE :: (Error e, MonadError e m, JSON a) => JSValue -> m aSource

Small wrapper over readJSON for MonadError.

jsonHead :: JSON b => [a] -> (a -> b) -> JSValueSource

Helper function that returns Null or first element of the list.

getMaybeJsonHead :: JSON b => [a] -> (a -> Maybe b) -> JSValueSource

Helper for extracting Maybe values from a possibly empty list.

getMaybeJsonElem :: JSON b => [a] -> Int -> (a -> Maybe b) -> JSValueSource

Helper for extracting Maybe values from a list that might be too short.

asJSObject :: Monad m => JSValue -> m (JSObject JSValue)Source

Converts a JSON value into a JSON object.

asObjectList :: Monad m => [JSValue] -> m [JSObject JSValue]Source

Coneverts a list of JSON values into a list of JSON objects.

tryFromObjSource

Arguments

:: 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 => JSValue -> m [JSValue]Source

Ensure a given JSValue is actually a JSArray.

optionalJSField :: JSON a => String -> Maybe a -> Maybe JSFieldSource

Creates a Maybe JSField. If the value string is Nothing, the JSField will be Nothing as well.

optFieldsToObj :: [Maybe JSField] -> JSValueSource

Creates an object with all the non-Nothing fields of the given list.

Container type (special type for JSON serialisation)

class HasStringRepr a whereSource

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.

Methods

fromStringRepr :: Monad m => String -> m aSource

toStringRepr :: a -> StringSource

newtype GenericContainer a b Source

The container type, a wrapper over Data.Map

Constructors

GenericContainer 

Fields

fromContainer :: Map a b
 

Instances

Arbitrary OsParams 
Arbitrary GroupDiskParams

No real arbitrary instance for GroupDiskParams yet.

Arbitrary IAllocatorParams 
Arbitrary ClusterNicParams 
Arbitrary ClusterOsParamsPrivate 
Arbitrary ClusterOsParams 
Arbitrary ClusterBeParams 
Arbitrary OsHvParams

No real arbitrary instance for OsHvParams yet.

Arbitrary ClusterHvParams

No real arbitrary instance for ClusterHvParams yet.

Functor (GenericContainer a) 
Foldable (GenericContainer a) 
Traversable (GenericContainer a) 
Arbitrary (Container DataCollectorConfig) 
(Eq a, Eq b) => Eq (GenericContainer a b) 
(Ord a, Ord b) => Ord (GenericContainer a b) 
(Show a, Show b) => Show (GenericContainer a b) 
(NFData a, NFData b) => NFData (GenericContainer a b) 
(HasStringRepr a, Ord a, JSON b) => JSON (GenericContainer a b) 

emptyContainer :: GenericContainer a bSource

The empty container.

type Container = GenericContainer ByteStringSource

Type alias for string keys.

containerFromList :: Ord a => [(a, b)] -> GenericContainer a bSource

Creates a GenericContainer from a list of key-value pairs.

lookupContainer :: (Monad m, Ord a) => m b -> a -> GenericContainer a b -> m bSource

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 :: (Monad m, HasStringRepr a, Ord a, JSON b) => JSObject JSValue -> m (GenericContainer a b)Source

Container loader.

showContainer :: (HasStringRepr a, JSON b) => GenericContainer a b -> JSValueSource

Types that (de)serialize in a special form of JSON

newtype UsedKeys Source

Constructors

UsedKeys (Maybe (Set String)) 

Instances

Monoid UsedKeys 

mkUsedKeys :: Set String -> UsedKeysSource

class DictObject a whereSource

Class of objects that can be converted from and to JSObject lists-format.

Methods

toDict :: a -> [(String, JSValue)]Source

fromDictWKeys :: [(String, JSValue)] -> WriterT UsedKeys Result aSource

fromDict :: [(String, JSValue)] -> Result aSource

Instances

DictObject ConfdReqQ 
DictObject ConfdRequest 
DictObject ConfdReply 
DictObject SignedMessage 
DictObject ISpec 
DictObject MinMaxISpecs 
DictObject IPolicy 
DictObject CPUavgload 
DictObject CPUstat 
DictObject LVInfo 
DictObject FieldDefinition 
DictObject QueryResult 
DictObject QueryFieldsResult 
DictObject Disk 
DictObject ForthcomingDiskData 
DictObject RealDiskData 
DictObject PartialNicParams 
DictObject FilledNicParams 
DictObject PartialNic 
DictObject PartialBeParams 
DictObject FilledBeParams 
DictObject Instance 
DictObject ForthcomingInstanceData 
DictObject RealInstanceData 
DictObject Diskstats 
DictObject INicParams 
DictObject IDiskParams 
DictObject OpCode 
DictObject MetaOpCode 
DictObject CommonOpParams 
DictObject QueuedOpCode 
DictObject QueuedJob 
DictObject Network 
DictObject DataCollectorConfig 
DictObject PartialISpecParams 
DictObject FilledISpecParams 
DictObject MinMaxISpecs 
DictObject PartialIPolicy 
DictObject FilledIPolicy 
DictObject PartialNDParams 
DictObject FilledNDParams 
DictObject Node 
DictObject NodeGroup 
DictObject FilterRule 
DictObject Cluster 
DictObject ConfigData 
DictObject MasterNetworkParameters 
DictObject DCStatus 
DictObject DCReport 
DictObject InstStatus 
DictObject ReportData 
DictObject RpcCallInstanceInfo 
DictObject InstanceInfo 
DictObject RpcResultInstanceInfo 
DictObject RpcCallAllInstancesInfo 
DictObject RpcResultAllInstancesInfo 
DictObject InstanceConsoleInfoParams 
DictObject RpcCallInstanceConsoleInfo 
DictObject InstanceConsoleInfo 
DictObject RpcResultInstanceConsoleInfo 
DictObject RpcCallInstanceList 
DictObject RpcResultInstanceList 
DictObject RpcCallNodeInfo 
DictObject StorageInfo 
DictObject HvInfo 
DictObject RpcResultNodeInfo 
DictObject RpcCallVersion 
DictObject RpcResultVersion 
DictObject RpcCallStorageList 
DictObject RpcResultStorageList 
DictObject RpcCallTestDelay 
DictObject RpcCallExportList 
DictObject RpcResultExportList 
DictObject RpcCallJobqueueUpdate 
DictObject RpcResultJobQueueUpdate 
DictObject RpcCallJobqueueRename 
DictObject RpcResultJobqueueRename 
DictObject RpcCallSetWatcherPause 
DictObject RpcResultSetWatcherPause 
DictObject RpcCallSetDrainFlag 
DictObject RpcResultSetDrainFlag 
DictObject RpcCallUploadFile 
DictObject RpcResultUploadFile 
DictObject RpcCallWriteSsconfFiles 
DictObject RpcResultWriteSsconfFiles 
DictObject RpcCallNodeActivateMasterIp 
DictObject RpcResultNodeActivateMasterIp 
DictObject RpcCallMasterNodeName 
DictObject RpcResultMasterNodeName 
DictObject LuxiOp 
DictObject TempResState 
DictObject TestObj 
DictObject UnitObj 
DictObject PartialTestParams 
DictObject FilledTestParams 

showJSONtoDict :: DictObject a => a -> JSValueSource

A default implementation of showJSON using toDict.

readJSONfromDict :: DictObject a => JSValue -> Result aSource

A default implementation of readJSON using fromDict. Checks that the input value is a JSON object and converts it using fromDict. Also checks the input contains only the used keys returned by fromDict.

class ArrayObject a whereSource

Class of objects that can be converted from and to [JSValue] with a fixed length and order.

Methods

toJSArray :: a -> [JSValue]Source

fromJSArray :: [JSValue] -> Result aSource

Instances

ArrayObject ConfdReqQ 
ArrayObject ConfdRequest 
ArrayObject ConfdReply 
ArrayObject SignedMessage 
ArrayObject ISpec 
ArrayObject MinMaxISpecs 
ArrayObject IPolicy 
ArrayObject CPUavgload 
ArrayObject CPUstat 
ArrayObject LVInfo 
ArrayObject FieldDefinition 
ArrayObject QueryResult 
ArrayObject QueryFieldsResult 
ArrayObject Disk 
ArrayObject ForthcomingDiskData 
ArrayObject RealDiskData 
ArrayObject PartialNicParams 
ArrayObject FilledNicParams 
ArrayObject PartialNic 
ArrayObject PartialBeParams 
ArrayObject FilledBeParams 
ArrayObject Instance 
ArrayObject ForthcomingInstanceData 
ArrayObject RealInstanceData 
ArrayObject Diskstats 
ArrayObject INicParams 
ArrayObject IDiskParams 
ArrayObject CommonOpParams 
ArrayObject QueuedOpCode 
ArrayObject QueuedJob 
ArrayObject Network 
ArrayObject DataCollectorConfig 
ArrayObject PartialISpecParams 
ArrayObject FilledISpecParams 
ArrayObject MinMaxISpecs 
ArrayObject PartialIPolicy 
ArrayObject FilledIPolicy 
ArrayObject PartialNDParams 
ArrayObject FilledNDParams 
ArrayObject Node 
ArrayObject NodeGroup 
ArrayObject FilterRule 
ArrayObject Cluster 
ArrayObject ConfigData 
ArrayObject MasterNetworkParameters 
ArrayObject DCStatus 
ArrayObject DCReport 
ArrayObject InstStatus 
ArrayObject ReportData 
ArrayObject RpcCallInstanceInfo 
ArrayObject InstanceInfo 
ArrayObject RpcResultInstanceInfo 
ArrayObject RpcCallAllInstancesInfo 
ArrayObject RpcResultAllInstancesInfo 
ArrayObject InstanceConsoleInfoParams 
ArrayObject RpcCallInstanceConsoleInfo 
ArrayObject InstanceConsoleInfo 
ArrayObject RpcResultInstanceConsoleInfo 
ArrayObject RpcCallInstanceList 
ArrayObject RpcResultInstanceList 
ArrayObject RpcCallNodeInfo 
ArrayObject StorageInfo 
ArrayObject HvInfo 
ArrayObject RpcResultNodeInfo 
ArrayObject RpcCallVersion 
ArrayObject RpcResultVersion 
ArrayObject RpcCallStorageList 
ArrayObject RpcResultStorageList 
ArrayObject RpcCallTestDelay 
ArrayObject RpcCallExportList 
ArrayObject RpcResultExportList 
ArrayObject RpcCallJobqueueUpdate 
ArrayObject RpcResultJobQueueUpdate 
ArrayObject RpcCallJobqueueRename 
ArrayObject RpcResultJobqueueRename 
ArrayObject RpcCallSetWatcherPause 
ArrayObject RpcResultSetWatcherPause 
ArrayObject RpcCallSetDrainFlag 
ArrayObject RpcResultSetDrainFlag 
ArrayObject RpcCallUploadFile 
ArrayObject RpcResultUploadFile 
ArrayObject RpcCallWriteSsconfFiles 
ArrayObject RpcResultWriteSsconfFiles 
ArrayObject RpcCallNodeActivateMasterIp 
ArrayObject RpcResultNodeActivateMasterIp 
ArrayObject RpcCallMasterNodeName 
ArrayObject RpcResultMasterNodeName 
ArrayObject TempResState 
ArrayObject TestObj 
ArrayObject UnitObj 
ArrayObject PartialTestParams 
ArrayObject FilledTestParams 

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.

Constructors

MaybeForJSON 

Fields

unMaybeForJSON :: Maybe a
 

Instances

Eq a => Eq (MaybeForJSON a) 
Ord a => Ord (MaybeForJSON a) 
Show a => Show (MaybeForJSON a) 
Arbitrary a => Arbitrary (MaybeForJSON a) 
JSON a => JSON (MaybeForJSON a) 

newtype Tuple5 a b c d e Source

Constructors

Tuple5 

Fields

unTuple5 :: (a, b, c, d, e)
 

Instances

(JSON a, JSON b, JSON c, JSON d, JSON e) => JSON (Tuple5 a b c d e) 

nestedAccessByKey :: [String] -> JSValue -> Result JSValueSource

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 JSValueSource

Same as nestedAccessByKey, but accessing with a dotted string instead (like nestedAccessByKeyDotted a.b.c).

branchOnFieldSource

Arguments

:: String

fieldname to branch on

-> (JSValue -> Result a)

decoding function if field is present and true; field will already be removed in the input

-> (JSValue -> Result a)

decoding function otherwise

-> JSValue 
-> Result a 

Branch decoding on a field in a JSON object.

addField :: (String, JSValue) -> JSValue -> JSValueSource

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.