Class JSONConfiguration
- All Implemented Interfaces:
Configuration
Configuration implementation based on JSON.
This class requires the org.json library, which can be obtained here: https://github.com/stleary/JSON-java.
-
Constructor Summary
ConstructorsConstructorDescriptionJSONConfiguration(byte[] fileData) Creates a newJSONConfiguration.JSONConfiguration(String fileData) Creates a newJSONConfiguration. -
Method Summary
Modifier and TypeMethodDescriptionstatic ConfigArrayconvertJSONArray(org.json.JSONArray json) Converts the givenJSONArrayto aConfigArray.static ConfigObjectconvertJSONObject(org.json.JSONObject json) Converts the givenJSONObjectto aConfigObject.static ObjectConverts the given, possibly JSON-specific, object v to an abstracted data representation.voidload()Loads this configuration.protected booleansetUnsupportedField(Field field, Object jsonObject) Method that is called when a field with an unsupported type is encountered.
-
Constructor Details
-
JSONConfiguration
public JSONConfiguration(byte[] fileData) Creates a newJSONConfiguration.- Parameters:
fileData- A byte array representing a JSON object- Throws:
org.json.JSONException- If the given data is not valid JSON
-
JSONConfiguration
Creates a newJSONConfiguration.- Parameters:
fileData- A string representing a JSON object- Throws:
org.json.JSONException- If the given data is not valid JSON
-
-
Method Details
-
setUnsupportedField
Method that is called when a field with an unsupported type is encountered.- Parameters:
field- The field to be setjsonObject- The object returned by the JSON library for the key of the field- Returns:
trueif population was successful,falseotherwise- See Also:
-
load
Loads this configuration.All fields with the
ConfigurationOptionannotation will be populated with values from the configuration source.The JSON file that was parsed in the constructor is read and fields that have the
ConfigurationOptionannotation will be populated if the JSON file contains a key with the same name as the field. Additional keys in the JSON file that have no corresponding field are ignored.
If the value of a key is a different type than the field type, anIllegalArgumentExceptionis thrown.Supported field types are
ConfigObject,ConfigArray,String, any primitive type andLists of the mentioned types. If the field type is not supported, the overridable methodsetUnsupportedField(Field, Object)is called. If this method is not implemented or returnsfalse(the default behavior), anUnsupportedOperationExceptionis thrown.- Specified by:
loadin interfaceConfiguration- Throws:
IOException- If an IO error occurs
-
convertJSONObject
Converts the givenJSONObjectto aConfigObject.- Parameters:
json- TheJSONObjectto convert- Returns:
- The resulting
ConfigObject - Since:
- 2.4
-
convertJSONArray
Converts the givenJSONArrayto aConfigArray.- Parameters:
json- TheJSONArrayto convert- Returns:
- The resulting
ConfigArray - Since:
- 2.4
-
convertJSONValue
Converts the given, possibly JSON-specific, object v to an abstracted data representation.- Parameters:
v- The object to convert- Returns:
- The resulting object
- Since:
- 2.4
- See Also:
-