-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1602e3
commit 0bc0d2a
Showing
6 changed files
with
92 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 0 additions & 56 deletions
56
src/main/java/io/github/sakurawald/config/serializer/CommentTypeAdapterFactory.java
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
src/main/java/io/github/sakurawald/config/serializer/ConfigModelSerializer.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import com.google.gson.*; | ||
import io.github.sakurawald.config.model.ConfigModel; | ||
import io.github.sakurawald.config.serializer.Comment; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
|
||
public class BuildConfigurationDocumentTest { | ||
|
||
private JsonObject buildJsonObjectWithComments(Object obj) { | ||
JsonObject jsonObject = new JsonObject(); | ||
processFieldsWithComments(obj, jsonObject); | ||
return jsonObject; | ||
} | ||
|
||
private void processFieldsWithComments(Object obj, JsonObject jsonObject) { | ||
Class<?> clazz = obj.getClass(); | ||
for (Field field : clazz.getFields()) { | ||
try { | ||
String fieldName = field.getName(); | ||
Object value = field.get(obj); | ||
|
||
/* insert related comment property */ | ||
if (field.isAnnotationPresent(Comment.class)) { | ||
Comment commentAnnotation = field.getAnnotation(Comment.class); | ||
jsonObject.addProperty(fieldName + "@comment", commentAnnotation.value()); | ||
} | ||
|
||
/* switch type */ | ||
if (value != null) { | ||
if (isPrimitiveOrWrapper(field.getType())) { | ||
jsonObject.addProperty(fieldName, value.toString()); | ||
} else if (List.class.isAssignableFrom(field.getType())) { | ||
JsonArray jsonArray = new JsonArray(); | ||
for (Object listItem : (List<?>) value) { | ||
if (listItem != null) { | ||
jsonArray.add(new Gson().toJsonTree(listItem)); | ||
} | ||
} | ||
jsonObject.add(fieldName, jsonArray); | ||
} else { | ||
JsonObject nestedJsonObject = new JsonObject(); | ||
processFieldsWithComments(value, nestedJsonObject); | ||
jsonObject.add(fieldName, nestedJsonObject); | ||
} | ||
} | ||
|
||
|
||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
} | ||
|
||
private boolean isPrimitiveOrWrapper(Class<?> clazz) { | ||
return clazz.isPrimitive() || clazz == Boolean.class || clazz == Character.class || | ||
Number.class.isAssignableFrom(clazz) || clazz == String.class; | ||
} | ||
|
||
private void writeToFile(String fileName, JsonObject content) { | ||
Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
String filePath = "./build/document/" + fileName; | ||
new File(filePath).getParentFile().mkdirs(); | ||
try (FileWriter writer = new FileWriter(filePath)) { | ||
writer.write(gson.toJson(content)); | ||
System.out.println("File " + fileName + " has been written successfully."); | ||
} catch (IOException e) { | ||
System.err.println("An error occurred while writing file: " + e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
void buildConfigurationDocument() { | ||
writeToFile("config.json", buildJsonObjectWithComments(new ConfigModel())); | ||
} | ||
} |