Skip to content

Commit

Permalink
json (de)serialization: special-case type_ to work around #412
Browse files Browse the repository at this point in the history
Conflicts:
	persistent/persistent.cabal
  • Loading branch information
hlian authored and gregwebs committed May 5, 2016
1 parent 350720f commit ecb69c6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions persistent-template/Database/Persist/TH.hs
Expand Up @@ -1534,7 +1534,7 @@ mkJSON mps def = do
(objectE `AppE` ListE pairs)
pairs = zipWith toPair (entityFields def) xs
toPair f x = InfixE
(Just (packE `AppE` LitE (StringL $ unpack $ unHaskellName $ fieldHaskell f)))
(Just (packE `AppE` LitE (StringL $ unpack $ unHaskellNameForJSON $ fieldHaskell f)))
dotEqualE
(Just $ VarE x)
fromJSONI = typeInstanceD ''FromJSON (mpsGeneric mps) typ [parseJSON']
Expand All @@ -1551,7 +1551,7 @@ mkJSON mps def = do
toPull f = InfixE
(Just $ VarE obj)
(if maybeNullable f then dotColonQE else dotColonE)
(Just $ AppE packE $ LitE $ StringL $ unpack $ unHaskellName $ fieldHaskell f)
(Just $ AppE packE $ LitE $ StringL $ unpack $ unHaskellNameForJSON $ fieldHaskell f)
case mpsEntityJSON mps of
Nothing -> return [toJSONI, fromJSONI]
Just entityJSON -> do
Expand Down
8 changes: 8 additions & 0 deletions persistent/Database/Persist/Types/Base.hs
Expand Up @@ -144,6 +144,14 @@ newtype HaskellName = HaskellName { unHaskellName :: Text }
newtype DBName = DBName { unDBName :: Text }
deriving (Show, Eq, Read, Ord)

-- | This special-cases "type_" and strips out its underscore. When
-- used for JSON serialization and deserialization, it works around
-- <https://github.com/yesodweb/persistent/issues/412>
unHaskellNameForJSON :: HaskellName -> Text
unHaskellNameForJSON = fixTypeUnderscore . unHaskellName
where fixTypeUnderscore "type_" = "type"
fixTypeUnderscore name = name

type Attr = Text

data FieldType
Expand Down
2 changes: 1 addition & 1 deletion persistent/persistent.cabal
@@ -1,5 +1,5 @@
name: persistent
version: 2.5
version: 2.5.0.1
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>
Expand Down

0 comments on commit ecb69c6

Please sign in to comment.