Revision 885dafbc

b/src/Ganeti/JSON.hs
27 27
module Ganeti.JSON
28 28
  ( fromJResult
29 29
  , fromJResultE
30
  , readJSONWithDesc
30 31
  , readEitherString
31 32
  , JSRecord
32 33
  , loadJSArray
......
91 92
-- | A type alias for the list-based representation of J.JSObject.
92 93
type JSRecord = [JSField]
93 94

  
95
-- | Annotate @readJSON@ error messages with descriptions of what
96
-- is being parsed into what.
97
readJSONWithDesc :: (J.JSON a)
98
                 => String                    -- ^ description of @a@
99
                 -> Bool                      -- ^ include input in
100
                                              --   error messages
101
                 -> J.JSValue                 -- ^ input value
102
                 -> J.Result a
103
readJSONWithDesc typ incInput input =
104
  case J.readJSON input of
105
    J.Ok r    -> J.Ok r
106
    J.Error e -> J.Error $ if incInput then msg ++ " from " ++ show input
107
                                       else msg
108
      where msg = "Can't parse value for type " ++ typ ++ ": " ++ e
109

  
94 110
-- | Converts a JSON Result into a monadic value.
95 111
fromJResult :: Monad m => String -> J.Result a -> m a
96 112
fromJResult s (J.Error x) = fail (s ++ ": " ++ x)

Also available in: Unified diff