Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

danthe1st/compile-time-json-parser

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.