Giter Site home page Giter Site logo

randomjson's Introduction

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'

randomjson's People

Contributors

codacy-badger avatar ex-ark avatar mangatmodi avatar tipsy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

randomjson's Issues

Support string type detection

Json string datatype could be detected for following types

  1. Email Address
  2. UUID
  3. Phone number
  4. IP Address, and etc.

Allow users to select string type detection, to create more realistic JSON strings.

Java interop issue

Steps to reproduce

  1. Try to create a java example with the library
  2. Doesn't compile

Reason
Java code produced contains function name as default(), which is a keyword in Java.

Fix
Introduce new function name. Depreciate the default function

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.