Bryan O'Sullivan avatar Bryan O'Sullivan committed fdbc70c

Split the JSON class in two

Comments (0)

Files changed (3)

       Array
     , Object
     , Value(..)
-    , JSON(..)
+    , FromJSON(..)
+    , ToJSON(..)
     , (.=)
     , (.:)
     , object

Data/Aeson/Encode.hs

 
 import Blaze.ByteString.Builder
 import Blaze.ByteString.Builder.Char.Utf8
-import Data.Aeson.Types (JSON(..), Value(..))
+import Data.Aeson.Types (ToJSON(..), Value(..))
 import Data.Monoid (mappend, mconcat)
 import qualified Data.ByteString.Char8 as B
 import qualified Data.ByteString.Lazy.Char8 as L
 string :: T.Text -> Builder
 string s = fromChar '"' `mappend` quote s `mappend` fromChar '"'
   where
-    quote s = case T.uncons t of
+    quote q = case T.uncons t of
                 Just (c,t') -> mconcat [fromText h, fromText (escape c),
                                         quote t']
                 Nothing -> fromText h
-        where (h,t) = T.break isEscape s
+        where (h,t) = T.break isEscape q
     isEscape c = c == '\"' || c == '\n' || c == '\r' || c == '\t'
     escape '\"' = "\\\""
     escape '\n' = "\\n"
     escape '\r' = "\\r"
     escape '\t' = "\\t"
+    escape _    = error "Data.Aeson.Encode.build: internal error"
 
-encode :: JSON a => a -> L.ByteString
+encode :: ToJSON a => a -> L.ByteString
 encode = toLazyByteString . build . toJSON
 {-# INLINE encode #-}

Data/Aeson/Types.hs

     , (.:)
     , object
     , Value(..)
-    , JSON(..)
+    , FromJSON(..)
+    , ToJSON(..)
     ) where
 
 import Control.Applicative
            | Null
              deriving (Eq, Show)
 
-(.=) :: JSON a => Text -> a -> Object
+(.=) :: ToJSON a => Text -> a -> Object
 name .= value = M.singleton name (toJSON value)
 {-# INLINE (.=) #-}
 
-(.:) :: (Alternative f, JSON a) => Object -> Text -> f a
+(.:) :: (Alternative f, FromJSON a) => Object -> Text -> f a
 obj .: key = case M.lookup key obj of
                Nothing -> empty
                Just v  -> fromJSON v
 object = Object . M.unions
 {-# INLINE object #-}
 
-class JSON a where
+class ToJSON a where
     toJSON   :: a -> Value
+
+class FromJSON a where
     fromJSON :: Alternative f => Value -> f a
 
-instance (JSON a) => JSON (Maybe a) where
+instance (ToJSON a) => ToJSON (Maybe a) where
     toJSON (Just a) = toJSON a
     toJSON Nothing  = Null
     {-# INLINE toJSON #-}
     
+instance (FromJSON a) => FromJSON (Maybe a) where
     fromJSON Null   = pure Nothing
     fromJSON a      = Just <$> fromJSON a
     {-# INLINE fromJSON #-}
 
-instance JSON Bool where
+instance ToJSON Bool where
     toJSON = Bool
     {-# INLINE toJSON #-}
 
+instance FromJSON Bool where
     fromJSON (Bool b) = pure b
     fromJSON _        = empty
     {-# INLINE fromJSON #-}
 
-instance JSON Double where
+instance ToJSON Double where
     toJSON = Number
     {-# INLINE toJSON #-}
 
+instance FromJSON Double where
     fromJSON (Number n) = pure n
     fromJSON _          = empty
     {-# INLINE fromJSON #-}
 
-instance JSON Int where
+instance ToJSON Int where
     toJSON = Number . fromIntegral
     {-# INLINE toJSON #-}
 
+instance FromJSON Int where
     fromJSON (Number n) = pure (floor n)
     fromJSON _          = empty
     {-# INLINE fromJSON #-}
 
-instance JSON Text where
+instance ToJSON Text where
     toJSON = String
     {-# INLINE toJSON #-}
 
+instance FromJSON Text where
     fromJSON (String t) = pure t
     fromJSON _          = empty
     {-# INLINE fromJSON #-}
 
-instance JSON ByteString where
+instance ToJSON ByteString where
     toJSON = String . decodeUtf8
 
+instance FromJSON ByteString where
     fromJSON (String t) = pure (encodeUtf8 t)
     fromJSON _          = empty
 
     go (a:as) = (:) <$> f a <*> go as
     go []     = pure []
 
-instance (JSON a) => JSON [a] where
+instance (ToJSON a) => ToJSON [a] where
     toJSON = Array . V.fromList . map toJSON
     {-# INLINE toJSON #-}
     
+instance (FromJSON a) => FromJSON [a] where
     fromJSON (Array a) = mapA fromJSON (V.toList a)
     fromJSON _         = empty
     {-# INLINE fromJSON #-}
 
-instance (JSON a) => JSON (Vector a) where
+instance (ToJSON a) => ToJSON (Vector a) where
     toJSON = Array . V.map toJSON
     {-# INLINE toJSON #-}
     
+instance (FromJSON a) => FromJSON (Vector a) where
     fromJSON (Array a) = V.fromList <$> mapA fromJSON (V.toList a)
     fromJSON _         = empty
     {-# INLINE fromJSON #-}
 
-instance JSON Value where
+instance ToJSON Value where
     toJSON a = a
     {-# INLINE toJSON #-}
 
+instance FromJSON Value where
     fromJSON a = pure a
     {-# INLINE fromJSON #-}
 
 -- We happen to use the same JSON formatting for a UTCTime as .NET
 -- does for a DateTime. How handy!
-instance JSON UTCTime where
+instance ToJSON UTCTime where
     toJSON t = String (pack (formatTime defaultTimeLocale "/Date(%s)/" t))
     {-# INLINE toJSON #-}
 
+instance FromJSON UTCTime where
     fromJSON (String t) =
         case parseTime defaultTimeLocale "/Date(%s)/" (unpack t) of
           Just d -> pure d
     fromJSON _          = empty
     {-# INLINE fromJSON #-}
 
-instance (JSON a, JSON b) => JSON (a,b) where
+instance (ToJSON a, ToJSON b) => ToJSON (a,b) where
     toJSON (a,b) = toJSON [toJSON a, toJSON b]
     {-# INLINE toJSON #-}
 
+instance (FromJSON a, FromJSON b) => FromJSON (a,b) where
     fromJSON (Array ab) = case V.toList ab of
                             [a,b] -> (,) <$> fromJSON a <*> fromJSON b
                             _     -> empty
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.