Commits

Bryan O'Sullivan committed dd7a349

Switch to the new Aeson types

Comments (0)

Files changed (3)

 name:                riak
-version:             0.1.0.1
+version:             0.2.0.0
 synopsis:            A Haskell client for the Riak decentralized data store
 description:
   A Haskell client library for the Riak decentralized data
     Network.Riak.Protocol
   
   build-depends:       
-    aeson == 0.1.*,
+    aeson == 0.2.*,
     attoparsec >= 0.8.4.0,
     base == 4.*,
     binary,

src/Network/Riak/JSON.hs

 
 import Control.Applicative ((<$>))
 import Control.Arrow (first)
+import Data.Aeson.Types (FromJSON(..), ToJSON(..))
 import Data.Monoid (Monoid)
 import Data.Typeable (Typeable)
 import Network.Riak.Types.Internal
-import Data.Aeson.Types (FromJSON(..), ToJSON(..))
 import qualified Network.Riak.Value as V
 
 newtype JSON a = J {
     {-# INLINE fmap #-}
 
 instance (FromJSON a, ToJSON a) => V.IsContent (JSON a) where
-    fromContent c = J `fmap` (V.fromContent c >>= fromJSON)
-    {-# INLINE fromContent #-}
+    parseContent c = J `fmap` (V.parseContent c >>= parseJSON)
+    {-# INLINE parseContent #-}
 
     toContent (J a) = V.toContent (toJSON a)
     {-# INLINE toContent #-}

src/Network/Riak/Value.hs

 module Network.Riak.Value
     (
       IsContent(..)
+    , fromContent
     , get
     , getMany
     , put
     , putMany_
     ) where
 
-import Data.Attoparsec.Lazy (maybeResult, parse)
+import Control.Applicative
+import qualified Data.Attoparsec.Lazy as A
 import Data.Foldable (toList)
 import Network.Riak.Connection.Internal
 import Network.Riak.Protocol.Content (Content(..))
 import qualified Data.Sequence as Seq
 import qualified Network.Riak.Content as C
 import qualified Network.Riak.Request as Req
+import Data.Aeson.Types (Parser, Result(..), parse)
+
+fromContent :: IsContent c => Content -> Maybe c
+fromContent c = case parse parseContent c of
+                  Success a -> Just a
+                  Error _   -> Nothing
 
 class IsContent c where
-    fromContent :: Content -> Maybe c
+    parseContent :: Content -> Parser c
     toContent :: c -> Content
 
 instance IsContent Content where
-    fromContent = Just
-    {-# INLINE fromContent #-}
+    parseContent = return
+    {-# INLINE parseContent #-}
 
     toContent v = v
     {-# INLINE toContent #-}
 
 instance IsContent () where
-    fromContent c | c == C.empty = Just ()
-                  | otherwise    = Nothing
-    {-# INLINE fromContent #-}
+    parseContent c | c == C.empty = pure ()
+                   | otherwise    = empty
+    {-# INLINE parseContent #-}
 
     toContent _ = C.empty
     {-# INLINE toContent #-}
 
 instance IsContent Aeson.Value where
-    fromContent c | content_type c == Just "application/json" =
-                      maybeResult (parse Aeson.json (value c))
-                  | otherwise = Nothing
-
+    parseContent c | content_type c == Just "application/json" =
+                      case A.parse Aeson.json (value c) of
+                        A.Done _ a     -> return a
+                        A.Fail _ _ err -> fail err
+                   | otherwise = fail "non-JSON document"
     toContent = C.json
     {-# INLINE toContent #-}
 
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.