Class JSONConfiguration

java.lang.Object
org.omegazero.common.config.JSONConfiguration
All Implemented Interfaces:
Configuration

public class JSONConfiguration extends Object implements Configuration
A Configuration implementation based on JSON.

This class requires the org.json library, which can be obtained here: https://github.com/stleary/JSON-java.

  • Constructor Details

    • JSONConfiguration

      public JSONConfiguration(byte[] fileData)
      Creates a new JSONConfiguration.
      Parameters:
      fileData - A byte array representing a JSON object
      Throws:
      org.json.JSONException - If the given data is not valid JSON
    • JSONConfiguration

      public JSONConfiguration(String fileData)
      Creates a new JSONConfiguration.
      Parameters:
      fileData - A string representing a JSON object
      Throws:
      org.json.JSONException - If the given data is not valid JSON
  • Method Details

    • setUnsupportedField

      protected boolean setUnsupportedField(Field field, Object jsonObject)
      Method that is called when a field with an unsupported type is encountered.
      Parameters:
      field - The field to be set
      jsonObject - The object returned by the JSON library for the key of the field
      Returns:
      true if population was successful, false otherwise
      See Also:
    • load

      public void load() throws IOException
      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, an IllegalArgumentException is thrown.

      Supported field types are ConfigObject, ConfigArray, String, any primitive type and Lists of the mentioned types. If the field type is not supported, the overridable method setUnsupportedField(Field, Object) is called. If this method is not implemented or returns false (the default behavior), an UnsupportedOperationException is thrown.

      Specified by:
      load in interface Configuration
      Throws:
      IOException - If an IO error occurs
    • convertJSONObject

      public static ConfigObject convertJSONObject(org.json.JSONObject json)
      Converts the given JSONObject to a ConfigObject.
      Parameters:
      json - The JSONObject to convert
      Returns:
      The resulting ConfigObject
      Since:
      2.4
    • convertJSONArray

      public static ConfigArray convertJSONArray(org.json.JSONArray json)
      Converts the given JSONArray to a ConfigArray.
      Parameters:
      json - The JSONArray to convert
      Returns:
      The resulting ConfigArray
      Since:
      2.4
    • convertJSONValue

      public static Object convertJSONValue(Object v)
      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: