summaryrefslogtreecommitdiff
path: root/lib/Data/Aeson/Extended.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data/Aeson/Extended.hs')
-rw-r--r--lib/Data/Aeson/Extended.hs22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/Data/Aeson/Extended.hs b/lib/Data/Aeson/Extended.hs
new file mode 100644
index 0000000..9b95cec
--- /dev/null
+++ b/lib/Data/Aeson/Extended.hs
@@ -0,0 +1,22 @@
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
+module Data.Aeson.Extended
+ ( module Data.Aeson
+
+ , FlexibleNum (..)
+ ) where
+
+import Control.Applicative ((<$>))
+import Data.Aeson
+import qualified Data.Text as T
+import Text.Read (readMaybe)
+import Prelude
+
+-- | This can be parsed from a JSON string in addition to a JSON number.
+newtype FlexibleNum a = FlexibleNum {unFlexibleNum :: a}
+ deriving (Show, ToJSON)
+
+instance (FromJSON a, Read a) => FromJSON (FlexibleNum a) where
+ parseJSON (String str) = case readMaybe (T.unpack str) of
+ Nothing -> fail $ "Could not parse " ++ T.unpack str ++ " as a number"
+ Just x -> return (FlexibleNum x)
+ parseJSON val = FlexibleNum <$> parseJSON val