I came from LibGDX where I can load objects from JSON-files. My own wrapper over LibGDX-JSON-utils makes it possible to create objects from JSON-files simple using calls like:
final String relativePathToEnemyDataStruct = "persons/enemy_1.json";
JsonLoader enemyDataLoader = new JsonLoader();
var enemyDataStruct = enemyDataLoader.loadFromInternal(relativePathToEnemyDataStruct, EnemyDataStruct.class);
…which calls the next code:
public @Null <T> T loadFromInternal(String relativePath, Class classDef){
FileHandle fileHandle = getPathWithExtension(Gdx.files.internal(relativePath).file().getAbsolutePath());
Object object = load(fileHandle, classDef);
return (T) object;
}
… And so on in the deep of the LibGDX-internals.
I didn’t found any information about similar tools in JME3. Which paradigm does JME3 use when the data must be loaded externally? Should I prefer another methods or file-formats or avoid reflection? What are the best practices?
Or you can use whatever you want. Depends on your needs. JSON is fine, XML is fine. Everything is fine. Many JSON/XML/WHATEVER libraries are really easy to use can serialize/deserialize Java classes without much effort.
Google’s GSON if you want something light-weight and simple. (May just work as is.)
Jackson if you want the more “industry standard” completely extensible. (There is also jackson-lite for somewhere in-between.)
I’ve used both.
I cannot understand why a “Game Engine” would roll their own JSON library… I could see supporting some internal customization for serializing/deserializing scenes (like JME has its own j3o and xml serialization)… but reinventing the JSON wheel itself seems a bit too “not invented here” for my tastes.
Edit: P.S.: I do NOT recommend using JME’s built in serialization for anything other than the JME scene graph. You are better off just using regular Java serialization if you want incomprehensible binary data.
Or you can write your own JsonUtility class (to hide the implementation of the json library used, in this case GSON). These kinds of classes can be generated in minutes using AI.
In any case, all the advice that @tonihele and @pspeed have given you is clear and equally valuable.
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
*
* @author capdevon
*/
public class JsonUtility {
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private JsonUtility() {}
public static String toJson(Object obj) {
return gson.toJson(obj);
}
public static <T> T fromJson(String json, Class<T> clazz) {
return gson.fromJson(json, clazz);
}
public static <T> T fromJsonFile(File f, Class<T> clazz) throws IOException {
try (FileReader reader = new FileReader(f)) {
return gson.fromJson(reader, clazz);
}
}
public static void toJsonFile(Object obj, File f) throws IOException {
try (FileWriter writer = new FileWriter(f)) {
gson.toJson(obj, writer);
}
}
}
It will be comfortable with j3o, if JME3 would have a GUI-supported tool for editing j3o-files with primitive data types, lists, maps and POJO-classes. In this case I would simple convert my JSONs in .j3o and use this tool (like a simple text editor) to adjust some game design parameters inside the j3o-files, like “health of an enemy” or “invisible time after a checkpoint” and so on.
Are there some tools for this purpose in the JME3 or in the community repos?
JME’s j3o format is an awful format and you should absolutely not use it for your game data.
It bears repeating: DO NOT USE IT FOR YOUR CUSTOM GAME DATA.
It is the product of a little bit of “we needed this special thing” (we didn’t) and a whole lot of “not invented here”… and a small tiny ounce of “Well, we wanted to write to XML, too…” which was broken for most of JME 3’s life anyway.
JME is stuck with it for scene data and it’s not bad for that because “momentum”… but you are absolutely not stuck with it and you can throw a dart into a field of technologies and EASILY hit something better.
If you want text editability then JSON is a fine format. For which there are already two EXTREMELY GOOD libraries that any “game engine” would be utterly and completely stupid for trying to reinvent. If your data objects are already well-formed “beans” then GSON or Jackson will work out of the box.
…and if that’s true, Java’s built in JAXB probably gets you XML serialization without any extra work, too.
And basically what you are asking is with the tools, I’m sure such are more readily available with JSON/XML. Being pretty much standards, chances are you don’t even need to roll your own editor for these. If you need some UI tool to manipulate them that is.