This is a helper function that gets the JSON encoding of the first
element of the list (if possible).

Signed-off-by: Agata Murawska <[email protected]>
---
 htools/Ganeti/JSON.hs |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/htools/Ganeti/JSON.hs b/htools/Ganeti/JSON.hs
index 132c0b2..bfeb48a 100644
--- a/htools/Ganeti/JSON.hs
+++ b/htools/Ganeti/JSON.hs
@@ -31,6 +31,7 @@ module Ganeti.JSON
   , fromObjWithDefault
   , fromKeyValue
   , fromJVal
+  , jsonNullOrFirst
   , asJSObject
   , asObjectList
   , tryFromObj
@@ -125,6 +126,11 @@ fromJVal v =
                        "', error: " ++ s)
     J.Ok x -> return x
 
+-- | Helper function that returns Null or first element of the list.
+jsonNullOrFirst :: (J.JSON b) => [a] -> (a -> b) -> J.JSValue
+jsonNullOrFirst [] _ = J.JSNull
+jsonNullOrFirst (x:_) f = J.showJSON $ f x
+
 -- | Converts a JSON value into a JSON object.
 asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
 asJSObject (J.JSObject a) = return a
-- 
1.7.7.3

Reply via email to