Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Aeson utility functions (map, traverse, etc)
branch: master

This branch is even with jb55:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
Data/Aeson
LICENSE
Makefile
Readme.md
Setup.hs
aeson-util.cabal

Readme.md

Data.Aeson.Util

module Data.Aeson.Util (
  traverseWithKey
, traverse
, map
) where
import           Data.Aeson
import           Prelude hiding (map)
import qualified Prelude as P
import           Data.Text (Text)
import           Control.Applicative
import qualified Data.Traversable as Traversable
import qualified Data.HashMap.Lazy as Map

Transform Values by accumulating an Applicative result from every value. The first argument to transforming function will be Nothing if it is not an Object key

traverseWithKey :: Applicative f => (Maybe Text -> Value -> f Value) 
                -> Value -> f Value
traverseWithKey fn = go Nothing
  where
    go _ (Array a)  = Array  <$> Traversable.traverse (go Nothing) a
    go _ (Object o) = Object <$> Map.traverseWithKey (go . Just) o
    go mt v         = fn mt v

Traverse singleton Values, object properties and array Values, accumulating an Applicative result along the way.

traverse :: Applicative f => (Value -> f Value) -> Value -> f Value
traverse fn = traverseWithKey $ const fn

Transform singleton Values, object properties and array Values

map :: (Value -> Value) -> Value -> Value
map fn = go
  where
    go (Array xs) = Array $ fmap go xs
    go (Object x) = Object $ Map.map go x
    go v          = fn v
Something went wrong with that request. Please try again.