generates a JSON-parser for Java-objects at compile-time
Compile-time JSON-parser supports both non-private variables and properties.
The generated JSON-parser uses org.json:json
.
- Download and install Maven
- Download the sources
- Run
mvn clean install
in the directory of Compile-time JSON-parser - Create a Maven Project in IntelliJ where you want to use Compile-time JSON-parser
- Add the following dependency to the
pom.xml
of the project where you want to use Compile-time JSON-parser (replaceVERSION
with the version from )
<dependency>
<groupId>io.github.danthe1st</groupId>
<artifactId>compile-time-json-parser</artifactId>
<version>VERSION</version>
</dependency>
- If you wish to use JPMS, also add the annotation processor to the
maven-compiler-plugin
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>io.github.danthe1st</groupId>
<artifactId>compile-time-json-parser</artifactId>
<version>VERSION</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</plugin>
- Enable annotation processing for this project under
Settings
>Build, Execution, Deployment
>Compiler
>Annotation Processors
>Enable Annotation Processing
- Create a data class and annotate it with
@GenerateJSON
like this:
import io.github.danthe1st.json_compile.api.GenerateJSON;
@GenerateJSON
public class TestClass {
public int someInt;
private String someString;
private int[][] someArray;
public String getSomeString() {
return someString;
}
public void setSomeString(String someString) {
this.someString = someString;
}
public int[][] getSomeArray() {
return someArray;
}
public void setSomeArray(int[][] someArray) {
this.someArray = someArray;
}
@Override
public String toString() {
return "TestClass{" +
"someInt=" + someInt +
", someString='" + someString + '\'' +
", someArray=" + Arrays.deepToString(someArray) +
'}';
}
}
- When compiling the class, a class suffixed with
JSONLoader
should be automatically generated.
This class contains a method namedfromJSON
that creates an instance of the data class from aString
:
String json= String.join("", Files.readAllLines(Path.of("testClass.json")));
TestClass obj = TestClassJSONLoader.fromJSON(json);
System.out.println(obj);
TestClass testObj=new TestClass();
testObj.setSomeString("test");
testObj.someInt=12345;
testObj.someArray=new int[][]{{1,2,3},{},null,{1,2,3,4,5,6}};
System.out.println(TestClassJSONLoader.toJSON(testObj));
An example project can be found in the directory examples/maven-example
.
- Import
Compile-time JSON-parser
in IntelliJ as a maven project - Run
mvn clean install
in that project - Expand the
examples/maven-example
directory - Right-click on the file
pom.xml
in that directory and selectAdd as a Maven Project
- Make sure you set up your IDE correctly
- Run
TestClass
inexamples/maven-example/src/main/java/io/github/danthe1st/json_compile/test/TestClass
String
int
long
float
double
boolean
- Enums
- Wrapper classes for supported primitive types
- Objects of classes annotated with
@GenerateJSON
Collection
s if they are not part of other collections or arrays,Collections
of classes annotated with@GenerateJSON
that contain collections are supported, however- Arrays
org.json.JSONObject
andorg.json.JSONArray
- It is not possible to create an array/collection of collections
- Objects annotated with
@GenerateJSON
need to have a no-args-constructor - Collections need to be initialized in the constructor
- Generic objects are not supported (except generic collections)
- Configuration is not supported
- Install the m2e-apt plugin
- Right-click the project, open
Properties
>Java Compiler
>Annotation Processing
> and selectEnable Project Specific Settings
andEnable processing in Editor
- Enable annotation processing for this project under
Settings
>Build, Execution, Deployment
>Compiler
>Annotation Processors
>Maven default annotation processors profile
>json-parser-maven-example
>Enable Annotation Processing