Skip to content

Commit 412591b

Browse files
authored
Merge pull request #43 from codedownio/fix-IntOrString
Fix FromJSON/ToJSON instances of CustomTypes (IntOrString and Quantity)
2 parents 6b115bc + e0a4857 commit 412591b

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

kubernetes/lib/Kubernetes/OpenAPI/CustomTypes.hs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
module Kubernetes.OpenAPI.CustomTypes where
55

66
import Data.Aeson (FromJSON, ToJSON)
7+
import qualified Data.Aeson as A
78
import Data.Data (Typeable)
89
import Data.Text (Text)
10+
import GHC.Base (mzero)
911

1012
import GHC.Generics
1113

@@ -18,7 +20,16 @@ import GHC.Generics
1820
data IntOrString
1921
= IntOrStringS Text
2022
| IntOrStringI Int
21-
deriving (Show, Eq, ToJSON, FromJSON, Typeable, Generic)
23+
deriving (Show, Eq, Typeable, Generic)
24+
25+
instance FromJSON IntOrString where
26+
parseJSON (A.String t) = return $ IntOrStringS t
27+
parseJSON (A.Number n) = return $ IntOrStringI (round n)
28+
parseJSON _ = mzero
29+
30+
instance ToJSON IntOrString where
31+
toJSON (IntOrStringS t) = A.String t
32+
toJSON (IntOrStringI n) = A.Number (fromIntegral n)
2233

2334
{- | `Quantity` is a fixed-point representation of a number.
2435
@@ -78,4 +89,11 @@ data IntOrString
7889
that will cause implementors to also use a fixed point implementation.
7990
-}
8091
newtype Quantity = Quantity { unQuantity :: Text }
81-
deriving (Show, Eq, ToJSON, FromJSON, Typeable, Generic)
92+
deriving (Show, Eq, Typeable, Generic)
93+
94+
instance FromJSON Quantity where
95+
parseJSON (A.String t) = return $ Quantity t
96+
parseJSON _ = mzero
97+
98+
instance ToJSON Quantity where
99+
toJSON (Quantity t) = A.String t

0 commit comments

Comments
 (0)