Coder Social home page Coder Social logo

jsongenerator's Introduction

JSON Generator

A JSON data generator from JSON Schemas, provided as a Java library.

It is available on JitPack.

An online demonstration is here.

How to Use

import java.util.Random;
import net.jimblackler.jsonschemafriend.Schema;
import net.jimblackler.jsonschemafriend.SchemaStore;

public class JsonGenerationExample {

  /**
   * Generate random Json with the following schema:
   * {
   *   "type": "object",
   *   "properties": {
   *     "name": { "type": "string" },
   *     "birthday": { "type": "string","format": "date" },
   *     "age": { "type": "integer" }
   *   }
   * }
   */
  public static void main(String[] args) throws Exception {
    Configuration config = DefaultConfig.build()
        .setGenerateMinimal(false)
        .setNonRequiredPropertyChance(0.5f)
        .get();
    SchemaStore schemaStore = new SchemaStore(true);
    Schema schema = schemaStore.loadSchemaJson("{ \"type\": \"object\", \"properties\": { \"name\": { \"type\": \"string\" }, \"birthday\": { \"type\": \"string\", \"format\": \"date\" }, \"age\": { \"type\": \"integer\" } } }");
    Generator generator = new Generator(config, schemaStore, new Random());
    Object json = generator.generate(schema, 10);

    // sample output: {name=vxtydd, birthday=2377-03-08, age=544}
    System.out.println(json);
  }

}

License

Written by [email protected] and offered under an Apache 2.0 license.

jsongenerator's People

Contributors

jimblackler avatar jimblacklercorp avatar pablo1ru avatar pruda1 avatar tuliren 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

Watchers

 avatar  avatar

jsongenerator's Issues

Support Object and Array ?

Hi,

Does it support Object or Array ?

for example this is my Schema :

{
"$schema": "https://json-schema.org/draft/2019-09/schema#",
"$id": "http://my-paintings-api.com/schemas/painting-schema.json",
"type": "object",
"title": "Painting",
"description": "Painting information",
"additionalProperties": true,
"required": ["name", "artist", "dimension", "description", "tags"],
"properties": {
"name": {
"type": "string",
"description": "Painting name"
},
"artist": {
"type": "string",
"maxLength": 50,
"description": "Name of the artist"
},
"description": {
"type": ["string", "null"],
"description": "Painting description"
},
"dimension": { "$ref": "#/$defs/dimension" },
"tags": {
"type": "array",
"items": { "$ref": "#/$defs/tag" }
}
},
"$defs": {
"tag": {
"type": "string",
"enum": ["oil", "watercolor", "digital", "famous"]
},
"dimension": {
"type": "object",
"title": "Painting dimension",
"description": "Describes the dimension of a painting in cm",
"additionalProperties": true,
"required": ["width", "height"],
"properties": {
"width": { "type": "number", "description": "Width of the product", "minimum": 1 },
"height": { "type": "number", "description": "Height of the product", "minimum": 1 }
}
}
}
}

all the 'simple' keys are working fine, but the 'complicated' ones ... seems not...

for this Schema the fake data is :

image

tried to add it in the pom but failed to get the library from maven

Hi ,

i tried to add it to my pom as mentioned , but it fail:

repositories


jitpack.io
https://jitpack.io

<dependency>
    <groupId>net.jimblackler</groupId>
    <artifactId>jsongenerator</artifactId>
    <version>0.4.5</version>
</dependency>

net.jimblackler:jsongenerator:pom:0.45 was not found in https://repo1.maven.org/maven2 during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of central has elapsed or updates are forced

any idea why ?

Incompatibility with net.jimblackler.jsonschemafriend.core 0.11.4 because CacheLoader implementation has changed

net.jimblackler.jsonschemafriend.core is required to get the SchemaStore class definition.
In jsonschemafriend 0.11.4 CacheLoader implementation changed and is no longer compatible with net.jimblackler.jsongenerator 0.4.6.
jsonschemafriend 0.11.2 works well with jsongenerator 0.4.6.

Steps to reproduce :

  1. Use those versions in pom.xml :
<dependencies>
    <dependency>
	    <groupId>net.jimblackler.jsonschemafriend</groupId>
	    <artifactId>core</artifactId>
	    <version>0.11.4</version>
    </dependency>
    <dependency>
	    <groupId>net.jimblackler</groupId>
	    <artifactId>jsongenerator</artifactId>
	    <version>0.4.6</version>
    </dependency>
</dependencies>
  1. Compile sample code in README.md
  2. Run sample code
  3. Exception in thread "main" java.lang.IncompatibleClassChangeError: Expected static method 'java.lang.String net.jimblackler.jsonschemafriend.CacheLoader.load(java.net.URI, boolean)'

Required on Constitutive fields

Question :)
if i want to set required on this schema :
{
"$schema": "https://json-schema.org/draft/2019-09/schema#",
"$id": "http://my-paintings-api.com/schemas/painting-schema.json",
"required": ["orgId"],
"properties": {
"orgId": {
"type": "integer",
"minimum": 1,
"description": "orgID"
},
"projectId": {
"type": "integer",
"description": "projectId"
}
}
but what can i do if i want required on constitutive ? for example :

{
"$schema": "https://json-schema.org/draft/2019-09/schema#",
"$id": "http://my-paintings-api.com/schemas/painting-schema.json",
"type": "object",
"title": "Painting",
"description": "Painting information",
"additionalProperties": true,
"required": ["orgId"],
"properties": {
"orgId": 12,
"sourceIssuedAt": "2022-02-08T15:19:17z",
"schemaVersion": "1.1",
"orgId": "test",
"payload": {
"orgId": {
"type": "integer",
"minimum": 1,
"description": "orgID"
}
}

and i would like to make required on payload.orgId can it be done ? ( and not external orgId )

Thanks
Ohad

Schema Question

first questions

Riding on your Schema example:
/**

  • Generate random Json with the following schema:
  • {
  • "type": "object",
  • "properties": {
  • "name": { "type": "string" },
    
  • "birthday": { "type": "string","format": "date" },
    
  • "age": { "type": "integer" }
    
  • }
  • }
    */

he fail a lot on : date
Attempt 22:
"0398-45-24" at #/birthday failed against #/properties/birthday with "Not compliant with format: date. Reason: Text '0398-45-24' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 45"

second question

what fields can i put in the Schema ? what i must remove ? ....i.e : this he can not do generator...
Schema example :
{
"$schema": "https://json-schema.org/draft/2019-09/schema#",
"$id": "http://my-paintings-api.com/schemas/painting-schema.json",
"type": "object",
"title": "eor",
"version": 1,
"description": "eor",
"additionalProperties": true,
"required": [
"orgId"
],
"anyOf": [
"projectId"
],
"oneOf": [
"workerID"
],
"properties": {
"orgId": {
"type": "integer",
"minimum": 1,
"description": "orgID"
},
"projectId": {
"type": "integer",
"description": "projectId"
},
"workerID": {
"type": "string",
"description": "workerID"
},
"accountingCodeName": {
"type": "string",
"description": "accountingCodeName"
},
"changedDate": {
"type": "string",
"description": "changedDate"
},
"clientName": {
"type": "string",
"description": "clientName"
},
"projectName": {
"type": "string",
"description": "projectName"
},
"workerName": {
"type": "string",
"description": "workerName"
},
"projectService": {
"type": "string",
"description": "projectService"
},
"source": {
"type": "string",
"description": "source"
},
"userName": {
"type": "string",
"description": "userName"
},
"previousValue": {
"type": "integer",
"description": "previousValue"
},
"newValue": {
"type": "integer",
"description": "newValue"
},
"effectiveDate": {
"type": "string",
"description": "effectiveDate"
}
}
}

and error is ๐Ÿ‘
Exception in thread "main" net.jimblackler.jsonschemafriend.StandardGenerationException: {valid=false, keywordLocation=https://json-schema.org/draft/2019-09/schema, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/schema, instanceLocation=, errors=[{valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/meta/core, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/meta/core, instanceLocation=#/anyOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/meta/applicator, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/meta/applicator, instanceLocation=#/anyOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/meta/validation, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/meta/validation, instanceLocation=#/anyOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/meta/meta-data, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/meta/meta-data, instanceLocation=#/anyOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/meta/format, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/meta/format, instanceLocation=#/anyOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/meta/content, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/meta/content, instanceLocation=#/anyOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/schema, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/schema, instanceLocation=#/anyOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/meta/core, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/meta/core, instanceLocation=#/oneOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/meta/applicator, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/meta/applicator, instanceLocation=#/oneOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/meta/validation, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/meta/validation, instanceLocation=#/oneOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/meta/meta-data, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/meta/meta-data, instanceLocation=#/oneOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/meta/format, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/meta/format, instanceLocation=#/oneOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/meta/content, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/meta/content, instanceLocation=#/oneOf/0}, {valid=false, error=Expected: [boolean, object] Found: [string], keywordLocation=https://json-schema.org/draft/2019-09/schema, absoluteKeywordLocation=https://json-schema.org/draft/2019-09/schema, instanceLocation=#/oneOf/0}]}
at net.jimblackler.jsonschemafriend.SchemaStore.loadSchema(SchemaStore.java:206)
at net.jimblackler.jsonschemafriend.SchemaStore.loadSchema(SchemaStore.java:117)
at net.jimblackler.jsonschemafriend.SchemaStore.loadSchema(SchemaStore.java:97)
at net.jimblackler.jsonschemafriend.SchemaStore.loadSchema(SchemaStore.java:73)
at net.jimblackler.jsonschemafriend.SchemaStore.loadSchemaJson(SchemaStore.java:110)
at com.example.demo.DemoApplication.fake(DemoApplication.java:68)
at com.example.demo.DemoApplication.main(DemoApplication.java:39)

Can not get DefaultConfig from maven

Hi ,

when i am trying to get maven project ver 0.4.5 i do not have DefaultConfig file ( but i do see it on your GitHub solution )- can u please help me understand what i am doing wrong ?
my pom is :


jitpack.io
https://jitpack.io

<dependencies>

    <dependency>
        <groupId>com.github.jimblackler</groupId>
        <artifactId>jsongenerator</artifactId>
        <version>0.4.5</version>
    </dependency>
    <dependency>
</dependencies>

code:
Configuration config = DefaultConfig.build()
.setGenerateMinimal(false)
.setNonRequiredPropertyChance(0.5f)
.get();
SchemaStore schemaStore = new SchemaStore(true);
Schema schema = schemaStore.loadSchemaJson("{ "type": "object", "properties": { "name": { "type": "string" }, "birthday": { "type": "string", "format": "date" }, "age": { "type": "integer" } } }");
Generator generator = new Generator(config, schemaStore, new Random());
Object json = generator.generate(schema, 10);

		// sample output: {name=vxtydd, birthday=2377-03-08, age=544}
		System.out.println(json);

fail here - DefaultConfig

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.