Commit daa79414 authored by Iustin Pop's avatar Iustin Pop
Browse files

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: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarHelga Velroyen <helgav@google.com>
parent 77d43564
{-# 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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment