Question

How can I suffix the following Aeson Lens expression

>>> "{\"a\": 4, \"b\": 7}" & members . _Number *~ 10
"{\"a\":40,\"b\":70}"

so that the result is a Value (with an Object constructor) and not a String?

Était-ce utile?

La solution

You can use the _Value prism to convert to Maybe Value, then proceed from there. The flipped fmap operator <&> from the lens library provides nice syntax for cases like this:

"{\"a\": 4, \"b\": 7}"^? _Value <&> members . _Number *~ 10
-- Just (Object fromList [("a",Number 40.0),("b",Number 70.0)])

Autres conseils

You can use decode from aeson to parse your string and then use lenses as before:

ghci> (decode "{\"a\": 4, \"b\": 7}" :: Maybe Value ) & _Just . members . _Number *~ 10
Just (Object fromList [("a",Number 40.0),("b",Number 70.0)]) 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top