Assume the following simple JSON document:
{
"key" : "val1"
}
I'd like to update the value of "key" but at the same time also change its type, so from string change it to an int. Now, using an HCursor like below it is possible and straight forward to do it:
val cursor = js.hcursor
val position = (cursor --\ "key") >-> (_ => jNumber(1))
By "undoing" the above position I end up having a new json where "key" has a numerical value and not a String, which is perfect.
Is it possible to do the same using lenses? I tried to do the following:
val lense = jObjectPL >=>
jsonObjectPL("key") >=>
jNumberPL
lense.mod(_ => JsonBigDecimal(1), js)
But although I do not get an error it also doesn't work, at the end I end up with the original json document unmodified. If I respect the datatype though, things work as they should. Is there a reason that lenses should be used for modifications of the same datatype only? Or I'm just doing something terribly wrong :)