aeson-casing-0.2.0.0/src/0000755000000000000000000000000013436054534013202 5ustar0000000000000000aeson-casing-0.2.0.0/src/Data/0000755000000000000000000000000013436054534014053 5ustar0000000000000000aeson-casing-0.2.0.0/src/Data/Aeson/0000755000000000000000000000000013436057734015125 5ustar0000000000000000aeson-casing-0.2.0.0/src/Data/Aeson/Casing/0000755000000000000000000000000013436057734016331 5ustar0000000000000000aeson-casing-0.2.0.0/test/0000755000000000000000000000000013436054534013372 5ustar0000000000000000aeson-casing-0.2.0.0/test/Data/0000755000000000000000000000000013436054534014243 5ustar0000000000000000aeson-casing-0.2.0.0/test/Data/Aeson/0000755000000000000000000000000013436054534015310 5ustar0000000000000000aeson-casing-0.2.0.0/test/Data/Aeson/Casing/0000755000000000000000000000000013436057734016521 5ustar0000000000000000aeson-casing-0.2.0.0/src/Data/Aeson/Casing/Internal.hs0000644000000000000000000000506313436057734020445 0ustar0000000000000000module Data.Aeson.Casing.Internal where import Data.Aeson.Types import Data.Char -- | Creates an Aeson options object that drops a specific number of characters -- from the front of a field name, then applies a casing function. aesonDrop :: Int -> (String -> String) -> Options aesonDrop n f = defaultOptions { fieldLabelModifier = f . drop n } -- | Creates an Aeson options object that drops the field name prefix from a -- field, then applies a casing function. We assume a convention of the prefix -- always being lower case, and the first letter of the actual field name being -- uppercase. This accommodated for field names in GHC 7.8 and below. -- -- > data Person = Person -- > { personFirstName :: Text -- > , personLastName :: Text -- > } deriving (Generic) -- > -- > data Dog = Dog -- > { dogFirstName :: Text -- > } deriving (Generic) -- -- In the above cases, dog and person are always dropped from the JSON field -- names. aesonPrefix :: (String -> String) -> Options aesonPrefix f = defaultOptions { fieldLabelModifier = f . dropFPrefix } ---- -- | Snake casing, where the words are always lower case and separated by an -- underscore. snakeCase :: String -> String snakeCase = symbCase '_' -- | Train casing, where the words are always lower case and separated by -- a hyphen trainCase :: String -> String trainCase = symbCase '-' -- | Camel casing, where the words are separated by the first letter of each -- word being a capital. However, the first letter of the field is never a -- capital. camelCase :: String -> String camelCase = applyFirst toLower -- | Pascal casing, where the words are separated by the first letter of each -- word being a capital. The first letter of the field is always a capital. pascalCase :: String -> String pascalCase = applyFirst toUpper ---- -- | Generic casing for symbol separated names symbCase :: Char -> (String -> String) symbCase sym = u . applyFirst toLower where u [] = [] u (x:xs) | isUpper x = sym : toLower x : u xs | otherwise = x : u xs applyFirst :: (Char -> Char) -> String -> String applyFirst _ [] = [] applyFirst f [x] = [f x] applyFirst f (x:xs) = f x: xs dropFPrefix :: String -> String dropFPrefix [] = [] dropFPrefix (x:xs) | isUpper x = x : xs | otherwise = dropFPrefix xs dropCPrefix :: String -> String dropCPrefix [] = [] dropCPrefix [x] = [x] dropCPrefix (x0:x1:xs) | isLower x1 = x0 : x1 : xs | otherwise = dropCPrefix (x1 : xs) aeson-casing-0.2.0.0/src/Data/Aeson/Casing.hs0000644000000000000000000000162113436057734016665 0ustar0000000000000000-- | The casing utilities allow you to specify how Aeson renders and parses -- the field names in JSON messages. Snake, Camel, and Pascal case are all -- supported. To include casing modifiers in Aeson, attach options to instances -- of ToJSON and FromJSON. -- -- > data Person = Person -- > { personFirstName :: Text -- > , personLastName :: Text -- > } deriving (Generic) -- > -- > instance ToJSON Person where -- > toJSON = genericToJSON $ aesonPrefix snakeCase -- > instance FromJSON Person where -- > parseJSON = genericParseJSON $ aesonPrefix snakeCase -- -- The above code will produce JSON messages like the following... -- -- > { -- > "first_name": "John", -- > "last_name": "Doe" -- > } module Data.Aeson.Casing ( aesonDrop , aesonPrefix , snakeCase , trainCase , camelCase , pascalCase ) where import Data.Aeson.Casing.Internal aeson-casing-0.2.0.0/test/Main.hs0000644000000000000000000000027113436054534014612 0ustar0000000000000000module Main where import Test.Tasty import qualified Data.Aeson.Casing.Test as Casing main :: IO () main = defaultMain $ testGroup "Tests" [ Casing.tests ] aeson-casing-0.2.0.0/test/Data/Aeson/Casing/Test.hs0000644000000000000000000000454713436057734020006 0ustar0000000000000000{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TemplateHaskell #-} module Data.Aeson.Casing.Test (tests) where import Data.Aeson import GHC.Generics import Test.Tasty import Test.Tasty.HUnit import Test.Tasty.TH import Data.Aeson.Casing.Internal tests :: TestTree tests = $(testGroupGenerator) case_snake :: Assertion case_snake = do snakeCase "SampleField" @=? "sample_field" snakeCase "sampleField" @=? "sample_field" case_camel :: Assertion case_camel = do camelCase "SampleField" @=? "sampleField" camelCase "sampleField" @=? "sampleField" case_pascal :: Assertion case_pascal = do pascalCase "SampleField" @=? "SampleField" pascalCase "sampleField" @=? "SampleField" case_train :: Assertion case_train = do trainCase "SampleField" @=? "sample-field" trainCase "sampleField" @=? "sample-field" case_prefix :: Assertion case_prefix = dropFPrefix "extraSampleField" @=? "SampleField" ---- data Person = Person { personFirstName :: String , personLastName :: String } deriving (Eq, Show, Generic) data Animal = Animal { animalFirstName :: String , animalBreedName :: String } deriving (Eq, Show, Generic) instance ToJSON Person where toJSON = genericToJSON $ aesonPrefix snakeCase instance FromJSON Person where parseJSON = genericParseJSON $ aesonPrefix snakeCase instance ToJSON Animal where toJSON = genericToJSON $ aesonPrefix trainCase instance FromJSON Animal where parseJSON = genericParseJSON $ aesonPrefix trainCase johnDoe = Person "John" "Doe" johnDoeJSON = "{\"first_name\":\"John\",\"last_name\":\"Doe\"}" persianEgypt = Animal "Toffee" "Persian Cat" persianEgyptJSON = "{\"breed-name\":\"Persian Cat\",\"first-name\":\"Toffee\"}" case_encode_snake :: Assertion case_encode_snake = do let b = encode johnDoe b @=? johnDoeJSON case_decode_snake :: Assertion case_decode_snake = do let p = decode johnDoeJSON :: Maybe Person p @=? Just johnDoe case_encode_train :: Assertion case_encode_train = do let b = encode persianEgypt b @=? persianEgyptJSON case_decode_train :: Assertion case_decode_train = do let p = decode persianEgyptJSON :: Maybe Animal p @=? Just persianEgypt aeson-casing-0.2.0.0/LICENSE0000644000000000000000000000204513436054534013421 0ustar0000000000000000Copyright (c) 2015 Andrew Rademacher Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. aeson-casing-0.2.0.0/Setup.hs0000644000000000000000000000005613436054534014050 0ustar0000000000000000import Distribution.Simple main = defaultMain aeson-casing-0.2.0.0/aeson-casing.cabal0000644000000000000000000000267213447164440015755 0ustar0000000000000000 name: aeson-casing version: 0.2.0.0 synopsis: Tools to change the formatting of field names in Aeson instances. description: Tools to change the formatting of field names in Aeson instances. This includes CamelCasing, Pascal Casing and Snake Casing. license: MIT license-file: LICENSE author: Andrew Rademacher maintainer: andrewrademacher@gmail.com category: Data build-type: Simple cabal-version: >=1.10 source-repository head type: git location: https://github.com/AndrewRademacher/aeson-casing.git library hs-source-dirs: src default-language: Haskell2010 exposed-modules: Data.Aeson.Casing.Internal , Data.Aeson.Casing build-depends: base >=4.7 && <5.0 , aeson >=0.8 test-suite casing type: exitcode-stdio-1.0 main-is: Main.hs hs-source-dirs: test default-language: Haskell2010 other-modules: Data.Aeson.Casing.Test build-depends: base >=4.7 && <5.0 , tasty , tasty-quickcheck , tasty-hunit , tasty-th , aeson , aeson-casing