문제

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?

도움이 되었습니까?

해결책

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)])

다른 팁

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)]) 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top