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;
|
import java.util.Map;
|
||||||
|
|
||||||
public class Translation {
|
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) {
|
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();
|
json.readJson();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String t(String key, Object... args) {
|
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 {
|
public class NightJson {
|
||||||
private static final String KEY_PATTERN = "\"(-?.*)\":";
|
private static final String KEY_PATTERN = "\"(-?.*)\":";
|
||||||
Class<?> jsonClass;
|
Class<?> jsonClass;
|
||||||
Map<String, ?> jsonMap;
|
Field jsonMap;
|
||||||
String fileLocation;
|
String fileLocation;
|
||||||
|
|
||||||
public NightJson(Class<?> jsonClass, String fileLocation) {
|
public NightJson(Class<?> jsonClass, String fileLocation) {
|
||||||
this.jsonClass = jsonClass;
|
this.jsonClass = jsonClass;
|
||||||
this.fileLocation = fileLocation;
|
this.fileLocation = fileLocation;
|
||||||
}
|
try {
|
||||||
public NightJson(Map<String, ?> jsonMap, String fileLocation) {
|
Field f = jsonClass.getField("jsonMap");
|
||||||
this.jsonMap = jsonMap;
|
if (f.getType() == Map.class && getTypeArgument(f, 0) == String.class) jsonMap = f;
|
||||||
this.fileLocation = fileLocation;
|
} catch (NoSuchFieldException ignored) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeJson() {
|
public void writeJson() {
|
||||||
@@ -34,16 +34,18 @@ public class NightJson {
|
|||||||
FileWriter jsonFile = new FileWriter(fileLocation);
|
FileWriter jsonFile = new FileWriter(fileLocation);
|
||||||
|
|
||||||
jsonFile.write("{\n");
|
jsonFile.write("{\n");
|
||||||
if (jsonClass != null) {
|
{
|
||||||
Iterator<Field> it = Arrays.stream(jsonClass.getFields()).iterator();
|
Iterator<Field> it = Arrays.stream(jsonClass.getFields()).iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
Field field = it.next();
|
Field field = it.next();
|
||||||
|
if (field == jsonMap) continue;
|
||||||
writeElement(jsonFile, field.get(null), field.getType(), field.getName());
|
writeElement(jsonFile, field.get(null), field.getType(), field.getName());
|
||||||
jsonFile.write(it.hasNext() ? ",\n" : "\n");
|
jsonFile.write(it.hasNext() ? ",\n" : "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (jsonMap != null) {
|
if (jsonMap != null) {
|
||||||
Iterator<String> it = jsonMap.keySet().iterator();
|
//noinspection unchecked
|
||||||
|
Iterator<String> it = ((Map<String,?>)jsonMap.get(null)).keySet().iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
String key = it.next();
|
String key = it.next();
|
||||||
Object value = jsonMap.get(key);
|
Object value = jsonMap.get(key);
|
||||||
@@ -100,19 +102,18 @@ public class NightJson {
|
|||||||
for (String key : jsonKeyValuePairs.keySet()) {
|
for (String key : jsonKeyValuePairs.keySet()) {
|
||||||
String currentString = jsonKeyValuePairs.get(key);
|
String currentString = jsonKeyValuePairs.get(key);
|
||||||
//System.out.printf("Key: %s Value: %s%n", key, currentString);
|
//System.out.printf("Key: %s Value: %s%n", key, currentString);
|
||||||
|
boolean isInClass = false;
|
||||||
if (jsonClass != null) {
|
if (jsonClass != null) {
|
||||||
Field field;
|
Field field;
|
||||||
try {
|
try {
|
||||||
field = jsonClass.getField(key);
|
field = jsonClass.getField(key);
|
||||||
} catch (NoSuchFieldException e) {
|
field.set(field, stringToFieldObj(currentString, field));
|
||||||
continue;
|
isInClass = true;
|
||||||
}
|
} catch (NoSuchFieldException ignored) {}
|
||||||
|
|
||||||
field.set(field, stringToFieldObj(currentString, field));
|
|
||||||
}
|
}
|
||||||
else if (jsonMap != null) {
|
if (jsonMap != null && !isInClass) {
|
||||||
//noinspection unchecked
|
//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();
|
jsonFile.close();
|
||||||
@@ -148,8 +149,8 @@ public class NightJson {
|
|||||||
String pair = it.next();
|
String pair = it.next();
|
||||||
if (!pair.contains(":")) break;
|
if (!pair.contains(":")) break;
|
||||||
int semicolonPos = pair.indexOf(":");
|
int semicolonPos = pair.indexOf(":");
|
||||||
Class<?> keyType = getPrimitiveType((Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]);
|
Class<?> keyType = getTypeArgument(field, 0);
|
||||||
Class<?> valType = getPrimitiveType((Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[1]);
|
Class<?> valType = getTypeArgument(field, 1);
|
||||||
map.put(stringToObj(pair.substring(0, semicolonPos), keyType),
|
map.put(stringToObj(pair.substring(0, semicolonPos), keyType),
|
||||||
stringToObj(pair.substring(semicolonPos+2), valType));
|
stringToObj(pair.substring(semicolonPos+2), valType));
|
||||||
}
|
}
|
||||||
@@ -172,6 +173,10 @@ public class NightJson {
|
|||||||
else return currentString;
|
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) {
|
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)
|
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; }
|
} catch (NoSuchFieldException | IllegalAccessException ignored) { return rawType; }
|
||||||
|
|||||||
Reference in New Issue
Block a user