Is it possible to programmatically change a json resource file? Or change FileUtils.java to use a different json file depending on user type?

478 views Asked by At

I know my question is worded somewhat confusingly, but...

My app is using an instant messaging SDK called Applozic. In order to hide/add certain features into the UI, there is an applozic-settings.json file which can be edited.

For example: "hideGroupExitButton": true

My app has two types of users: senders and receivers. Senders have some capabilities that receivers do not... so I need to change some of these values depending on the user type.

SO: is it possible to programmatically change some of these values based on user type?

(applozic-settings.json is only used in FileUtils.java-- which is a LOCKED file...)

Or, do I need to create a new json file for one of the user types, and somehow change FileUtils.java to use the proper file?

This is the relevant code in FileUtils.java:


public static String loadSettingsJsonFile(Context context) {
    BufferedReader br = null;
    StringBuffer sb = new StringBuffer();
    try {
        br = new BufferedReader(new InputStreamReader(context.getAssets().open(
                "applozic-settings.json"), "UTF-8"));
        String line;
        if (br != null) {
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        }
    } catch (IOException ioe) {
        return null;
    } catch (Exception e) {
    } finally {
        try {
            if (br != null) {
                br.close();
            }
        } catch (IOException e) {
        }
    }
    return sb.toString();
}

1

There are 1 answers

0
ty1 On

If I understand correctly, it is possible to edit the values of a JSON file. See: JSON File - Java: editing/updating fields values

If you wanted to open a different file based on the user, just store the different files in strings for example here:

br = new BufferedReader(new InputStreamReader(context.getAssets().open(
                "applozic-settings.json"), "UTF-8"));

You could simply say string file = "applozic-settings.json";

and directly pass it into the function above:

br = new BufferedReader(new InputStreamReader(context.getAssets().open(
                file), "UTF-8"));

So in essence you could say:

string file;
if(userType == x){
   file="x.json"
}
else{
   file="y.json"
}