Skip to content

Configurable JSON schema generation. Rewrite of JSON schema component of rest-schemagen.

License

Notifications You must be signed in to change notification settings

Mercateo/json-schema

Repository files navigation

json-schema

Codacy Badge Build Status Coverage Status MavenCentral

Generic JSON-Schema generator.

Features

  • Handles Generics
  • Configurable
  • Supports dynamic schema with allowed values and default values

Example

    SchemaGenerator schemaGenerator = new SchemaGenerator(
            Collections.emptyList(),
            Collections.<RawPropertyCollector>singletonList(new FieldCollector()),
            new HashMap<>());
    
    ObjectNode schema = schemaGenerator.generateSchema(Foo.class);
    
    String schemaString = schema.toString();

Generates the following result:

{
  "type": "object", 
  "properties": {
    "baz": {
      "type": "object", 
      "properties": {
        "qux": {
          "type": "string"
        }
      }
    }, 
    "bar": {
      "type": "string"
    }
  }
}}

Property structure

class Foo {
    String bar;
    Baz baz;
}

class Baz {
    String qux;
}

bar, baz and qux are properties.

Property representation

Properties are represented as Property Objects specified by containing and contained type:

Property<Foo, String> bar;
Property<Foo, Baz> baz;
Property<Baz, String> qux;

The part of the property which is represented by its type is contained in a PropertyDescriptor which has a generic type representing its contained type.

Property unwrapping

class Foo {
    String bar;
    
    @JsonUnwrapped
    Baz baz;
}

class Baz {
    String qux;
}
Property<Foo, String> bar;
Property<Foo, String> qux;

Benchmarks

current results

Benchmark                         Mode  Cnt        Score       Error  Units
Benchmarks.createRepeatedSchema  thrpt   10  1818545.697 ± 38533.578  ops/s
Benchmarks.createSchema          thrpt   10   260946.251 ± 15201.070  ops/s

createSchema creates SchemaGenerator and schema for each iteration, createdRepeatedSchema uses caching in the SchemaGenerator for schema creation

About

Configurable JSON schema generation. Rewrite of JSON schema component of rest-schemagen.

Resources

License

Stars

Watchers

Forks

Packages

No packages published