@hackage microlens-aeson2.0.0
Law-abiding lenses for Aeson, using microlens.
Categories
License
MIT
Maintainer
Colin Woodbury <colingw@gmail.com>
Links
Versions
Installation
CustomDependencies (9)
- aeson >=0.7.0.5 && <0.11
- attoparsec >=0.10 && <0.14
- base >=4.8 && <5
- bytestring >=0.9 && <0.11
- microlens >=0.3 && <0.4
- scientific >=0.3.2 && <0.4 Show all…
Dependents (15)
@hackage/web3-ethereum, @hackage/postgrest, @hackage/pagure-cli, @hackage/nanq, @cardano/cardano-cli, @cardano/cardano-api, Show all…
Package Flags
test-doctests(on by default)
microlens-aeson
microlens-aeson provides Traversals for the
Aeson library's Value type,
while obeying the Traversal laws.
microlens-aeson is derived from lens-aeson, but is based upon microlens
to reduce the amount of dependencies involved.
Here is the dependency graph for lens-aeson:

And that for microlens-aeson:

Usage
microlens-aeson provides Traversals into both lazy and strict variants
of all the text types. Here are some examples:
{-# LANGUAGE OverloadedStrings #-}
import Data.Aeson
import Data.Text (Text)
import Lens.Micro.Aeson
--------------------------
-- Manipulating primatives
--------------------------
-- | Optionally getting one value
a :: Maybe Int
a = ("37" :: Text) ^? _Integer -- Just 42
-- | Setting one value within encoded JSON
b :: Maybe Text
b = "true" & _Bool .~ False -- "false"
----------------------
-- Manipulating arrays
----------------------
-- | Get all values as an Aeson type.
c :: [Value]
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]
-- | Access a specific index, and set a `Value` directly.
d :: Text
d = "[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}"
-- | Access via a given key.
f :: Maybe Value
f = ("{\"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
-- Just (Object (fromList [("a",Number 4.0),("b",Number 7.0)]))
See the Haddock documentation for a full API specification.