feat(NightJson): add arbitrary key-val pairs via map
This commit is contained in:
@@ -6,14 +6,14 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Translation {
|
||||
public static Map<String, String> translationMap = new HashMap<>();
|
||||
public static Map<String, String> jsonMap = new HashMap<>();
|
||||
|
||||
public static void load(String locale) {
|
||||
NightJson json = new NightJson(translationMap, String.format("translation/%s.json5", locale));
|
||||
NightJson json = new NightJson(Translation.class, String.format("translation/%s.json5", locale));
|
||||
json.readJson();
|
||||
}
|
||||
|
||||
public static String t(String key, Object... args) {
|
||||
return String.format(translationMap.getOrDefault(key, key), args);
|
||||
return String.format(jsonMap.getOrDefault(key, key), args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,16 +16,16 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
public class NightJson {
|
||||
private static final String KEY_PATTERN = "\"(-?.*)\":";
|
||||
Class<?> jsonClass;
|
||||
Map<String, ?> jsonMap;
|
||||
Field 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;
|
||||
try {
|
||||
Field f = jsonClass.getField("jsonMap");
|
||||
if (f.getType() == Map.class && getTypeArgument(f, 0) == String.class) jsonMap = f;
|
||||
} catch (NoSuchFieldException ignored) {}
|
||||
}
|
||||
|
||||
public void writeJson() {
|
||||
@@ -34,16 +34,18 @@ public class NightJson {
|
||||
FileWriter jsonFile = new FileWriter(fileLocation);
|
||||
|
||||
jsonFile.write("{\n");
|
||||
if (jsonClass != null) {
|
||||
{
|
||||
Iterator<Field> it = Arrays.stream(jsonClass.getFields()).iterator();
|
||||
while (it.hasNext()) {
|
||||
Field field = it.next();
|
||||
if (field == jsonMap) continue;
|
||||
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();
|
||||
if (jsonMap != null) {
|
||||
//noinspection unchecked
|
||||
Iterator<String> it = ((Map<String,?>)jsonMap.get(null)).keySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
String key = it.next();
|
||||
Object value = jsonMap.get(key);
|
||||
@@ -100,19 +102,18 @@ public class NightJson {
|
||||
for (String key : jsonKeyValuePairs.keySet()) {
|
||||
String currentString = jsonKeyValuePairs.get(key);
|
||||
//System.out.printf("Key: %s Value: %s%n", key, currentString);
|
||||
boolean isInClass = false;
|
||||
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));
|
||||
isInClass = true;
|
||||
} catch (NoSuchFieldException ignored) {}
|
||||
}
|
||||
else if (jsonMap != null) {
|
||||
if (jsonMap != null && !isInClass) {
|
||||
//noinspection unchecked
|
||||
((Map<String, Object>)jsonMap).put(key, stringToObj(currentString, String.class)); // TODO: Un-hardcode string type
|
||||
((Map<String,Object>)jsonMap.get(null)).put(key, stringToObj(currentString, getTypeArgument(jsonMap, 1)));
|
||||
}
|
||||
}
|
||||
jsonFile.close();
|
||||
@@ -148,8 +149,8 @@ public class NightJson {
|
||||
String pair = it.next();
|
||||
if (!pair.contains(":")) break;
|
||||
int semicolonPos = pair.indexOf(":");
|
||||
Class<?> keyType = getPrimitiveType((Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]);
|
||||
Class<?> valType = getPrimitiveType((Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[1]);
|
||||
Class<?> keyType = getTypeArgument(field, 0);
|
||||
Class<?> valType = getTypeArgument(field, 1);
|
||||
map.put(stringToObj(pair.substring(0, semicolonPos), keyType),
|
||||
stringToObj(pair.substring(semicolonPos+2), valType));
|
||||
}
|
||||
@@ -172,6 +173,10 @@ public class NightJson {
|
||||
else return currentString;
|
||||
}
|
||||
|
||||
private static Class<?> getTypeArgument(Field field, int index) {
|
||||
return getPrimitiveType((Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[index]);
|
||||
}
|
||||
|
||||
public static Class<?> getPrimitiveType(Class<?> rawType) {
|
||||
try { return (Class<?>) rawType.getField("TYPE").get(null); // Tries to get primitive types from non-primitives (e.g. Boolean -> boolean)
|
||||
} catch (NoSuchFieldException | IllegalAccessException ignored) { return rawType; }
|
||||
|
||||
Reference in New Issue
Block a user