Here is the definition for a Json Value :
-- | A JSON value represented as a Haskell value.
data Value = Object !Object
| Array !Array
| String !Text
| Number !Scientific
| Bool !Bool
| Null
deriving (Eq, Show)
let value = String "myValue"
looking for => fromString value == "myValue" ??
fromString :: Value -> Text
I'm looking a function like where I could get the Text from String without to do some pattern matching, obviously this function will be unsafe... a fromString like fromJust in Data.Maybe for example... Something in Data.Lens.Aeson ?
As Thomas M. DuBuisson implies in the above comment, this sounds like an XY Problem. Still, I'll do my best to nonetheless address the specifics.
Technically, you can trivially write a function with the type
Value -> Text
, although it requires pattern matching. I realise that the OP requests a function without pattern matching, but please read on:Such a function compiles, but is unsafe!
While it works for
String
values, it crashes for any other type of value. While it's technically possible to write and compile unsafe functions like the above, AFAICT it's not considered idiomatic Haskell.A better alternative is a safe function that returns
Maybe Text
. This is still easy to write using pattern matching:This function is total:
If you don't wish to write such a function yourself, but prefer to use lens-aeson, you can use the
_String
prism:As you can tell, this is also safe, in that
^? _String
returnsMaybe Text
:If you really, really want an unsafe function, you can use
^?! _String
:It is, not surprisingly, unsafe:
I wonder why you're asking about this sort of functionality, though. Is there a specific problem you're trying to solve with Aeson that we can help with?