summaryrefslogtreecommitdiff
path: root/lib/Data
diff options
context:
space:
mode:
authorJasperVanDerJeugt <>2019-10-09 15:10:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2019-10-09 15:10:00 (GMT)
commit843663f9e31b1dac40b3090f848715b2fd334587 (patch)
tree61f6553efd128a85282c70d179fb005ef3d52eb4 /lib/Data
parentc78dda812788f59bdff0a50281abee00039bc333 (diff)
version 0.8.4.00.8.4.0
Diffstat (limited to 'lib/Data')
-rw-r--r--lib/Data/Aeson/Extended.hs22
-rw-r--r--lib/Data/Aeson/TH/Extended.hs21
-rw-r--r--lib/Data/Data/Extended.hs23
3 files changed, 66 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
diff --git a/lib/Data/Aeson/TH/Extended.hs b/lib/Data/Aeson/TH/Extended.hs
new file mode 100644
index 0000000..0fa5487
--- /dev/null
+++ b/lib/Data/Aeson/TH/Extended.hs
@@ -0,0 +1,21 @@
+--------------------------------------------------------------------------------
+module Data.Aeson.TH.Extended
+ ( module Data.Aeson.TH
+ , dropPrefixOptions
+ ) where
+
+
+--------------------------------------------------------------------------------
+import Data.Aeson.TH
+import Data.Char (isUpper, toLower)
+
+
+--------------------------------------------------------------------------------
+dropPrefixOptions :: Options
+dropPrefixOptions = defaultOptions
+ { fieldLabelModifier = dropPrefix
+ }
+ where
+ dropPrefix str = case break isUpper str of
+ (_, (y : ys)) -> toLower y : ys
+ _ -> str
diff --git a/lib/Data/Data/Extended.hs b/lib/Data/Data/Extended.hs
new file mode 100644
index 0000000..636591e
--- /dev/null
+++ b/lib/Data/Data/Extended.hs
@@ -0,0 +1,23 @@
+module Data.Data.Extended
+ ( module Data.Data
+
+ , grecQ
+ , grecT
+ ) where
+
+import Data.Data
+
+-- | Recursively find all values of a certain type.
+grecQ :: (Data a, Data b) => a -> [b]
+grecQ = concat . gmapQ (\x -> maybe id (:) (cast x) $ grecQ x)
+
+-- | Recursively apply an update to a certain type.
+grecT :: (Data a, Data b) => (a -> a) -> b -> b
+grecT f x = gmapT (grecT f) (castMap f x)
+
+castMap :: (Typeable a, Typeable b) => (a -> a) -> b -> b
+castMap f x = case cast x of
+ Nothing -> x
+ Just y -> case cast (f y) of
+ Nothing -> x
+ Just z -> z