aeson-lens-0.3.0.0: Lens of Aeson

Safe HaskellNone

Data.Aeson.Lens

Contents

Synopsis

Lenses

nth :: (ToJSON v, FromJSON v) => Int -> SimpleIndexedLens ValueIx (Maybe Value) (Maybe v)Source

Lens of Array

>>> let v = decode (L.pack "{\"foo\": {\"baz\": 3.14}, \"bar\": [123, false, null]}") :: Maybe Value
>>> v ^. key (T.pack "bar") . nth 1 :: Maybe Bool
Just False
>>> v ^. key (T.pack "bar") . nth 1 :: Maybe String
Nothing
>>> v ^. key (T.pack "bar") . nth 3 :: Maybe Value
Nothing
>>> v ^. nth 0 :: Maybe Value
Nothing
>>> let x = nth 0 .~ Just 1 $ Nothing
>>> L.unpack $ encode x
"[1]"
>>> let y = nth 1 .~ Just "hoge" $ x
>>> L.unpack $ encode y
"[1,\"hoge\"]"
>>> let z = nth 0 .~ Just False $ y
>>> L.unpack $ encode z
"[false,\"hoge\"]"

key :: (ToJSON v, FromJSON v) => Text -> SimpleIndexedLens ValueIx (Maybe Value) (Maybe v)Source

Lens of Object

>>> let v = decode (L.pack "{\"foo\": {\"baz\": 3.14}, \"bar\": [123, false, null]}") :: Maybe Value
>>> v ^. key (T.pack "foo") . key (T.pack "baz") :: Maybe Double
Just 3.14
>>> v ^. key (T.pack "foo") . key (T.pack "baz") :: Maybe Object
Nothing
>>> v ^. key (T.pack "foo") . key (T.pack "hoge") :: Maybe Value
Nothing
>>> v ^. key (T.pack "hoge") :: Maybe Value
Nothing
>>> let w = key (T.pack "a") .~ Just 2.23 $ Nothing
>>> L.unpack $ encode w
"{\"a\":2.23}"
>>> let x = key (T.pack "b") . key (T.pack "c") .~ Just True $ w
>>> L.unpack $ encode x
"{\"b\":{\"c\":true},\"a\":2.23}"

asDouble :: SimpleLens (Maybe Value) (Maybe Double)Source

Lens of Double

>>> let v = decode (L.pack "{\"foo\": {\"baz\": 3.14}, \"bar\": [123, false, null]}") :: Maybe Value
>>> v ^. key (T.pack "foo") . key (T.pack "baz") . asDouble
Just 3.14
>>> v ^. key (T.pack "bar") . asDouble
Nothing
>>> v ^. key (T.pack "hoge") . asDouble
Nothing

asText :: SimpleLens (Maybe Value) (Maybe Text)Source

Lens of Text

>>> let v = decode (L.pack "{\"foo\": {\"baz\": \"3.14\"}, \"bar\": [123, false, null]}") :: Maybe Value
>>> v ^. key (T.pack "foo") . key (T.pack "baz") . asText
Just "3.14"
>>> v ^. key (T.pack "bar") . asText
Nothing
>>> v ^. key (T.pack "hoge") . asText
Nothing

asBool :: SimpleLens (Maybe Value) (Maybe Bool)Source

Lens of Bool

>>> let v = decode (L.pack "{\"foo\": {\"baz\": false}, \"bar\": [123, false, null]}") :: Maybe Value
>>> v ^. key (T.pack "foo") . key (T.pack "baz") . asBool
Just False
>>> v ^. key (T.pack "bar") . asBool
Nothing
>>> v ^. key (T.pack "hoge") . asBool
Nothing

Traversals

traverseArray :: (ToJSON v, FromJSON v) => SimpleIndexedTraversal Int (Maybe Value) (Maybe v)Source

Indexed traversal of Array

>>> let v = decode (L.pack "[1, true, null]") :: Maybe Value
>>> catMaybes . toListOf traverseArray $ v :: [Value]
[Number 1,Bool True,Null]
>>> let w = decode (L.pack "[{\"name\": \"tanakh\", \"age\": 29}, {\"name\": \"nushio\", \"age\": 28}]") :: Maybe Value
>>> catMaybes . toListOf (traverseArray . key (T.pack "name")) $ w :: [T.Text]
["tanakh","nushio"]

traverseObject :: (ToJSON v, FromJSON v) => SimpleIndexedTraversal Text (Maybe Value) (Maybe v)Source

Indexed traversal of Object

>>> let w = decode (L.pack "[{\"name\": \"tanakh\", \"age\": 29}, {\"name\": \"nushio\", \"age\": 28}]") :: Maybe Value
>>> catMaybes . toListOf (traverseArray . traverseObject) $ w :: [Value]
[String "tanakh",Number 29,String "nushio",Number 28]