microlens-aeson: Law-abiding lenses for Aeson, using microlens.

[ library, mit, numeric ] [ Propose Tags ]

Law-abiding lenses for Aeson, using microlens.


[Skip to Readme]

Modules

[Index]

Flags

Manual Flags

NameDescriptionDefault
test-doctestsEnabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 2.0.0, 2.1.0, 2.1.1, 2.1.1.1, 2.1.1.2, 2.1.1.3, 2.2.0, 2.2.0.1, 2.2.0.2, 2.3.0, 2.3.0.1, 2.3.0.2, 2.3.0.3, 2.3.0.4, 2.3.1, 2.4.0, 2.4.1, 2.5.0, 2.5.1, 2.5.2 (info)
Change log CHANGELOG.md
Dependencies 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.5), scientific (>=0.3.2 && <0.4), text (>=0.11.1.10 && <1.3), unordered-containers (>=0.2.3 && <0.3), vector (>=0.9 && <0.12) [details]
License MIT
Copyright Copyright (C) 2012 Paul Wilson Copyright (C) 2013 Edward A. Kmett Copyright (C) 2015 Colin Woodbury
Author Colin Woodbury
Maintainer Colin Woodbury <colingw@gmail.com>
Revised Revision 1 made by HerbertValerioRiedel at 2019-01-02T21:52:26Z
Category Numeric
Home page http://github.com/fosskers/microlens-aeson/
Bug tracker http://github.com/fosskers/microlens-aeson/issues
Source repo head: git clone git://github.com/fosskers/microlens-aeson.git
Uploaded by fosskers at 2016-01-04T20:06:58Z
Distributions Arch:2.5.2, Debian:2.3.1, LTSHaskell:2.5.2, NixOS:2.5.2, Stackage:2.5.2
Reverse Dependencies 6 direct, 3 indirect [details]
Downloads 15035 total (139 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2016-01-04 [all 1 reports]

Readme for microlens-aeson-2.1.0

[back to package description]

microlens-aeson

Build Status

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:

lens-aeson dependencies

And that for microlens-aeson:

microlens-aeson dependencies

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 37

-- | 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.
d :: [Double]
d = "[1, 2, 3]" ^.. values . _Double  -- [1.0, 2.0, 3.0]

-- | Access a specific index, and set a `Value` directly.
e :: Text
e = "[1,2,3]" & nth 1 .~ Number 20  -- "[1,20,3]"

-----------------------
-- Manipulating objects
-----------------------
-- | Access all values of the key/value pairs.
f :: Text
f = "{\"a\":4,\"b\":7}" & members . _Number %~ (*10)  -- "{\"a\":40,\"b\":70}"

-- | Access via a given key.
g :: Maybe Value
g = ("{\"a\": 100, \"b\": 200}" :: Text) ^? key "a"  -- Just (Number 100.0)

-----------------------------------
-- Aeson `Value`s from encoded JSON
-----------------------------------
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:

>>> _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.