{"id":25776541,"url":"https://github.com/mangatmodi/RandomJson","last_synced_at":"2025-02-27T06:06:39.959Z","repository":{"id":37270776,"uuid":"163420278","full_name":"mangatmodi/RandomJson","owner":"mangatmodi","description":"Provides a Kotlin/Java library to create a random json string","archived":false,"fork":false,"pushed_at":"2022-11-16T11:44:34.000Z","size":133,"stargazers_count":71,"open_issues_count":3,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2023-07-28T22:08:06.062Z","etag":null,"topics":["fuzzing","fuzzy-testing","json","testdatabuilder"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mangatmodi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-28T14:43:29.000Z","updated_at":"2023-01-14T14:54:16.000Z","dependencies_parsed_at":"2022-09-04T16:01:34.905Z","dependency_job_id":null,"html_url":"https://github.com/mangatmodi/RandomJson","commit_stats":null,"previous_names":[],"tags_count":2,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mangatmodi%2FRandomJson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mangatmodi%2FRandomJson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mangatmodi%2FRandomJson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mangatmodi%2FRandomJson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mangatmodi","download_url":"https://codeload.github.com/mangatmodi/RandomJson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240987435,"owners_count":19889334,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fuzzing","fuzzy-testing","json","testdatabuilder"],"created_at":"2025-02-27T06:01:26.233Z","updated_at":"2025-02-27T06:06:39.953Z","avatar_url":"https://github.com/mangatmodi.png","language":"Kotlin","funding_links":[],"categories":["测试"],"sub_categories":[],"readme":"# RandomJson [![CodeFactor](https://www.codefactor.io/repository/github/mangatmodi/randomjson/badge)](https://www.codefactor.io/repository/github/mangatmodi/randomjson) [![Build Status](https://travis-ci.com/mangatmodi/RandomJson.svg?branch=master)](https://travis-ci.com/mangatmodi/RandomJson)\n\nProvides library to create a random json. Provides two implementation of json creation\n\n**[SampleJsonCreator](#SampleJsonCreator)**: Creates JSON string from a sample string.\n\n**[SimpleJsonCreator](#SimpleJsonCreator)**: Creates JSON string by taking number of required keys.\n\nSome important considerations:\n1. The random value generations could be customised by giving your own [implementation](#Customize-random-value-generators).\n2. The default given implementation is _thread-safe_. That means random strings can be [created](#Parallel-creation-of-random-strings) in different threads\n3. 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 \nwants to use it.    \n\n#### Java Interoperational\nThe library can be used in Java 10+. See [example](#Java-example).   \n### Usage\n#### Configuration\nFirst we need to create configuration for the creator. This config specify, the random value generators for each of the \nprimitive json type. The library includes some basic generator for each type.\n```kotlin\nval config =  RandomJsonConfig(\n    RandomDouble.threadLocalRandom(),\n    RandomInt.threadLocalRandom(),\n    RandomString.charArray(\"eusbwopw\".toCharArray(), 5),\n    RandomBoolean.uniform(),\n    RandomString.charArray(\"abcdefg\".toCharArray(), 5)\n    )\n\n```\n\n#### SampleJsonCreator\nCreates JSON string similar to `{\"key1\":{\"key2\":3}}` in structure but keys and values have random values.\n```kotlin\nval jsonCreator = RandomJsonCreator\n    .fromSampleString(\"\"\"{\"key1\":{\"key2\":3}}\"\"\", config, KeepKeys.No)\n    println(jsonCreator.create())            \n    \n```\nAbove prints\n```json\n{\"ggdb\":{\"faea\":1279812142}}\n```\n\n**Keep the same keys as the sample string**\nThe sample string creator can create json with same keys as the original json.\n```kotlin\n    val input = \"\"\"{\"key1\":{\"key2\":3}}\"\"\".trimIndent()\n\n    val jsonCreator = RandomJsonCreator\n        .fromSampleString(input, config, KeepKeys.YES)\n    println(jsonCreator.create())\n\n```    \nAbove prints\n```json\n{\"key1\":{\"key2\":2083614805}}\n```\n\nSee more [examples](https://github.com/mangatmodi/RandomJson/blob/master/examples/).\n#### SimpleJsonCreator \nCreates JSON string similar 2ith 10 keys, the structure of the json is decided by `RandomTypeSelector`,\nwhich specify which which type of json field will be added next. SimpleJsonCreator does not support \narrays or nested json. \n\n```kotlin\nval jsonCreator = RandomJsonCreator\n    .fromNumberOfKeys(10,config, RandomTypeSelector.uniform())\n    println(jsonCreator.create())            \n    \n```\n#### Customize random value generators\n\nIn the example below `DummyDoubleValue` implements `RandomDouble` to give `2.0`\nas the double value. So all the JSON strings created by `jsonCreator` below will contain `2.0` as double value\n```kotlin\nclass DummyDoubleValue:RandomDouble{\n    override fun next() =  2.0\n}\nval config =  RandomJsonConfig(\n    DummyDoubleValue(),\n    RandomInt.threadLocalRandom(),\n    RandomString.charArray(\"eusbwopw\".toCharArray(), 5),\n    RandomBoolean.uniform(),\n    RandomString.charArray(\"abcdefg\".toCharArray(), 5)\n    )\n\n\nval jsonCreator = RandomJsonCreator\n    .fromNumberOfKeys(10,config, RandomTypeSelector.uniform())\n    println(jsonCreator.create())            \n\n```\n\n#### Parallel creation of random strings\nIn the example below, we used kotlin's [coroutines](https://kotlinlang.org/docs/reference/coroutines/coroutines-guide.html)\n based `async-await` util to create 10 json strings in parallel.\n```kotlin\nval jsonCreator = RandomJsonCreator\n    .fromNumberOfKeys(10,config, RandomTypeSelector.uniform())\n            \nval tasks =  (1..10).map {\n    async {\n          println(jsonCreator.create())\n        }\n    }\n    \ntasks.forEach{ it.await()}\n\n```\n\n#### Java example\n```java\n        RandomJsonConfig config = new RandomJsonConfig(\n                RandomDouble.threadLocalRandom(),\n                RandomInt.threadLocalRandom(),\n                RandomString.charArray(\"eusbwopw\".toCharArray(), 5),\n                RandomBoolean.uniform(),\n                RandomString.charArray(\"abcdefg\".toCharArray(), 5)\n        );\n\n        RandomJsonCreator creator = RandomJsonCreator.fromSampleString(\n                \"{\\\"q\\\":1}\",\n                config,\n                KeepKeys.no()\n        );\n\n       System.out.println(creator.create())\n```\n### Install\nThe library could be installed from maven central\n\n**Maven**\n```maven\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.mangatmodi\u003c/groupId\u003e\n    \u003cartifactId\u003erandomjson\u003c/artifactId\u003e\n    \u003cversion\u003e2.1.2\u003c/version\u003e\n\u003c/dependency\u003e\n\n```\n\n**Gradle**\n```gradle\ncompile group: 'com.github.mangatmodi', name: 'randomjson', version: '2.1.2'\n```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmangatmodi%2FRandomJson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmangatmodi%2FRandomJson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmangatmodi%2FRandomJson/lists"}