Created
November 20, 2017 17:38
-
-
Save chrisdone/7b0c4ebb5b9b94514959206df8992076 to your computer and use it in GitHub Desktop.
Arbitrary Value (Aeson)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
instance Arbitrary Aeson.Value where | |
arbitrary = sized sizedArbitraryValue | |
sizedArbitraryValue :: Int -> Gen Aeson.Value | |
sizedArbitraryValue n | |
| n <= 0 = oneof [pure Aeson.Null, bool, number, string] | |
| otherwise = resize n' $ oneof [pure Aeson.Null, bool, number, string, array, object'] | |
where | |
n' = n `div` 2 | |
bool = Aeson.Bool <$> arbitrary | |
number = (Aeson.Number . fromRational . toRational :: Double -> Aeson.Value) <$> arbitrary | |
string = (Aeson.String . T.pack) <$> arbitrary | |
array = (Aeson.Array . V.fromList) <$> arbitrary | |
object' = (Aeson.Object . HM.fromList . map (first T.pack)) <$> arbitrary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment