I am currently using json for my code to retrieve an array of booleans. However, I am adding a subfield to reach another array inside, but I am not very good at json and somehow stuck on how to do this. Here is my code:
field values:
public enum Field { FIELD_CAN_CHANGE_PASSWORD("canChangePassword", true, canUpdate), FIELD_MAX_AUTO_DOWNLOAD_SIZE("maxAutoDownloadSize", 5000000L), FIELD_ALWAYS_REAUTHENTICATE("alwaysReauthenticate", false, canUpdate), FIELD_CAN_START_CALL("canStartCall", false), FIELD_ROOMS_ENABLED("roomsEnabled", !Core.isMessenger()), FIELD_CAN_CREATE_ROOM("canCreateRoom", !Core.isMessenger(), canUpdate), FIELD_MAX_ENVELOPE_TTL("maxTTL", Core.isMessenger() ? 518400L : 31536000L, canUpdate), FIELD_MAX_BURN_ON_READ_TTL("maxBOR", 0L, canUpdate), FIELD_MAX_UPLOAD_SIZE("maxUploadSize", -1L, true), FIELD_FRIEND_FINDER("friendFinder", !Core.isEnterprise(), canUpdate), FIELD_ONLY_SHOW_IN_NETWORK_CONTACTS("onlyShowInNetwork", false), FIELD_CAN_ADD_CONTACT("canAddContact", true, canUpdate), FIELD_FORCE_DEVICE_LOCKOUT("forceDeviceLockout", 5L, canUpdate), FIELD_VERIFICATION_MODE("verificationMode", VerificationMode.OPTIONAL.getValue(), true), FIELD_ENABLE_NOTIFICATION_PREVIEW("enableNotificationPreview", true, true), FIELD_DIRECTORY_ENABLED("directoryEnabled", true, true); public String fieldName; public Object defaultValue; public boolean updateFromServer; Field(String key, Object defaultValue) { this(key, defaultValue, true); } Field(String key, Object defaultValue, boolean updateFromServer) { this.fieldName = key; this.defaultValue = defaultValue; this.updateFromServer = updateFromServer; } }
entering values ββin the field:
private void putValueForField(JSONObject configuration, Field field) { try { if (configuration.isNull(field.fieldName)) { Object value = field.defaultValue; if (value instanceof long[]) { JSONArray array = new JSONArray(); for (long obj : (long[]) field.defaultValue) { array.put(obj); } value = array; } runtimeConfiguration.put(field.fieldName, value); } else { runtimeConfiguration.put(field.fieldName, configuration.get(field.fieldName)); } } catch (JSONException e) { } }
getting values:
private Object getValueForField(Field field) { if (runtimeConfiguration.has(field.fieldName) && field.updateFromServer) { try { Object value = runtimeConfiguration.get(field.fieldName); if (value instanceof JSONArray) { JSONArray values = (JSONArray) value; if (values.get(0) instanceof Number) { long[] retVals = new long[values.length()]; for (int i = 0; i < values.length(); i++) { retVals[i] = ((Number) values.get(i)).longValue(); } return retVals; } } else if (value instanceof Number) { return ((Number) value).longValue(); } else { return value; } } catch (Exception e) { e.printStackTrace(); } } return field.defaultValue; }
one of the methods using the fields above:
public boolean canChangePassword() { return (boolean) getValueForField(Field.FIELD_CAN_CHANGE_PASSWORD); }
My new json:
{"enableNotificationPreview":true,"destructOnRead":[30,60,300],"alwaysReauthenticate":false,"forceDeviceLockout":0,"permmod":1516894585,"maxBOR":0,"roomsEnabled":true,"directoryEnabled":true,"canStartCall":true,"canAddContact":true,"legacyDownload":false,"verificationMode":1,"restrictedAdmin":false,"canChangePassword":true,"friendFinder":true,"NEWVALUE":{"canStartNewValue1":true,"canStartroupValue":true,"canVideoCall":true,"canStartRoomValue":true,"canAddtoValue":true,"canStartValueshare":true},"canCreateRoom":true,"maxTTL":2592000,"onlyShowInNetwork":false,"maxUploadSize":null,"availableEnvelopeTTL":[0,600,3600,86400,604800,2592000],"maxAutoDownloadSize":7340032}
where do i connect:
"NewValue": {"canStartNewValue1": true, canStartroupValue: true, canVideoCall: true, canStartRoomValue: true, canAddtoValue: true, canStartValueshare: true}
Don't know how to update my putValueForField to reflect this new json and related fields. Any ideas?