@@ -541,9 +541,10 @@ data Label = Label Name Value deriving (Eq, Show)
541541-- If there are multiple lables with the same Name in the list
542542-- then the last one wins.
543543instance ToJSON [Label] where
544− toJSON [] = JSON.Object HM.empty
545− toJSON (l:ls) = JSON.Object $ foldl f HM.empty (l:ls)
546− where f acc (Label n v) = HM.insert n (toJSON v) acc
544+ toJSON [] = emptyJsonObject
545+ toJSON (l:ls) = toJsonKeyVal (l:ls) key val
546+ where key (Label k _) = T.unpack k
547+ val (Label _ v) = v
547548
548549instance FromJSON [Label] where
549550 parseJSON (JSON.Object o) = HM.foldlWithKey' f (return []) o
@@ -763,9 +764,9 @@ instance FromJSON VolumePermission where
763764newtype Volume = Volume FilePath deriving (Eq, Show)
764765
765766instance ToJSON [Volume] where
766− toJSON [] = JSON.Object HM.empty
767− toJSON (v:vs) = JSON.Object $ foldl f HM.empty (v:vs)
768− where f acc (Volume k) = HM.insert (T.pack k) (JSON.Object HM.empty) acc
767+ toJSON [] = emptyJsonObject
768+ toJSON (v:vs) = toJsonKey (v:vs) getKey
769+ where getKey (Volume v) = v
769770
770771instance FromJSON [Volume] where
771772 parseJSON (JSON.Object o) = return $ map (Volume . T.unpack) $ HM.keys o
@@ -1017,11 +1018,10 @@ instance FromJSON [PortBinding] where
10171018 parseJSON _ = fail "PortBindings is not an object"
10181019
10191020instance ToJSON [PortBinding] where
1020− toJSON [] = JSON.Object HM.empty
1021− toJSON (p:ps) = JSON.Object $ foldl f HM.empty (p:ps)
1022− where mKey p = portAndType2Text (containerPort p) (portType p)
1023− mVal p = toJSON $ hostPorts p
1024− f acc p = HM.insert (mKey p) (mVal p) acc
1021+ toJSON [] = emptyJsonObject
1022+ toJSON (p:ps) = toJsonKeyVal (p:ps) key val
1023+ where key p = T.unpack $ portAndType2Text (containerPort p) (portType p)
1024+ val p = hostPorts p
10251025
10261026data HostPort = HostPort {
10271027 hostIp :: Text
@@ -1326,10 +1326,10 @@ instance FromJSON [ExposedPort] where
13261326 parseJSON _ = fail "ExposedPorts is not an object"
13271327
13281328instance ToJSON [ExposedPort] where
1329− toJSON [] = JSON.Object HM.empty
1330− toJSON (p:ps) = JSON.Object $ foldl f HM.empty (p:ps)
1331− where f acc (ExposedPort p t) = HM.insert (exposify p t) (JSON.Object HM.empty) acc
1332− exposify p t = (T.pack $ show p) <> "/" <> (T.pack $ show t)
1329+ toJSON [] = emptyJsonObject
1330+ toJSON (p:ps) = toJsonKey (p:ps) key
1331+ where key (ExposedPort p t) = show p <> slash <> show t
1332+ slash = T.unpack "/"
13331333
13341334data ContainerConfig = ContainerConfig {
13351335 hostname :: Maybe Text
@@ -1397,3 +1397,20 @@ parseIntegerText t = case readMaybe $ T.unpack t of
13971397 fail "Could not parse Integer"
13981398 Just i ->
13991399 return i
1400+
1401+-- | Helper function for converting a data type [a] to a json dictionary
1402+-- like so {"something": {}, "something2": {}}
1403+toJsonKey :: Foldable t => t a -> (a -> String) -> JSON.Value
1404+toJsonKey vs getKey = JSON.Object $ foldl f HM.empty vs
1405+ where f acc x = HM.insert (T.pack $ getKey x) (JSON.Object HM.empty) acc
1406+
1407+-- | Helper function for converting a data type [a] to a json dictionary
1408+-- like so {"something": "val1", "something2": "val2"}
1409+toJsonKeyVal :: (Foldable t, JSON.ToJSON r) => t a -> (a -> String) -> (a -> r) -> JSON.Value
1410+toJsonKeyVal vs getKey getVal = JSON.Object $ foldl f HM.empty vs
1411+ where f acc x = HM.insert (T.pack $ getKey x) (toJSON $ getVal x) acc
1412+
1413+-- | Helper function that return an empty dictionary "{}"
1414+emptyJsonObject :: JSON.Value
1415+emptyJsonObject = JSON.Object HM.empty
1416+