Safe Haskell | None |
---|
- nth :: (FromJSON v, ToJSON v) => Int -> IndexedLens' ValueIx (Maybe Value) (Maybe v)
- nth' :: (FromJSON u, ToJSON v) => Int -> IndexedLens ValueIx (Maybe Value) (Maybe Value) (Maybe u) (Maybe v)
- key :: (FromJSON v, ToJSON v) => Text -> IndexedLens' ValueIx (Maybe Value) (Maybe v)
- key' :: (FromJSON u, ToJSON v) => Text -> IndexedLens ValueIx (Maybe Value) (Maybe Value) (Maybe u) (Maybe v)
- asDouble :: Lens' (Maybe Value) (Maybe Double)
- asText :: Lens' (Maybe Value) (Maybe Text)
- asBool :: Lens' (Maybe Value) (Maybe Bool)
- traverseArray :: (FromJSON v, ToJSON v) => IndexedTraversal' Int (Maybe Value) (Maybe v)
- traverseArray' :: (FromJSON u, ToJSON v) => IndexedTraversal Int (Maybe Value) (Maybe Value) (Maybe u) (Maybe v)
- traverseObject :: (FromJSON v, ToJSON v) => IndexedTraversal' Text (Maybe Value) (Maybe v)
- traverseObject' :: (FromJSON u, ToJSON v) => IndexedTraversal Text (Maybe Value) (Maybe Value) (Maybe u) (Maybe v)
- data ValueIx
- valueAt :: (FromJSON u, ToJSON v) => ValueIx -> IndexedLens ValueIx (Maybe Value) (Maybe Value) (Maybe u) (Maybe v)
Lenses
nth :: (FromJSON v, ToJSON v) => Int -> IndexedLens' 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 = Nothing & nth 0 .~ Just 1
>>>
L.unpack $ encode x
"[1]">>>
let y = x & nth 1 .~ Just "hoge"
>>>
L.unpack $ encode y
"[1,\"hoge\"]">>>
let z = y & nth 0 .~ Just False
>>>
L.unpack $ encode z
"[false,\"hoge\"]"
>>>
let v = decode (L.pack "[]") :: Maybe Value
>>>
v & nth 0 .~ Just "hello"
Just (Array (fromList [String "hello"]))>>>
v & nth 1 .~ Just "hello"
Just (Array (fromList [Null,String "hello"]))
nth' :: (FromJSON u, ToJSON v) => Int -> IndexedLens ValueIx (Maybe Value) (Maybe Value) (Maybe u) (Maybe v)Source
key :: (FromJSON v, ToJSON v) => Text -> IndexedLens' 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 = Nothing & key (T.pack "a") .~ Just 2.23
>>>
L.unpack $ encode w
"{\"a\":2.23}">>>
let x = w & key (T.pack "b") . key (T.pack "c") .~ Just True
>>>
L.unpack $ encode x
"{\"b\":{\"c\":true},\"a\":2.23}"
key' :: (FromJSON u, ToJSON v) => Text -> IndexedLens ValueIx (Maybe Value) (Maybe Value) (Maybe u) (Maybe v)Source
asDouble :: Lens' (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 :: Lens' (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 :: Lens' (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 :: (FromJSON v, ToJSON v) => IndexedTraversal' Int (Maybe Value) (Maybe v)Source
Indexed traversal of Array
>>>
let v = decode (L.pack "[1, true, null]") :: Maybe Value
>>>
v & catMaybes . toListOf traverseArray :: [Value]
[Number 1,Bool True,Null]>>>
let w = decode (L.pack "[{\"name\": \"tanakh\", \"age\": 29}, {\"name\": \"nushio\", \"age\": 28}]") :: Maybe Value
>>>
w & catMaybes . toListOf (traverseArray . key (T.pack "name")) :: [T.Text]
["tanakh","nushio"]
traverseArray' :: (FromJSON u, ToJSON v) => IndexedTraversal Int (Maybe Value) (Maybe Value) (Maybe u) (Maybe v)Source
Type-changing indexed traversal of an Array
traverseObject :: (FromJSON v, ToJSON v) => IndexedTraversal' 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
>>>
w & catMaybes . toListOf (traverseArray . traverseObject) :: [Value]
[String "tanakh",Number 29,String "nushio",Number 28]
traverseObject' :: (FromJSON u, ToJSON v) => IndexedTraversal Text (Maybe Value) (Maybe Value) (Maybe u) (Maybe v)Source
Type-changing indexed traversal of Object