git.iko.soy/typeable/aeson-inflections
on master help sign in
4 files changed+97−20
since splitsnapshots
unifiedsplit
LICENSE+2 aeson-inflections.cabal+29−19 src/Data/Aeson/Inflections.hs+20−1 src/Data/Aeson/Inflections/Generic.hs+46
LICENSEnew file+2
@@ -0,0 +1,2 @@
1+Copyright © 2020, Antorica LLC
2+All rights reserved
aeson-inflections.cabal+29−19
@@ -1,21 +1,31 @@
1−name: aeson-inflections
2−version: 0.1.0.0
3−synopsis: Collection of useful functions for aeson instances and stuff
4−author: Author name here
5−maintainer: example@example.com
6−copyright: 2020 Author name here
7−category: Web
8−build-type: Simple
9−cabal-version: >=1.10
1+cabal-version: 2.4
2+name: aeson-inflections
3+version: 0.1.0.0
4+synopsis:
5+ Collection of useful functions for aeson instances and stuff
6+
7+author: Author name here
8+maintainer: example@example.com
9+license-file: LICENSE
10+category: Web
11+build-type: Simple
1012
1113library
12− hs-source-dirs: src
13− ghc-options: -Wall
14− exposed-modules: Data.Aeson.Inflections
15− build-depends: base >= 4.7 && < 5
16− , aeson
17− , inflections
18− , lens
19− , megaparsec
20− , text
21− default-language: Haskell2010
14+ hs-source-dirs: src
15+ ghc-options: -Wall
16+ exposed-modules:
17+ Data.Aeson.Inflections
18+ Data.Aeson.Inflections.Generic
19+
20+ build-depends:
21+ , aeson
22+ , base >=4.12 && <5
23+ , deriving-aeson >=0.2
24+ , inflections
25+ , lens
26+ , megaparsec >=7.0
27+ , template-haskell
28+ , text
29+
30+ default-extensions: DataKinds
31+ default-language: Haskell2010
src/Data/Aeson/Inflections.hs+20−1
@@ -1,10 +1,13 @@
11module Data.Aeson.Inflections
22 ( defaultFieldLabelModifier
33 , defaultFieldLabelModifier'
4+ , deriveDefaultJSON
5+ , deriveEnumJSON
46 , dropPrefix
57 , dropPrefixLower
68 , dropByPrefixLength
79 , cutPrefix
10+ , cutOptionalPrefix
811 , toUnderscore
912 , toDashed
1013 , toSpaced
@@ -12,10 +15,14 @@ module Data.Aeson.Inflections
1215
1316import Control.Lens
1417import Control.Monad
18+import Data.Aeson
19+import Data.Aeson.TH
1520import Data.Char
21+import Data.Maybe
1622import Data.Text (Text)
17−import qualified Data.Text as T (length, pack, unpack)
23+import qualified Data.Text as T
1824import Data.Void
25+import Language.Haskell.TH
1926import qualified Text.Inflections as I
2027import Text.Megaparsec.Error
2128
@@ -28,6 +35,14 @@ defaultFieldLabelModifier = toUnderscore . dropPrefix
2835defaultFieldLabelModifier' :: Text -> Text
2936defaultFieldLabelModifier' = T.pack . defaultFieldLabelModifier . T.unpack
3037
38+deriveDefaultJSON :: Name -> Q [Dec]
39+deriveDefaultJSON = deriveJSON defaultOptions
40+ { fieldLabelModifier = defaultFieldLabelModifier }
41+
42+deriveEnumJSON :: String -> Name -> Q [Dec]
43+deriveEnumJSON prefix = deriveJSON defaultOptions
44+ { constructorTagModifier = toUnderscore . cutPrefix prefix }
45+
3146-- | Just drops prefix
3247dropPrefix :: String -> String
3348dropPrefix = dropWhile (not . isUpper)
@@ -47,6 +62,10 @@ cutPrefix pref s = if pref == take plen s
4762 where
4863 plen = length pref
4964
65+-- | Drop prefix if exists
66+cutOptionalPrefix :: Text -> Text -> Text
67+cutOptionalPrefix prefix txt = fromMaybe txt $ T.stripPrefix prefix txt
68+
5069-- | Drops prefix and turns first character to lowercase
5170dropPrefixLower :: String -> String
5271dropPrefixLower = over _head toLower . dropPrefix
src/Data/Aeson/Inflections/Generic.hsnew file+46
@@ -0,0 +1,46 @@
1+{-# LANGUAGE KindSignatures #-}
2+
3+module Data.Aeson.Inflections.Generic
4+ -- Reexports from Deriving.Aeson
5+ ( CustomJSON(..)
6+ , FieldLabelModifier
7+ , ConstructorTagModifier
8+ , CamelToSnake
9+ , StripPrefix
10+ , OmitNothingFields
11+
12+ , DropPrefix
13+ , DropPrefixLower
14+ , ToLower
15+ , DefaultFieldLabelModifier
16+ , DefaultJSON
17+ , DefaultEnumJSON
18+ )
19+ where
20+
21+import Data.Aeson.Inflections
22+import Data.Char
23+import Deriving.Aeson
24+import GHC.TypeLits
25+
26+data DropPrefix
27+
28+instance StringModifier DropPrefix where
29+ getStringModifier = dropPrefix
30+
31+data DropPrefixLower
32+
33+instance StringModifier DropPrefixLower where
34+ getStringModifier = dropPrefixLower
35+
36+data ToLower
37+
38+instance StringModifier ToLower where
39+ getStringModifier = map toLower
40+
41+type DefaultFieldLabelModifier = (DropPrefix, CamelToSnake)
42+
43+type DefaultJSON = CustomJSON '[FieldLabelModifier DefaultFieldLabelModifier]
44+
45+type DefaultEnumJSON (prefix :: Symbol)
46+ = CustomJSON '[ConstructorTagModifier (StripPrefix prefix, CamelToSnake)]