Revision 06fb92cf src/Ganeti/JSON.hs

b/src/Ganeti/JSON.hs
5 5

  
6 6
{-
7 7

  
8
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
8
Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc.
9 9

  
10 10
This program is free software; you can redistribute it and/or modify
11 11
it under the terms of the GNU General Public License as published by
......
36 36
  , fromJVal
37 37
  , jsonHead
38 38
  , getMaybeJsonHead
39
  , getMaybeJsonElem
39 40
  , asJSObject
40 41
  , asObjectList
41 42
  , tryFromObj
......
186 187
getMaybeJsonHead [] _ = J.JSNull
187 188
getMaybeJsonHead (x:_) f = maybe J.JSNull J.showJSON (f x)
188 189

  
190
-- | Helper for extracting Maybe values from a list that might be too short.
191
getMaybeJsonElem :: (J.JSON b) => [a] -> Int -> (a -> Maybe b) -> J.JSValue
192
getMaybeJsonElem [] _ _ = J.JSNull
193
getMaybeJsonElem xs 0 f = getMaybeJsonHead xs f
194
getMaybeJsonElem (_:xs) n f
195
  | n < 0 = J.JSNull
196
  | otherwise = getMaybeJsonElem xs (n - 1) f
197

  
189 198
-- | Converts a JSON value into a JSON object.
190 199
asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
191 200
asJSObject (J.JSObject a) = return a

Also available in: Unified diff