I have a Java project that requires the use of arrays with complex data stored in them. In order to make it easier to edit I’m creating a GUI in Godot. That will auto-generate the necessary file to be read by the Java project. I have 2 (and a bonus third option) for how I’m thinking about storing this data. Either I could:
Use a Json file
Pros:
A Json doesn’t require too much extra code to get working in Java nor Godot.
Cons:
The code structure in Java works better indexing an Array since values should be indexed through linearly.
Save as a .java file
Pros:
No extra code to get working in Java. This is good since I’m planning on making this a software to be used for more than just me and asking people to do extra setup is annoying and I can save it as an Array which is the best case circumstance
Cons:
I’d need to make a custom interpreter in Godot to be able to pull values from the .java file so somebody could edit the array in Godot post generation.
Make a custom file type
This option is so stupid, I don’t know why I thought it’d be a good idea. It’s just so much work. The only benefit would be that I get to learn something but I’m kinda on a time limit for this project.
The core question is, how do I make a file interpreter in Godot so it can read .java files? This is the ideal thing that I’d want to happen, I just put up the other stuff so people could tell me if another idea is so much better.
Sounds like a lexer and parser for .java code.
Not a small task by any means.
I would search the internet for pre-existing lexer/parser as this is going to be not trivial and you would be duplicating already existing projects.
It depends on your ability level, but maybe you can start here for a general idea on lexers.
And/or here for more in depth look at the situation.
I am glad I don’t need to do this.
2 Likes
This is going to be so easy!
3 Likes
JSON can store arrays
May be more helpful if you describe more about your project, we can’t recommend a data structure without knowing the data.
5 Likes
It’s just difficult since if I explained what this was for it would be giving away a bit of personal life stuff. I guess basically it’s storing a list of positions, but I can’t really elaborate further. Thanks though for that clarification about Jsons. I’ve only really used them for small things in Minecraft modding and don’t know too much about how they work so this was helpful because I thought jsons were exclusively like dictionaries.
I understand that in Java you’ll have a list (or array) of objects representing the information. These objects should be serializable, and you should write custom methods like “writeObject” and “readObject”. The serialization format can be anything: CSV data, data separated by a delimiter (-#~) in the correct order, XML, JSON, etc. Use whatever best suits your needs.
I prefer XML to JSON. JSON is a horrible format, in my opinion.
Ultimately, if it’s XML, you’ll simply have methods like “toXml” and “fromXml” that save your Java object in that format or create it from that format.
Generating any format you choose from the Godot interface shouldn’t be a problem.
Godot’s XML parser isn’t great, but it’s sufficient for this.
XML is more expressive than JSON and is human-readable.
JSON is more useful if you want to share that information with others. Sadly, it’s a trend that has become the standard for APIs and other things.
I don’t know if the latest versions of Java include JSON support, or if you’ll have to force the use of a library like json.org or Jackson. That would require you to add several JAR files to your Java application just for handling JSON. XML has always been included in the Java SDK (I don’t know if they’ve removed it in newer versions either). You should definitely keep that in mind.
1 Like