What Is the Benefit of Using JSONs over Classes with Dictionaries to Store Information?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By scrubswithnosleeves

Hey All,

I am making a dialogue system that uses a JSON to store a dictionary of dialogue options.

  "Father_002": [
    "That mech over there needs to have it's AI's focus reset.",
    "Head over to the computer and give it a shot would ya?"

My question is, what is the benefit of using a JSON versus just making a script with the class_name Dialogue and a dictionary of dialogue just like I have in the JSON. Then I could just do Dialogue.dict.get("Father_002") rather than using my current method of loading the JSON and then accessing that key. I actually access the JSON relatively frequently, and it would be easier to edit and access the class from the Editor, so I am just wondering if there is any major advantage of using the JSON to do this?

:bust_in_silhouette: Reply From: exuin

The advantage of JSON is that it can be used for other applications than just the game you’re making since it’s a data interchange format. If you’re just planning on using the data for your game only, then there’s not really an advantage to using JSON.

:bust_in_silhouette: Reply From: Mario

To be fair, I’d probably combine both approaches:

  • Store the data in your own class, modelling exactly what you want.
  • Load the data from JSON and save it to JSON as a human-readable format avoiding other issues (that are possible when loading/saving Godot resources).

But why? A few points I can think of:

  • Accessing the JSON data directly provides only a minimalistic form of control:

    • You can’t sanitize the data, unless you add extra processing (which break the original idea of just using JSON). Think of your program expecting a number, but the JSON actually providing some kind of string.
    • There’s no versioning happening. Data is just read 1:1. If you’d have to handle older versions of some settings file, you’d once again add extra processing.
    • There are no default values or you have to handle them whenever you read something. Or again process/add defaults in an extra step, breaking the whole idea behind this approach.
  • Managing the data through your own class/node gives you several benefits:

    • You have full control over which values are interpreted/read into your storage.
    • You can have one central position in your code where you define default values applied if loading fails (or a value is missing/invalid).
    • You add versioning to your file. The structure might change later, but by reading a version number you’ll always know which values to expect at a certain location.
    • By loading/saving JSON (rather than loading/saving your class as a Godot resource) you’re also preventing any code or values getting injected through the stored file, since a resource could also have stuff like a script attached.