Commits

Bryan O'Sullivan committed 9813041

Get rid of "_"-prefixed field name mangling.

This fixes gh-53.

  • Participants
  • Parent commits be3f922

Comments (0)

Files changed (2)

File Data/Aeson/Generic.hs

       where tyrep = typeOf . head . H.keys $ m
             remap f = Object . mapKeyVal (f . fromJust . cast) toJSON $ m
 
--- Skip leading '_' in field name so we can use keywords
--- etc. as field names.
-mungeField :: String -> Text
-mungeField ('_':cs) = pack cs
-mungeField cs       = pack cs
-
 toJSON_generic :: (Data a) => a -> Value
 toJSON_generic = generic
   where
         encodeArgs c = encodeArgs' (constrFields c)
         encodeArgs' [] [j] = j
         encodeArgs' [] js  = Array . V.fromList $ js
-        encodeArgs' ns js  = object $ zip (map mungeField ns) js
+        encodeArgs' ns js  = object $ zip (map pack ns) js
 
 
 fromJSON :: (Data a) => Value -> Result a
                          (j':js') -> do put js'; lift $ parseJSON j'
 
         -- Select the named fields from a JSON object.
-        selectFields fjs = mapM sel
-          where sel f = maybe (modFail "parseJSON" $ "field does not exist " ++
-                               f) return $ H.lookup (mungeField f) fjs
+        selectFields fjs = mapM $ \f ->
+           maybe (modFail "parseJSON" $ "field does not exist " ++ f) return $
+             H.lookup (pack f) fjs
 
         -- Count how many arguments a constructor has.  The value x is
         -- used to determine what type the constructor returns.

File tests/Properties.hs

 instance Arbitrary Foo where
     arbitrary = liftM4 Foo arbitrary arbitrary arbitrary arbitrary
 
-{-
-   Test for Data.Aeson.Generic handling '_' names
--}
 data UFoo = UFoo {
       _UFooInt :: Int
     , uFooInt :: Int