Skip to content

psumiya/json-generator

Repository files navigation

Json Generator

About Json Generator

This is an experimental project to generate a pseudo-random Json string based on a given input Json string.

String generate(String input)

The method generates a pseudo random Json based on the input provided. To see some examples, here are some generated Json strings for corresponding inputs.

The library takes an opinionated view of what things to randomize and how to go about it.

Examples

Input Output
{
 "firstName": "alpha",
 "lastName": "beta"
}
{
  "firstName" : "Sylvester",
  "lastName" : "Lakin"
}
{
  "id": "0",
  "username": "string",
  "strings": [
    "traveling",
    "technology"
  ],
  "numbers": [1, 3],
  "nestedObject": {
    "param1": "Mahanaxar",
    "param2": "Valinor"
  },
  "firstName": "blah",
  "lastName": "blahh",
  "gender": "Unknown",
  "address": "123 St"
}
{
  "id" : "2587beeb-0d79-400c-a11b-e7df2bc45c0a",
  "username" : "tjkvR",
  "strings" : [ "traveling", "technology", "", "P", "By", "nGHUKAdG" ],
  "numbers" : [ 1, 3, 1873970129, 2146031070, 1655147238 ],
  "nestedObject" : {
    "param1" : "vuSNEWy",
    "param2" : ""
  },
  "firstName" : "Fabian",
  "lastName" : "Bechtelar",
  "gender" : "NonBinary",
  "address" : "47862 Morissette Drive, New Juttaville, MS 09109"
}
[
	{
		"genre": "reference",
		"author": "Nigel Rees",
		"title": "Sayings of the Century",
		"price": 8.95
	}
]
[
	{
		"genre": "Fantasy",
		"author": "Miss Wesley Hammes",
		"title": "Ah, Wilderness!",
		"price": 177996346
	},
	{
		"genre": "Metafiction",
		"author": "Gigi West",
		"title": "The Golden Apples of the Sun",
		"price": 2139671705
	},
	{
		"genre": "Tall tale",
		"author": "Enid Johnson",
		"title": "Waiting for the Barbarians",
		"price": 1351133435
	},
	{
		"genre": "Tall tale",
		"author": "Werner Ruecker",
		"title": "Butter In a Lordly Dish",
		"price": 1014620715
	},
	{
		"genre": "Fiction narrative",
		"author": "Mrs. Rueben Cole",
		"title": "Fame Is the Spur",
		"price": 434184399
	},
	{
		"genre": "Biography/Autobiography",
		"author": "Ms. Don Pouros",
		"title": "Ego Dominus Tuus",
		"price": 19385680
	}
]

Usage

Build

Add the following dependency.

  • Gradle

implementation 'json-generator:json-generator:0.0.3'
  • Maven

<dependency>
  <groupId>json-generator</groupId>
  <artifactId>json-generator</artifactId>
  <version>0.0.3</version>
</dependency>

Code

import json.generator.api.Generator;
import json.generator.api.GeneratorFactory;

Generator<String, String> generator = GeneratorFactory.getDefaultGenerator();
String generated = generator.generate(inputJson);

See the JsonToJsonGeneratorTest class for other canonical ways to invoke the Json generator.

Run FullSpecTest to see randomized output for an input with all supported fields and spec.

This project depends on the net.datafaker:datafaker library. That library generates fake data, similar to the javascript faker.js library or the python faker package.

The default language supported for this library is en, and the default region is US.

This library can be used in situations where you may want to randomize the data being used across test runs. For example, given a sample, this library can keep generating different data across each run, which can help with better tests for your system.

Note
While the library tries to generate pseudo random data, it is possible it can generate some data that is not fake. Exercise caution around its usage.

Example

Here is a github repository showing a project that uses this library: https://github.com/psumiya/json-generator-demo

Customization

You may choose to provide a generator specification that informs the library regarding certain aspects of how to generate the Json.

The generator specification is provided as its own node on the input to the library. This looks like an extra node in addition to the original sample to randomize, as the full input to the library.

For an example, see the ___GENERATOR_SPEC node in the input below. The spec helps with overriding the default locale values, as defined in ___LOCALIZATION, and a way to plug in some excel-style formulas for generating specific nodes, as defined in ___FIELDS.

Input Output
{
  "___GENERATOR_SPEC": {
    "___LOCALIZATION": {
      "language": "en",
      "country": "US"
    },
    "___FIELDS": [
      {
        "fieldName": "someStaticField",
        "generatorName": "IDENTITY"
      },
      {
        "fieldName": "generatedId",
        "generatorName": "RANDOM_UUID"
      },
      {
        "fieldName": "someOneOf",
        "generatorName": "ONE_OF",
        "parameters": {
          "values": [
            "B1",
            "B2",
            "AX"
          ]
        }
      }
    ]
  },
  "someStaticField": "static",
  "uuid": "72ebd5b3-6109-443c-8cb3-6e3a96e7fb86",
  "id": "b66aef25-de3b-4761-8db2-b66793043aac",
  "generatedId": "2587beeb-0d79-400c-a11b-e7df2bc45c0a",
  "someOneOf": "B2",

  "fullName": "ab",
  "firstName": "a",
  "lastName": "b",
  "gender": "Unknown",

  "address": "123 Bond St, Paris, FR",
  "streetAddress": "123 Bond St",
  "state": "AS",
  "timeZone": "GMT",
  "city": "Paris",

  "creditCardType": "Visa",
  "creditCardNumber": "1234567890123456",
  "creditCardExpiry": "12/31/2022",

  "title": "t",
  "author": "",
  "genre": "Finance",
  "publisher": "RandomHouse",

  "blend": "blend",

  "color": "color",
  "hexColor": "#12",

  "bloodGroup": "A",

  "currency": "USD",
  "currencyName": "US Dollars"
}
{
  "someStaticField" : "static",
  "uuid" : "f08569ed-8cab-442f-9f72-d656bac14a25",
  "id" : "896e192f-99ec-4e39-816d-1ad3808b570e",
  "generatedId" : "b658f7cb-6e88-4046-b14b-aaed27b29d15",
  "someOneOf" : "B2",
  "fullName" : "Miss Carrol Jenkins",
  "firstName" : "Margret",
  "lastName" : "Dickens",
  "gender" : "Genderfluid",
  "address" : "8581 Leannon Points, North Oliver, AR 82615",
  "streetAddress" : "4108 Bo Radial",
  "state" : "Massachusetts",
  "timeZone" : "Europe/Madrid",
  "city" : "Katharineborough",
  "creditCardType" : "discover",
  "creditCardNumber" : "1211-1221-1234-2201",
  "creditCardExpiry" : "2013-9-12",
  "title" : "Look Homeward, Angel",
  "author" : "Donte Kirlin",
  "genre" : "Classic",
  "publisher" : "Hackett Publishing Company",
  "blend" : "Jacked Been",
  "color" : "magenta",
  "hexColor" : "#D00E8D",
  "bloodGroup" : "AB-",
  "currency" : "XTS",
  "currencyName" : "Dalasi"
}
{
  "___GENERATOR_SPEC": {
    "___LOCALIZATION": {
      "language": "es",
      "country": "MX"
    },
    "___FIELDS": [
      {
        "fieldName": "someStaticField",
        "generatorName": "IDENTITY"
      },
      {
        "fieldName": "id",
        "generatorName": "RANDOM_UUID"
      },
      {
        "fieldName": "someOneOf",
        "generatorName": "ONE_OF",
        "parameters": {
          "values": [
            "B1",
            "B2",
            "AX"
          ]
        }
      }
    ]
  },
  "id": "b66aef25-de3b-4761-8db2-b66793043aac",
  "username": "string",
  "someStaticField": "static",
  "strings": [
    "traveling",
    "technology"
  ],
  "numbers": [1, 3],
  "nestedObject": {
    "param1": "Mahanaxar",
    "param2": "Valinor"
  },
  "firstName": "blah",
  "lastName": "blahh",
  "gender": "Unknown",
  "someOneOf": "B2"
}
{
  "id" : "bd08f64c-fbef-4776-8514-fc66fb19830f",
  "username" : "PpUulr",
  "someStaticField" : "static",
  "strings" : [ "traveling", "technology", "Z", "mAUj" ],
  "numbers" : [ 1, 3, 2089642448, 1658931818, 1158107405 ],
  "nestedObject" : {
    "param1" : "lqc",
    "param2" : "R"
  },
  "firstName" : "Ismael",
  "lastName" : "Durán",
  "gender" : "Genderfluid",
  "someOneOf" : "AX"
}

Read about generators here: Generators.adoc

Performance

To evaluate performance, a performance benchmark is provided in the src/jmh directory.

To execute the benchmark, run ./gradlew jmh at the root of this project.

Here is the result of a sample jmh benchmark execution:

Iteration   1: 24.095 ms/opNG [7m 45s]
Iteration   2: 23.740 ms/opNG [7m 55s]
Iteration   3: 22.989 ms/opNG [8m 5s]
Iteration   4: 23.197 ms/opNG [8m 15s]
Iteration   5: 22.819 ms/opNG [8m 25s]


Result "json.generator.Benchmark.executeMethodToBenchmark":
  28.216 ±(99.9%) 6.692 ms/op [Average]
  (min, avg, max) = (22.819, 28.216, 45.607), stdev = 6.260
  CI (99.9%): [21.524, 34.907] (assumes normal distribution)
.
.
.
Benchmark                           Mode  Cnt   Score   Error  Units
Benchmark.executeMethodToBenchmark  avgt   15  28.216 ± 6.692  ms/op