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 ConfigArray
convertJSONArray
(org.json.JSONArray json) Converts the givenJSONArray
to aConfigArray
.static ConfigObject
convertJSONObject
(org.json.JSONObject json) Converts the givenJSONObject
to aConfigObject
.static Object
Converts the given, possibly JSON-specific, object v to an abstracted data representation.void
load()
Loads this configuration.protected boolean
setUnsupportedField
(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:
true
if population was successful,false
otherwise- See Also:
-
load
Loads this configuration.All fields with the
ConfigurationOption
annotation 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
ConfigurationOption
annotation 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, anIllegalArgumentException
is thrown.Supported field types are
ConfigObject
,ConfigArray
,String
, any primitive type andList
s 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), anUnsupportedOperationException
is thrown.- Specified by:
load
in interfaceConfiguration
- Throws:
IOException
- If an IO error occurs
-
convertJSONObject
Converts the givenJSONObject
to aConfigObject
.- Parameters:
json
- TheJSONObject
to convert- Returns:
- The resulting
ConfigObject
- Since:
- 2.4
-
convertJSONArray
Converts the givenJSONArray
to aConfigArray
.- Parameters:
json
- TheJSONArray
to 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:
-