https://github.com/mangatmodi/RandomJson
Provides a Kotlin/Java library to create a random json string
https://github.com/mangatmodi/RandomJson
fuzzing fuzzy-testing json testdatabuilder
Last synced: about 2 months ago
JSON representation
Provides a Kotlin/Java library to create a random json string
- Host: GitHub
- URL: https://github.com/mangatmodi/RandomJson
- Owner: mangatmodi
- License: mit
- Created: 2018-12-28T14:43:29.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-11-16T11:44:34.000Z (over 2 years ago)
- Last Synced: 2023-07-28T22:08:06.062Z (over 1 year ago)
- Topics: fuzzing, fuzzy-testing, json, testdatabuilder
- Language: Kotlin
- Homepage:
- Size: 130 KB
- Stars: 71
- Watchers: 5
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-java - RandomJson
README
# RandomJson [](https://www.codefactor.io/repository/github/mangatmodi/randomjson) [](https://travis-ci.com/mangatmodi/RandomJson)
Provides library to create a random json. Provides two implementation of json creation
**[SampleJsonCreator](#SampleJsonCreator)**: Creates JSON string from a sample string.
**[SimpleJsonCreator](#SimpleJsonCreator)**: Creates JSON string by taking number of required keys.
Some important considerations:
1. The random value generations could be customised by giving your own [implementation](#Customize-random-value-generators).
2. The default given implementation is _thread-safe_. That means random strings can be [created](#Parallel-creation-of-random-strings) in different threads
3. The library create one string for each call to `create()`. It does not provides any concurrency or streaming. It totally depends on the developer on how one
wants to use it.#### Java Interoperational
The library can be used in Java 10+. See [example](#Java-example).
### Usage
#### Configuration
First we need to create configuration for the creator. This config specify, the random value generators for each of the
primitive json type. The library includes some basic generator for each type.
```kotlin
val config = RandomJsonConfig(
RandomDouble.threadLocalRandom(),
RandomInt.threadLocalRandom(),
RandomString.charArray("eusbwopw".toCharArray(), 5),
RandomBoolean.uniform(),
RandomString.charArray("abcdefg".toCharArray(), 5)
)```
#### SampleJsonCreator
Creates JSON string similar to `{"key1":{"key2":3}}` in structure but keys and values have random values.
```kotlin
val jsonCreator = RandomJsonCreator
.fromSampleString("""{"key1":{"key2":3}}""", config, KeepKeys.No)
println(jsonCreator.create())
```
Above prints
```json
{"ggdb":{"faea":1279812142}}
```**Keep the same keys as the sample string**
The sample string creator can create json with same keys as the original json.
```kotlin
val input = """{"key1":{"key2":3}}""".trimIndent()val jsonCreator = RandomJsonCreator
.fromSampleString(input, config, KeepKeys.YES)
println(jsonCreator.create())```
Above prints
```json
{"key1":{"key2":2083614805}}
```See more [examples](https://github.com/mangatmodi/RandomJson/blob/master/examples/).
#### SimpleJsonCreator
Creates JSON string similar 2ith 10 keys, the structure of the json is decided by `RandomTypeSelector`,
which specify which which type of json field will be added next. SimpleJsonCreator does not support
arrays or nested json.```kotlin
val jsonCreator = RandomJsonCreator
.fromNumberOfKeys(10,config, RandomTypeSelector.uniform())
println(jsonCreator.create())
```
#### Customize random value generatorsIn the example below `DummyDoubleValue` implements `RandomDouble` to give `2.0`
as the double value. So all the JSON strings created by `jsonCreator` below will contain `2.0` as double value
```kotlin
class DummyDoubleValue:RandomDouble{
override fun next() = 2.0
}
val config = RandomJsonConfig(
DummyDoubleValue(),
RandomInt.threadLocalRandom(),
RandomString.charArray("eusbwopw".toCharArray(), 5),
RandomBoolean.uniform(),
RandomString.charArray("abcdefg".toCharArray(), 5)
)val jsonCreator = RandomJsonCreator
.fromNumberOfKeys(10,config, RandomTypeSelector.uniform())
println(jsonCreator.create())```
#### Parallel creation of random strings
In the example below, we used kotlin's [coroutines](https://kotlinlang.org/docs/reference/coroutines/coroutines-guide.html)
based `async-await` util to create 10 json strings in parallel.
```kotlin
val jsonCreator = RandomJsonCreator
.fromNumberOfKeys(10,config, RandomTypeSelector.uniform())
val tasks = (1..10).map {
async {
println(jsonCreator.create())
}
}
tasks.forEach{ it.await()}```
#### Java example
```java
RandomJsonConfig config = new RandomJsonConfig(
RandomDouble.threadLocalRandom(),
RandomInt.threadLocalRandom(),
RandomString.charArray("eusbwopw".toCharArray(), 5),
RandomBoolean.uniform(),
RandomString.charArray("abcdefg".toCharArray(), 5)
);RandomJsonCreator creator = RandomJsonCreator.fromSampleString(
"{\"q\":1}",
config,
KeepKeys.no()
);System.out.println(creator.create())
```
### Install
The library could be installed from maven central**Maven**
```mavencom.github.mangatmodi
randomjson
2.1.2```
**Gradle**
```gradle
compile group: 'com.github.mangatmodi', name: 'randomjson', version: '2.1.2'
```