From daa7941478b73d8057389fe27797a670bcaa398e Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Wed, 12 Dec 2012 01:29:28 +0100 Subject: [PATCH] Introduce NFData instances for JSON types The JSValue/JSObject types don't come with a NFData instance, so let's add one ourselves, so that we can force evaluation of JSValues (either when building or when reading them). Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: Helga Velroyen <helgav@google.com> --- htools/Ganeti/JSON.hs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/htools/Ganeti/JSON.hs b/htools/Ganeti/JSON.hs index 30b80d829..dc8dc0fe8 100644 --- a/htools/Ganeti/JSON.hs +++ b/htools/Ganeti/JSON.hs @@ -1,4 +1,6 @@ {-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-} +{-# OPTIONS_GHC -fno-warn-orphans #-} + {-| JSON utility functions. -} {- @@ -46,6 +48,7 @@ module Ganeti.JSON ) where +import Control.DeepSeq import Control.Monad (liftM) import Data.Maybe (fromMaybe, catMaybes) import qualified Data.Map as Map @@ -62,6 +65,17 @@ import Ganeti.BasicTypes -- * JSON-related functions +instance NFData J.JSValue where + rnf J.JSNull = () + rnf (J.JSBool b) = rnf b + rnf (J.JSRational b r) = rnf b `seq` rnf r + rnf (J.JSString s) = rnf $ J.fromJSString s + rnf (J.JSArray a) = rnf a + rnf (J.JSObject o) = rnf o + +instance (NFData a) => NFData (J.JSObject a) where + rnf = rnf . J.fromJSObject + -- | A type alias for a field of a JSRecord. type JSField = (String, J.JSValue) @@ -203,6 +217,9 @@ newtype GenericContainer a b = GenericContainer { fromContainer :: Map.Map a b } deriving (Show, Eq) +instance (NFData a, NFData b) => NFData (GenericContainer a b) where + rnf = rnf . Map.toList . fromContainer + -- | Type alias for string keys. type Container = GenericContainer String -- GitLab