feat: add translation support

This commit is contained in:
Martin Prokoph
2025-08-02 11:15:53 +02:00
parent ec1841a592
commit dfba8b397e
14 changed files with 173 additions and 43 deletions

View File

@@ -9,19 +9,24 @@ import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
/*
NightJson v0.1 by Martin Prokoph
NightJson v0.2 by Martin Prokoph
Extremely lightweight (and incomplete) JSON library
Concept inspired by GSON
*/
public class NightJson {
private static final String KEY_PATTERN = "\"(-?[0-9a-zA-Z]*)\":";
private static final String KEY_PATTERN = "\"(-?.*)\":";
Class<?> jsonClass;
Map<String, ?> jsonMap;
String fileLocation;
public NightJson(Class<?> jsonClass, String fileLocation) {
this.jsonClass = jsonClass;
this.fileLocation = fileLocation;
}
public NightJson(Map<String, ?> jsonMap, String fileLocation) {
this.jsonMap = jsonMap;
this.fileLocation = fileLocation;
}
public void writeJson() {
if (fileLocation == null) return;
@@ -29,17 +34,22 @@ public class NightJson {
FileWriter jsonFile = new FileWriter(fileLocation);
jsonFile.write("{\n");
Iterator<Field> it = Arrays.stream(jsonClass.getFields()).iterator();
while (it.hasNext()) {
Field field = it.next();
jsonFile.write("\t");
if (field.getType() == Comment.class) {
jsonFile.write(String.format("// %s\n", ((Comment) field.get(null)).commentString));
continue;
if (jsonClass != null) {
Iterator<Field> it = Arrays.stream(jsonClass.getFields()).iterator();
while (it.hasNext()) {
Field field = it.next();
writeElement(jsonFile, field.get(null), field.getType(), field.getName());
jsonFile.write(it.hasNext() ? ",\n" : "\n");
}
}
else if (jsonMap != null) {
Iterator<String> it = jsonMap.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
Object value = jsonMap.get(key);
writeElement(jsonFile, jsonMap.get(key), value.getClass(), key);
jsonFile.write(it.hasNext() ? ",\n" : "\n");
}
jsonFile.write(String.format("\"%s\": ", field.getName()));
jsonFile.write(objToString(field.get(null), field.getType()));
jsonFile.write(it.hasNext() ? ",\n" : "\n");
}
jsonFile.write("}");
jsonFile.close();
@@ -49,6 +59,16 @@ public class NightJson {
}
}
private void writeElement(FileWriter jsonFile, Object value, Class<?> type, String name) throws IOException, IllegalAccessException {
jsonFile.write("\t");
if (type == Comment.class) {
jsonFile.write(String.format("// %s\n", ((Comment) value).commentString));
return;
}
jsonFile.write(String.format("\"%s\": ", name));
jsonFile.write(objToString(value, type));
}
public void readJson() {
if (fileLocation == null) return;
try {
@@ -80,11 +100,20 @@ public class NightJson {
for (String key : jsonKeyValuePairs.keySet()) {
String currentString = jsonKeyValuePairs.get(key);
//System.out.printf("Key: %s Value: %s%n", key, currentString);
Field field;
try { field = jsonClass.getField(key);
} catch (NoSuchFieldException e) {continue;}
if (jsonClass != null) {
Field field;
try {
field = jsonClass.getField(key);
} catch (NoSuchFieldException e) {
continue;
}
field.set(field, stringToFieldObj(currentString, field));
field.set(field, stringToFieldObj(currentString, field));
}
else if (jsonMap != null) {
//noinspection unchecked
((Map<String, Object>)jsonMap).put(key, stringToObj(currentString, String.class)); // TODO: Un-hardcode string type
}
}
jsonFile.close();
} catch (IOException | IllegalAccessException | NoSuchElementException e) {
@@ -93,7 +122,7 @@ public class NightJson {
}
}
private String objToString(Object value, Class<?> type) throws IllegalAccessException {
private String objToString(Object value, Class<?> type) {
if (type == Map.class) {
StringBuilder mapPairs = new StringBuilder();
Map<?, ?> map = (Map<?, ?>) value;