4
4
module Kubernetes.OpenAPI.CustomTypes where
5
5
6
6
import Data.Aeson (FromJSON , ToJSON )
7
+ import qualified Data.Aeson as A
7
8
import Data.Data (Typeable )
8
9
import Data.Text (Text )
10
+ import GHC.Base (mzero )
9
11
10
12
import GHC.Generics
11
13
@@ -18,7 +20,16 @@ import GHC.Generics
18
20
data IntOrString
19
21
= IntOrStringS Text
20
22
| 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)
22
33
23
34
{- | `Quantity` is a fixed-point representation of a number.
24
35
@@ -78,4 +89,11 @@ data IntOrString
78
89
that will cause implementors to also use a fixed point implementation.
79
90
-}
80
91
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