Skip to content

Instantly share code, notes, and snippets.

@chrisdone
Created November 20, 2017 17:38
Show Gist options
  • Save chrisdone/7b0c4ebb5b9b94514959206df8992076 to your computer and use it in GitHub Desktop.
Save chrisdone/7b0c4ebb5b9b94514959206df8992076 to your computer and use it in GitHub Desktop.
Arbitrary Value (Aeson)
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