1
1
# JSON Schema Compiler
2
2
3
- > ⚠️ WARNING! WORK IN PROGRESS: This project is in an early stage and should not be considered production-ready. Use it at your
3
+ > ⚠️ WARNING! WORK IN PROGRESS: This project is in an early stage and should not be considered production-ready. Use it
4
+ > at your
4
5
> own risk.
5
6
6
- When you need to integrate JSON-Schema models into JVM-based applications, you can use several tools to translate it
7
- Java, or other supported language, and then compile it along with your application .
7
+ ` json-schema-compiler ` , ** compiles JSON-Schema code directly to bytecode ** , in both in ` .class ` files and ` .jar `
8
+ libraries, eliminating the need to translate the schema to other programming language and then compiling it.
8
9
9
- This approach works for many use cases, but requires a complete development kit and takes an indirect path as you need
10
- translate one language to another, and then use a compiler to create your executable .
10
+ This makes the process faster, removes the need of a development kit available, and reduces the complexity to
11
+ integrate such schemas into JVM-based applications .
11
12
12
- ` json-schema-compiler ` , as it's name implies, ** compiles JSON-Schema code directly to bytecode** . This removes the need
13
- to have a development kit available, and reduces the complexity to integrate such schemas into JVM-based applications.
14
-
15
- ## How to use
13
+ ## How to compile JSON Schema files to bytecode
16
14
17
15
` json-schema-compiler ` is a command line utility. The general syntax is:
18
16
@@ -22,12 +20,12 @@ json-schema-compiler -p [package] -o [output]
22
20
23
21
Supported parameters:
24
22
25
- | parameter | default value | description |
26
- | -------------------------| ---------------| ------------------------------------------------|
27
- | ` -p ` , ` --package-name ` | empty | The package where generated classes should be. |
28
- | ` -o ` , ` --output ` | "." | The output folder for the generated classes. |
23
+ | parameter | default value | description |
24
+ | -------------------------| ---------------| --------------------------------------------------------------------------------------- |
25
+ | ` -p ` , ` --package-name ` | empty | The package where generated classes should be. |
26
+ | ` -o ` , ` --output ` | "." | The output folder for the generated classes, or the path of the jar file to generate. |
29
27
30
- ### 1. Download your specific image
28
+ ### 1. Download your specific image, or generate one
31
29
32
30
Everytime the code is updated a new native image is generated for Windows, Linux and MacOS
33
31
using [ GraalVM's Native Image] ( https://www.graalvm.org/latest/reference-manual/native-image/ ) . You can find the image
@@ -40,17 +38,37 @@ for your operating system under the `[OS]-current` tag:
40
38
Once downloaded, you can run it directly from your current folder, or add it to your systems ` $PATH ` so that it can be
41
39
launched from anywhere.
42
40
41
+ If your system is not listed here, you can generate your own image by:
42
+
43
+ 1 . Installing the [ GraalVM JDK] ( https://www.graalvm.org/downloads/ )
44
+ 2 . Cloning this repository a
45
+ 3 . Running the [ create_image.sh] ( ./create-image.sh ) script
46
+
47
+ The script will build and image for your current operating system and architecture, and put it in the ` bin/local `
48
+ folder.
49
+
43
50
### 2. Compile json schema files
44
51
45
52
If you are using ` json-schema-compiler ` in a UNIX-like environment, you can pass the JSON code trough standard input:
46
53
47
54
``` bash
55
+ # compile as .class files in the ./output folder:
48
56
cat /my/json-schema/file.json | json-schema-compiler -p com.example -o ./output
49
57
```
50
58
59
+ ``` bash
60
+ # compile as ./library.jar:
61
+ cat /my/json-schema/file.json | json-schema-compiler -p com.example -o ./library.jar
62
+ ```
63
+
51
64
You can also pass the json schema's path as an argument:
52
65
53
66
``` bash
54
- json-schema-compiler -p com.example -o ./output /my/json-schema/file.json
67
+ json-schema-compiler -p com.example -o ./library.jar /my/json-schema/file.json
55
68
```
56
69
70
+ ## How it works
71
+
72
+ This compiler makes heavy use of the [ Java Class-File API] ( https://openjdk.org/jeps/484 ) to generate bytecode directly
73
+ at runtime. You have more details about this project
74
+ in [ this article I wrote] ( https://www.nachobrito.es/sideprojects/json-schema-compiler/ ) .
0 commit comments