diff options
| author | Chris Wells <chris@mathematicaster.org> | 2025-02-04 10:26:00 -0600 |
|---|---|---|
| committer | Chris Wells <chris@mathematicaster.org> | 2025-02-04 10:26:00 -0600 |
| commit | 7085075a90c03025e5d53a256d42e317805b879b (patch) | |
| tree | 69d2dfe53cf35258818482bec3b2ff52acfdb37d /src/Data/Name.hs | |
| parent | ceb898747ad287e83fe18f81a955c186b7abac8e (diff) | |
| download | main-7085075a90c03025e5d53a256d42e317805b879b.tar main-7085075a90c03025e5d53a256d42e317805b879b.tar.gz main-7085075a90c03025e5d53a256d42e317805b879b.tar.bz2 main-7085075a90c03025e5d53a256d42e317805b879b.tar.lz main-7085075a90c03025e5d53a256d42e317805b879b.tar.xz main-7085075a90c03025e5d53a256d42e317805b879b.tar.zst main-7085075a90c03025e5d53a256d42e317805b879b.zip | |
Working on alias resolution
Diffstat (limited to 'src/Data/Name.hs')
| -rw-r--r-- | src/Data/Name.hs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/src/Data/Name.hs b/src/Data/Name.hs index 9572709..b1f2739 100644 --- a/src/Data/Name.hs +++ b/src/Data/Name.hs @@ -10,6 +10,7 @@ module Data.Name ) where import Prolog +import Data.Aeson import Data.Char (isSpace) import Data.List qualified as L import Data.Text qualified as T @@ -19,7 +20,7 @@ data Name a = Name { lastName :: !a , firstName :: !a , middleName :: !(Maybe a) - } deriving (Eq, Functor, Foldable, Generic, Generic1, Ord, Show, Traversable) + } deriving (Eq, Functor, Foldable, Generic, Generic1, Hashable, Hashable1, Ord, Show, Traversable) type Name_ = Name StrictText @@ -29,6 +30,33 @@ instance Applicative Name where pure a = Name a a (pure a) Name l f m <*> Name l' f' m' = Name (l l') (f f') (m <*> m') +instance ToJSON a => ToJSON (Name a) where + toJSON (Name l f m) = object $ + [ "first" .= f + , "last" .= l + ] <> maybe [] (pure . ("middle" .=)) m + toEncoding (Name l f m) = pairs $ fold $ + [ "first" .= f + , "last" .= l + ] <> maybe [] (pure . ("middle" .=)) m + +instance StringConv s StrictText => ToJSONKey (Name s) where + toJSONKey = showNameLFM . toSL >$< toJSONKey + +instance StringConv StrictText s => FromJSONKey (Name s) where + fromJSONKey = FromJSONKeyTextParser $ fmap toSL . (readNameLFM <|> readNameFML) + + + +instance StringConv StrictText s => FromJSON (Name s) where + parseJSON (Object v) = fmap toSL $ Name + <$> v .: "first" + <*> v .: "last" + <*> v .:? "middle" .!= Nothing + parseJSON (String t) = toSL <$> readNameLFM t + parseJSON _ = empty + + readNameLFM :: Alternative m => StrictText -> m Name_ readNameLFM t = T.map (\c -> if c == '~' then ' ' else c) <<$>> |
