Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/danthe1st/compile-time-json-parser

generates a JSON-parser for Java-objects at compile-time
https://github.com/danthe1st/compile-time-json-parser

annotation-processor java json-java json-parser json-parsing-library

Last synced: 13 days ago
JSON representation

generates a JSON-parser for Java-objects at compile-time

Awesome Lists containing this project

README

        

# Compile-time JSON-parser [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.danthe1st/compile-time-json-parser/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.github.danthe1st/compile-time-json-parser)
> 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`.

### Setup
* [Download](https://maven.apache.org/download.cgi) and [install](https://maven.apache.org/install.html) [Maven](https://maven.apache.org/)
* 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 (replace `VERSION` with the version from [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.danthe1st/compile-time-json-parser/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.github.danthe1st/compile-time-json-parser))
```xml

io.github.danthe1st
compile-time-json-parser
VERSION

```
* If you wish to use JPMS, also add the annotation processor to the `maven-compiler-plugin`
```xml

maven-compiler-plugin
3.8.1

11


io.github.danthe1st
compile-time-json-parser
VERSION


```
* Enable annotation processing for this project under `Settings`>`Build, Execution, Deployment`>`Compiler`>`Annotation Processors`>`Enable Annotation Processing`

### Usage
* Create a data class and annotate it with `@GenerateJSON` like this:

```java
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 named `fromJSON` that creates an instance of the data class from a `String`:

```java
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));
```

### Example

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 select `Add as a Maven Project`
* [Make sure you set up your IDE correctly](#ide-specific-configuration)
* Run `TestClass` in `examples/maven-example/src/main/java/io/github/danthe1st/json_compile/test/TestClass`

### Supported types
* `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` and `org.json.JSONArray`

### Limitations
* 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

### IDE-specific configuration

### Eclipse
* Install the [m2e-apt plugin](https://marketplace.eclipse.org/content/m2e-apt)
[![Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client](https://marketplace.eclipse.org/sites/all/themes/solstice/public/images/marketplace/btn-install.svg)](http://marketplace.eclipse.org/marketplace-client-intro?mpc_install=1216155 "Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client")

![m2e-apt](https://user-images.githubusercontent.com/34687786/114560029-9713f280-9c6c-11eb-889d-096ee46b52c0.png)

* Right-click the project, open `Properties`>`Java Compiler`>`Annotation Processing`> and select `Enable Project Specific Settings` and `Enable processing in Editor`

### IntelliJ
* 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`