Skip to content

Commit

Permalink
Restored original AsJSON and _JSON typing
Browse files Browse the repository at this point in the history
  • Loading branch information
fosskers committed Jan 4, 2016
1 parent 504d1f0 commit 8514254
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
2.1.0
-----
* Restored original `AsJSON` and `_JSON` typing
* Bumped `microlens` dep max

2.0.0
-----
* Complete conversion to `microlens`
Expand Down
4 changes: 2 additions & 2 deletions microlens-aeson.cabal
@@ -1,6 +1,6 @@
name: microlens-aeson
category: Numeric
version: 2.0.0
version: 2.1.0
license: MIT
cabal-version: >= 1.8
license-file: LICENSE
Expand Down Expand Up @@ -42,7 +42,7 @@ library
, attoparsec >= 0.10 && < 0.14
, base >= 4.5 && < 5
, bytestring >= 0.9 && < 0.11
, microlens >= 0.3 && < 0.4
, microlens >= 0.3 && < 0.5
, scientific >= 0.3.2 && < 0.4
, text >= 0.11.1.10 && < 1.3
, unordered-containers >= 0.2.3 && < 0.3
Expand Down
24 changes: 11 additions & 13 deletions src/Lens/Micro/Aeson.hs
Expand Up @@ -40,7 +40,6 @@ module Lens.Micro.Aeson
) where

import Data.Aeson
import Data.Aeson.Parser (value)
import Data.Attoparsec.ByteString.Lazy (maybeResult, parse)
import qualified Data.ByteString as Strict
import Data.ByteString.Lazy.Char8 as Lazy hiding (putStrLn)
Expand Down Expand Up @@ -377,16 +376,17 @@ lazyTextUtf8 = lens LazyText.encodeUtf8 (const LazyText.decodeUtf8)
class AsJSON t where
-- | '_JSON' is a 'Traversal' from something containing JSON
-- to something encoded in that structure.
_JSON :: Traversal' t Value
_JSON :: (FromJSON a, ToJSON a) => Traversal' t a

instance AsJSON Strict.ByteString where
_JSON = lazyUtf8 . _JSON
{-# INLINE _JSON #-}

instance AsJSON Lazy.ByteString where
_JSON f b = case maybeResult (parse value b) of
Just v -> encode <$> f v
_ -> pure b
_JSON f b = maybe (pure b) (\v' -> encode <$> f v') v
where v = maybeResult (parse json b) >>= \x -> case fromJSON x of
Success x' -> Just x'
_ -> Nothing
{-# INLINE _JSON #-}

instance AsJSON String where
Expand All @@ -402,29 +402,27 @@ instance AsJSON LazyText.Text where
{-# INLINE _JSON #-}

instance AsJSON Value where
_JSON = id
_JSON f v = case fromJSON v of
Success v' -> toJSON <$> f v'
_ -> pure v
{-# INLINE _JSON #-}

------------------------------------------------------------------------------
-- Some additional tests for prismhood; see https://github.com/ekmett/lens/issues/439.
------------------------------------------------------------------------------

-- $LazyByteStringTests
-- >>> ("42" :: Lazy.ByteString) ^? _JSON
-- >>> ("42" :: Lazy.ByteString) ^? (_JSON :: Traversal' Lazy.ByteString Value)
-- Just (Number 42.0)
--
-- >>> ("42" :: Lazy.ByteString) ^? _Integer
-- Just 42

-- $StrictByteStringTests
-- >>> ("42" :: Strict.ByteString) ^? _JSON
-- >>> ("42" :: Strict.ByteString) ^? (_JSON :: Traversal' Strict.ByteString Value)
-- Just (Number 42.0)
--
-- >>> ("42" :: Lazy.ByteString) ^? _Integer
-- Just 42

-- $StringTests
-- >>> ("42" :: String) ^? _JSON
-- >>> ("42" :: String) ^? (_JSON :: Traversal' String Value)
-- Just (Number 42.0)
--
-- >>> ("42" :: String) ^? _Integer
Expand Down

0 comments on commit 8514254

Please sign in to comment.