Skip to content

NachoBrito/json-schema-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON Schema Compiler

⚠️ WARNING! WORK IN PROGRESS: This project is in an early stage and should not be considered production-ready. Use it at your own risk.

json-schema-compiler, compiles JSON-Schema code directly to bytecode, in both in .class files and .jar libraries, eliminating the need to translate the schema to other programming language and then compiling it.

This makes the process faster, removes the need of a development kit available, and reduces the complexity to integrate such schemas into JVM-based applications.

How to compile JSON Schema files to bytecode

json-schema-compiler is a command line utility. The general syntax is:

json-schema-compiler -p [package] -o [output]

Supported parameters:

parameter default value description
-p , --package-name empty The package where generated classes should be.
-o , --output "." The output folder for the generated classes, or the path of the jar file to generate.

1. Download your specific image, or generate one

Everytime the code is updated a new native image is generated for Windows, Linux and MacOS using GraalVM's Native Image. You can find the image for your operating system under the [OS]-current tag:

Once downloaded, you can run it directly from your current folder, or add it to your systems $PATH so that it can be launched from anywhere.

If your system is not listed here, you can generate your own image by:

  1. Installing the GraalVM JDK
  2. Cloning this repository a
  3. Running the create_image.sh script

The script will build and image for your current operating system and architecture as target/json-schema-compiler.

2. Compile json schema files

If you are using json-schema-compiler in a UNIX-like environment, you can pass the JSON code trough standard input:

# compile as .class files in the ./output folder:
cat /my/json-schema/file.json | json-schema-compiler -p com.example -o ./output
# compile as ./library.jar:
cat /my/json-schema/file.json | json-schema-compiler -p com.example -o ./library.jar

You can also pass the json schema's path as an argument:

json-schema-compiler -p com.example -o ./library.jar /my/json-schema/file.json

How it works

This compiler makes heavy use of the Java Class-File API to generate bytecode directly at runtime. You have more details about this project in this article I wrote.

About

JSON Schema to bytecode compiler

Resources

License

Stars

Watchers

Forks

Packages

No packages published