vendredi 6 septembre 2019

How can I generate random values in Haskell for any Value of an aeson Object

I've started coding Haskell few weeks ago. I'm trying to do something like a random JSON generator using aeson.

Suppose that I have a json object like this:

{
    name: "bob",
    age: 13,
    role: "admin"
}

When I use aeson to decode it, I get this:

let Just json = decode str :: Maybe Object

-- fromList [("name",String "bob"),("age",Number 13.0),("role",String "admin")]

What I want is how can I generate a list of JSON objects based on bob:

[
 {
   name: <any random string>,
   age: <any random number>,
   role: <any random string>
 },
 ...
]

I know that I need to fmap over the HashMap values, but I don't have any clue of how can I produce random values based on its types.

I'm not worried about other complex values that can appear on JSON fields yet. I'm focusing on numbers and strings for now.




Aucun commentaire:

Enregistrer un commentaire