mangatmodi / RandomJson

Provides a Kotlin/Java library to create a random json string

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RandomJson CodeFactor Build Status

Provides library to create a random json. Provides two implementation of json creation

SampleJsonCreator: Creates JSON string from a sample string.

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.
  2. The default given implementation is thread-safe. That means random strings can be created 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.

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.

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.

val jsonCreator = RandomJsonCreator
    .fromSampleString("""{"key1":{"key2":3}}""", config, KeepKeys.No)
    println(jsonCreator.create())            
    

Above prints

{"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.

    val input = """{"key1":{"key2":3}}""".trimIndent()

    val jsonCreator = RandomJsonCreator
        .fromSampleString(input, config, KeepKeys.YES)
    println(jsonCreator.create())

Above prints

{"key1":{"key2":2083614805}}

See more 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.

val jsonCreator = RandomJsonCreator
    .fromNumberOfKeys(10,config, RandomTypeSelector.uniform())
    println(jsonCreator.create())            
    

Customize random value generators

In 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

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 based async-await util to create 10 json strings in parallel.

val jsonCreator = RandomJsonCreator
    .fromNumberOfKeys(10,config, RandomTypeSelector.uniform())
            
val tasks =  (1..10).map {
    async {
          println(jsonCreator.create())
        }
    }
    
tasks.forEach{ it.await()}

Java example

        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

<dependency>
    <groupId>com.github.mangatmodi</groupId>
    <artifactId>randomjson</artifactId>
    <version>2.1.2</version>
</dependency>

Gradle

compile group: 'com.github.mangatmodi', name: 'randomjson', version: '2.1.2'

About

Provides a Kotlin/Java library to create a random json string

License:MIT License


Languages

Language:Kotlin 93.6%Language:Java 6.4%