GithubHelp home page GithubHelp logo

hvr / microlens-aeson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lens/lens-aeson

0.0 2.0 0.0 3.08 MB

Traversals for Data.Aeson, based on microlens for minimal dependencies

License: MIT License

Haskell 100.00%

microlens-aeson's Introduction

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.

microlens-aeson's People

Contributors

ekmett avatar fosskers avatar glguy avatar statusfailed avatar bennofs avatar supki avatar arkeet avatar bitemyapp avatar ijuma avatar nomeata avatar markus1189 avatar mgsloan avatar phadej avatar

Watchers

Herbert Valerio Riedel avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.