Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / JSON.hs @ 706f7f51

History | View | Annotate | Download (3.6 kB)

1
{-| JSON utility functions. -}
2

    
3
{-
4

    
5
Copyright (C) 2009, 2010, 2011 Google Inc.
6

    
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11

    
12
This program is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
General Public License for more details.
16

    
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
02110-1301, USA.
21

    
22
-}
23

    
24
module Ganeti.HTools.JSON
25
  ( fromJResult
26
  , readEitherString
27
  , JSRecord
28
  , loadJSArray
29
  , fromObj
30
  , maybeFromObj
31
  , fromObjWithDefault
32
  , fromKeyValue
33
  , fromJVal
34
  , asJSObject
35
  , asObjectList
36
  )
37
  where
38

    
39
import Control.Monad (liftM)
40
import Data.Maybe (fromMaybe)
41
import Text.Printf (printf)
42

    
43
import qualified Text.JSON as J
44

    
45
-- * JSON-related functions
46

    
47
-- | A type alias for the list-based representation of J.JSObject.
48
type JSRecord = [(String, J.JSValue)]
49

    
50
-- | Converts a JSON Result into a monadic value.
51
fromJResult :: Monad m => String -> J.Result a -> m a
52
fromJResult s (J.Error x) = fail (s ++ ": " ++ x)
53
fromJResult _ (J.Ok x) = return x
54

    
55
-- | Tries to read a string from a JSON value.
56
--
57
-- In case the value was not a string, we fail the read (in the
58
-- context of the current monad.
59
readEitherString :: (Monad m) => J.JSValue -> m String
60
readEitherString v =
61
  case v of
62
    J.JSString s -> return $ J.fromJSString s
63
    _ -> fail "Wrong JSON type"
64

    
65
-- | Converts a JSON message into an array of JSON objects.
66
loadJSArray :: (Monad m)
67
               => String -- ^ Operation description (for error reporting)
68
               -> String -- ^ Input message
69
               -> m [J.JSObject J.JSValue]
70
loadJSArray s = fromJResult s . J.decodeStrict
71

    
72
-- | Reads the value of a key in a JSON object.
73
fromObj :: (J.JSON a, Monad m) => JSRecord -> String -> m a
74
fromObj o k =
75
  case lookup k o of
76
    Nothing -> fail $ printf "key '%s' not found, object contains only %s"
77
               k (show (map fst o))
78
    Just val -> fromKeyValue k val
79

    
80
-- | Reads the value of an optional key in a JSON object.
81
maybeFromObj :: (J.JSON a, Monad m) =>
82
                JSRecord -> String -> m (Maybe a)
83
maybeFromObj o k =
84
  case lookup k o of
85
    Nothing -> return Nothing
86
    Just val -> liftM Just (fromKeyValue k val)
87

    
88
-- | Reads the value of a key in a JSON object with a default if missing.
89
fromObjWithDefault :: (J.JSON a, Monad m) =>
90
                      JSRecord -> String -> a -> m a
91
fromObjWithDefault o k d = liftM (fromMaybe d) $ maybeFromObj o k
92

    
93
-- | Reads a JValue, that originated from an object key.
94
fromKeyValue :: (J.JSON a, Monad m)
95
              => String     -- ^ The key name
96
              -> J.JSValue  -- ^ The value to read
97
              -> m a
98
fromKeyValue k val =
99
  fromJResult (printf "key '%s'" k) (J.readJSON val)
100

    
101
-- | Small wrapper over readJSON.
102
fromJVal :: (Monad m, J.JSON a) => J.JSValue -> m a
103
fromJVal v =
104
  case J.readJSON v of
105
    J.Error s -> fail ("Cannot convert value '" ++ show v ++
106
                       "', error: " ++ s)
107
    J.Ok x -> return x
108

    
109
-- | Converts a JSON value into a JSON object.
110
asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
111
asJSObject (J.JSObject a) = return a
112
asJSObject _ = fail "not an object"
113

    
114
-- | Coneverts a list of JSON values into a list of JSON objects.
115
asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue]
116
asObjectList = mapM asJSObject