Save and recall variable in a file

Hi,
I’m trying to save several variables to a file so I can reload them when the program opens. I manage to create a JSON file but I have to manually add the code, I would like to automate this by adding in an Array, the variables that I would like to save and recall. I managed to save my variables in a JSON file but I can’t extract these values ​​to reassign them to variables.

Old code that is working :

      json = new JSONObject();
      json.setInt("Mode", Mode);
      json.setInt("PortServeur", PortServeur);
      json.setString("IpRP1", IpRP1);
      json.setInt("PortRP1", PortRP1);
      json.setString("IpRP2", IpRP2);
      json.setInt("PortRP2", PortRP2);
      json.setString("IpRP3", IpRP3);
      json.setInt("PortRP3", PortRP3);
      json.setString("IpRP4", IpRP4);
      json.setInt("PortRP4", PortRP4);
      json.setString("IpLive", IpLive);
      json.setInt("PortLive", PortLive);
      json.setInt("DureePenalite", DureePenalite);
      saveJSONObject(json, "data/config.json");

void setup() {
  // Load JSON
  json = loadJSONObject("config.json");
  Mode = json.getInt("Mode");
  PortServeur = json.getInt("PortServeur");
  IpRP1 = json.getString("IpRP1");
  PortRP1 = json.getInt("PortRP1");
  IpRP2 = json.getString("IpRP2");
  PortRP2 = json.getInt("PortRP2");
  IpLive = json.getString("IpLive");
  PortLive = json.getInt("PortLive");
  ...

New code trying to automate (the names of the variables have changed in the meantime) :

void saveConfig() {
  int[] JsonIntValeur = { Mode, PortServeur, PortBuzzer1, PortBuzzer2, PortBuzzer3, PortBuzzer4, PortLive, PortMoniteur1, PortMoniteur2, DureePenalite};
  String[] JsonStringValeur = { IpBuzzer1, IpBuzzer2, IpBuzzer3, IpBuzzer4, IpLive, IpMoniteur1, IpMoniteur2};

  JSONArray valuesInt = new JSONArray();
  JSONArray valuesString = new JSONArray();

  for (int i = 0; i < JsonIntValeur.length; i++) {
    JSONObject config = new JSONObject();
    config.setInt(JsonIntNom[i], JsonIntValeur[i]);
    valuesInt.setJSONObject(i, config);
  }

  for (int i = 0; i < JsonStringValeur.length; i++) {
    JSONObject config2 = new JSONObject();
    config2.setString(JsonStringNom[i], JsonStringValeur[i]);
    valuesString.setJSONObject(i, config2);
  }

  json = new JSONObject();
  json.setJSONArray("Int", valuesInt);
  json.setJSONArray("String", valuesString);

  saveJSONObject(json, "data/config.json");
}
void loadConfig() {
  int[] JsonIntValeur = { Mode, PortServeur, PortBuzzer1, PortBuzzer2, PortBuzzer3, PortBuzzer4, PortLive, PortMoniteur1, PortMoniteur2, DureePenalite};
  String[] JsonStringValeur = { IpBuzzer1, IpBuzzer2, IpBuzzer3, IpBuzzer4, IpLive, IpMoniteur1, IpMoniteur2};
  json = loadJSONObject("config.json");
  for (int i = 0; i < JsonIntValeur.length; i++) {
    JsonIntValeur[i] = json.getInt(JsonIntNom[i]);
  }
}

I see 4 arrays in your attempt, not just 1:

  1. JsonIntNom[]
  2. JsonIntValeur[]
  3. JsonStringNom[]
  4. JsonStringValeur[]

Can’t you just use the JSONObject itself in place of your many variables?

For example, if you wanna read the “Mode” “variable” you use: mode = json.getInt("Mode");.
If you wanna assign to “Mode” you use: json.setInt("Mode", mode);.

Indeed, I had to make two Array for the values, one for the int and one for the strings. Which I doubled with an array where I have the variable name. I could have used maybe a hashmap or a table.

Is it possible to extract the variables from the array?
without rewriting this formula for all the values…
mode = json.getInt("Mode");
With a “for loop” ?
Otherwise indeed, I replace all my variables of my program by references to this array. Is this a generalized practice? I’m new to programming, I don’t have the best practices yet

The only way Java would allow us to access fields from a class via an array of String names is by using reflection (a.K.a. introspection), which would be an over-the-top solution btW.

Both containers would provide access to their content very similar to how JSONObject or JSONArray do already.

That is, you’d still use get()-like methods w/ a String argument to get a value from them.

IMO you should stick w/ JSONObject and treat its entries as if they were actual variables.

In order to make things safer for such approach, don’t declare Mode, PortServeur, IpLive, etc. as global variables.

Instead declare them as local variables whenever reading from the JSONObject container like this:

  • int mode = json.getInt("Mode");
  • String ipLive = json.getString("IpLive");

And if you happen to update the value on any of those local variables, don’t forget to write it back to their corresponding JSONObject entry after finishing:

int mode = json.getInt("Mode");
String ipLive = json.getString("IpLive");

mode -= 5;
mode *= 3;
ipLive += mode;

json.setInt("Mode", mode);
json.setString("IpLive", ipLive);

That’s pretty much it.

1 Like

Thanks for your help !