Skip to content

Commit 60ca941

Browse files
committed
support JSON creation
1 parent faf454e commit 60ca941

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,24 @@ public class TestClass {
4343
String json= String.join("", Files.readAllLines(Path.of("testClass.json")));
4444
TestClass obj = TestClassJSONLoader.fromJSON(json);
4545
System.out.println(obj);
46+
TestClass testObj=new TestClass();
47+
testObj.setSomeString("test");
48+
testObj.someInt=12345;
49+
System.out.println(TestClassJSONLoader.toJSON(testObj));
4650
```
4751

4852
### Example
4953

5054
An example project can be found in the directory `examples/maven-example`.
5155

5256
* Import `Compile-time JSON-parser` in IntelliJ as a maven project
57+
* Run `mvn clean install` in that project
5358
* Expand the `examples/maven-example` directory
5459
* Right-click on the file `pom.xml` in that directory and select `Add as a Maven Project`
55-
* Enable annotation processing for this project under `Settings`>`Build, Execution, Deployment`>`Compiler`>`Annotation Processors`>`Enable Annotation Processing`
60+
* 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`
5661
* Run `TestClass` in `examples/maven-example/src/main/java/io/github/danthe1st/json_compile/test/TestClass`
5762

5863
### Limitations
5964

6065
* Currently, the only supported data types are `int` and `String`.
6166
* Eclipse may not detect the annotation processor
62-
* Converting objects to JSON is currently not supported

examples/maven-example/src/main/java/io/github/danthe1st/json_compile/test/TestClass.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,9 @@ public static void main(String[] args) throws IOException {
3333
String json= String.join("", Files.readAllLines(Path.of("testClass.json")));
3434
TestClass obj = TestClassJSONLoader.fromJSON(json);
3535
System.out.println(obj);
36+
TestClass testObj=new TestClass();
37+
testObj.setSomeString("test");
38+
testObj.someInt=12345;
39+
System.out.println(TestClassJSONLoader.toJSON(testObj));
3640
}
3741
}

src/main/java/io/github/danthe1st/json_compile/impl/JSONCreator.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class JSONCreator extends AbstractProcessor {
3838

3939
private static final String JSONOBJECT_PARAM_NAME = "data";
4040

41-
private static final Map<String, String> simpleAssignments=Map.of("java.lang.String","getString","int","getInt");
41+
private static final Map<String, String> simpleAssignments=Map.of("java.lang.String","String","int","Int");
4242

4343
@Override
4444
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
@@ -114,7 +114,7 @@ private void generateJSON(Element element, String fullyQualidiedClassName, Class
114114
TypeMirror type = jsonOperation.getType();
115115
String typeName=type.toString();
116116
if(simpleAssignments.containsKey(typeName)) {
117-
String val=JSONOBJECT_PARAM_NAME+"."+simpleAssignments.get(typeName)+"(\""+jsonOperation.getAttributeName()+"\")";
117+
String val=JSONOBJECT_PARAM_NAME+".get"+simpleAssignments.get(typeName)+"(\""+jsonOperation.getAttributeName()+"\")";
118118
switch (jsonOperation.getOpType()) {
119119
case FIELD:
120120
writer.addAssignment("ret."+jsonOperation.getAttributeName(), val);
@@ -123,7 +123,6 @@ private void generateJSON(Element element, String fullyQualidiedClassName, Class
123123
writer.addMethodCall("ret","set"+Character.toUpperCase(jsonOperation.getAttributeName().charAt(0))+(jsonOperation.getAttributeName().length()>1?jsonOperation.getAttributeName().substring(1):""),val);
124124
break;
125125
}
126-
127126
}else {
128127
processingEnv.getMessager().printMessage(Kind.ERROR, "type "+typeName+" is currenly not supported");
129128
}
@@ -138,7 +137,42 @@ private void generateJSON(Element element, String fullyQualidiedClassName, Class
138137

139138
writer.beginMethod("fromJSON", element.toString(),
140139
new VariableDefinition(String.class.getCanonicalName(), JSONOBJECT_PARAM_NAME));
141-
writer.addMethodCall(fullyQualidiedClassName,"fromJSON","new JSONObject("+JSONOBJECT_PARAM_NAME+")");
140+
writer.addReturn("fromJSON(new "+JSONObject.class.getCanonicalName()+"("+JSONOBJECT_PARAM_NAME+"))");
141+
writer.endMethod();
142+
143+
writer.beginMethod("toJSONObject", JSONObject.class.getCanonicalName(),
144+
new VariableDefinition(element.toString(), "obj"));
145+
146+
writer.addAssignment(JSONObject.class.getCanonicalName()+" "+JSONOBJECT_PARAM_NAME,"new "+JSONObject.class.getCanonicalName()+"()");
147+
148+
for (JSONOperation jsonOperation : operations) {
149+
TypeMirror type = jsonOperation.getType();
150+
String typeName=type.toString();
151+
if(simpleAssignments.containsKey(typeName)) {
152+
String val=null;
153+
//=JSONOBJECT_PARAM_NAME+"."+simpleAssignments.get(typeName)+"(\""+jsonOperation.getAttributeName()+"\")";
154+
switch (jsonOperation.getOpType()) {
155+
case FIELD:
156+
val="obj."+jsonOperation.getAttributeName();
157+
break;
158+
case PROPERTY:
159+
val="obj.get"+Character.toUpperCase(jsonOperation.getAttributeName().charAt(0))+(jsonOperation.getAttributeName().length()>1?jsonOperation.getAttributeName().substring(1):"")+"()";
160+
break;
161+
}
162+
if(val!=null){
163+
writer.addMethodCall(JSONOBJECT_PARAM_NAME,"put","\""+jsonOperation.getAttributeName()+"\"",val);
164+
}
165+
}else {
166+
processingEnv.getMessager().printMessage(Kind.ERROR, "type "+typeName+" is currenly not supported");
167+
}
168+
//TODO objects, arrays, primitives
169+
}
170+
writer.addReturn("data");
171+
writer.endMethod();
172+
173+
writer.beginMethod("toJSON", String.class.getCanonicalName(),
174+
new VariableDefinition(element.toString(), "obj"));
175+
writer.addReturn("toJSONObject(obj).toString()");
142176
writer.endMethod();
143177

144178
writer.endClass();

0 commit comments

Comments
 (0)