Revision 3c8e6d09

b/htools/Ganeti/JSON.hs
38 38
  , asObjectList
39 39
  , tryFromObj
40 40
  , toArray
41
  , optionalJSField
42
  , optFieldsToObj
41 43
  , HasStringRepr(..)
42 44
  , GenericContainer(..)
43 45
  , Container
......
45 47
  where
46 48

  
47 49
import Control.Monad (liftM)
48
import Data.Maybe (fromMaybe)
50
import Data.Maybe (fromMaybe, catMaybes)
49 51
import qualified Data.Map as Map
50 52
import Text.Printf (printf)
51 53

  
......
60 62

  
61 63
-- * JSON-related functions
62 64

  
65
-- | A type alias for a field of a JSRecord.
66
type JSField = (String, J.JSValue)
67

  
63 68
-- | A type alias for the list-based representation of J.JSObject.
64
type JSRecord = [(String, J.JSValue)]
69
type JSRecord = [JSField]
65 70

  
66 71
-- | Converts a JSON Result into a monadic value.
67 72
fromJResult :: Monad m => String -> J.Result a -> m a
......
167 172
toArray o =
168 173
  fail $ "Invalid input, expected array but got " ++ show (pp_value o)
169 174

  
175
-- | Creates a Maybe JSField. If the value string is Nothing, the JSField
176
-- will be Nothing as well.
177
optionalJSField :: (J.JSON a) => String -> Maybe a -> Maybe JSField
178
optionalJSField name (Just value) = Just (name, J.showJSON value)
179
optionalJSField _ Nothing = Nothing
180

  
181
-- | Creates an object with all the non-Nothing fields of the given list.
182
optFieldsToObj :: [Maybe JSField] -> J.JSValue
183
optFieldsToObj = J.makeObj . catMaybes
184

  
170 185
-- * Container type (special type for JSON serialisation)
171 186

  
172 187
-- | Class of types that can be converted from Strings. This is

Also available in: Unified diff