Compile time JSON parser

prepares converting JSON objects to java objects at compile-time

License

License

Categories

Categories

Ant Build Tools JSON Data
GroupId

GroupId

io.github.danthe1st
ArtifactId

ArtifactId

compile-time-json-parser
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Compile time JSON parser
prepares converting JSON objects to java objects at compile-time
Project URL

Project URL

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

Source Code Management

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

Download compile-time-json-parser

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.danthe1st/compile-time-json-parser/ -->
<dependency>
    <groupId>io.github.danthe1st</groupId>
    <artifactId>compile-time-json-parser</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.danthe1st/compile-time-json-parser/
implementation 'io.github.danthe1st:compile-time-json-parser:1.1.0'
// https://jarcasting.com/artifacts/io.github.danthe1st/compile-time-json-parser/
implementation ("io.github.danthe1st:compile-time-json-parser:1.1.0")
'io.github.danthe1st:compile-time-json-parser:jar:1.1.0'
<dependency org="io.github.danthe1st" name="compile-time-json-parser" rev="1.1.0">
  <artifact name="compile-time-json-parser" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.danthe1st', module='compile-time-json-parser', version='1.1.0')
)
libraryDependencies += "io.github.danthe1st" % "compile-time-json-parser" % "1.1.0"
[io.github.danthe1st/compile-time-json-parser "1.1.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.json : json jar 20210307

Project Modules

There are no modules declared in this project.

Compile-time JSON-parser Maven Central

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 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 (replace VERSION with the version from Maven Central)
<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

Usage

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

m2e-apt

  • 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

Versions

Version
1.1.0
1.0.0