microlens-aeson 2.0.0 → 2.1.0
raw patch · 6 files changed
+52/−32 lines, 6 filesdep ~microlensbinary-addedPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: microlens
API changes (from Hackage documentation)
- Lens.Micro.Aeson: _JSON :: AsJSON t => Traversal' t Value
+ Lens.Micro.Aeson: _JSON :: (AsJSON t, FromJSON a, ToJSON a) => Traversal' t a
Files
- CHANGELOG.md +6/−0
- README.md +25/−11
- lens-aeson.png binary
- microlens-aeson.cabal +10/−8
- microlens-aeson.png binary
- src/Lens/Micro/Aeson.hs +11/−13
CHANGELOG.md view
@@ -1,6 +1,12 @@+2.1.0+-----+* Restored original `AsJSON` and `_JSON` typing+* Bumped `microlens` dep max+ 2.0.0 ----- * Complete conversion to `microlens`+* All `Prism` are now `Traversal` 1.0.0.5 -------
README.md view
@@ -35,7 +35,7 @@ -------------------------- -- | Optionally getting one value a :: Maybe Int-a = ("37" :: Text) ^? _Integer -- Just 42+a = ("37" :: Text) ^? _Integer -- Just 37 -- | Setting one value within encoded JSON b :: Maybe Text@@ -49,30 +49,44 @@ c = "[1, 2, 3]" ^.. values -- [Number 1.0, Number 2.0, Number 3.0] -- | Get all values cast to some simpler number type.-c :: [Double]-c = "[1, 2, 3]" ^.. values . _Double -- [1.0, 2.0, 3.0]+d :: [Double]+d = "[1, 2, 3]" ^.. values . _Double -- [1.0, 2.0, 3.0] -- | Access a specific index, and set a `Value` directly.-d :: Text-d = "[1,2,3]" & nth 1 .~ Number 20 -- "[1,20,3]"+e :: Text+e = "[1,2,3]" & nth 1 .~ Number 20 -- "[1,20,3]" ----------------------- -- Manipulating objects ----------------------- -- | Access all values of the key/value pairs.-e :: Text-e = "{\"a\":4,\"b\":7}" & members . _Number %~ (*10) -- "{\"a\":40,\"b\":70}"+f :: Text+f = "{\"a\":4,\"b\":7}" & members . _Number %~ (*10) -- "{\"a\":40,\"b\":70}" -- | Access via a given key.-f :: Maybe Value-f = ("{\"a\": 100, \"b\": 200}" :: Text) ^? key "a" -- Just (Number 100.0)+g :: Maybe Value+g = ("{\"a\": 100, \"b\": 200}" :: Text) ^? key "a" -- Just (Number 100.0) ----------------------------------- -- Aeson `Value`s from encoded JSON ------------------------------------g :: Maybe Text-g = "{\"a\":4,\"b\":7}" ^? _Value+h :: Maybe Text+h = "{\"a\":4,\"b\":7}" ^? _Value -- Just (Object (fromList [("a",Number 4.0),("b",Number 7.0)])) ``` See the Haddock documentation for a full API specification.++Migration from Data.Aeson.Lens+------------------------------+The functions provided here are Traversals, not Prisms, therefore+creation of encoded JSON from Haskell types like:++```haskell+>>> _Bool # True :: String+"true"+```++is no longer possible. Otherwise, if your use cases are strictly like+those listed in the Usage section above, then you need only to switch+the import from `Data.Aeson.Lens` to `Lens.Micro.Aeson`.
+ lens-aeson.png view
binary file changed (absent → 1190547 bytes)
microlens-aeson.cabal view
@@ -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@@ -18,12 +18,14 @@ description: Law-abiding lenses for Aeson, using microlens. extra-source-files:- .travis.yml- .ghci- .gitignore- AUTHORS.md- README.md- CHANGELOG.md+ .travis.yml+ .ghci+ .gitignore+ AUTHORS.md+ README.md+ CHANGELOG.md+ microlens-aeson.png+ lens-aeson.png source-repository head type: git@@ -40,7 +42,7 @@ , 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
+ microlens-aeson.png view
binary file changed (absent → 220463 bytes)
src/Lens/Micro/Aeson.hs view
@@ -40,7 +40,6 @@ ) 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)@@ -377,16 +376,17 @@ 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@@ -402,29 +402,27 @@ {-# 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