Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Neurogami
Created February 26, 2014 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Neurogami/9223893 to your computer and use it in GitHub Desktop.
Save Neurogami/9223893 to your computer and use it in GitHub Desktop.
import java.util.Map;
import java.util.Iterator;
class Configgy {
String[] configTextLines;
JSONObject json;
Configgy(String configFile) {
try {
configTextLines = getDataLines(loadStrings(configFile));
String line = trim(configTextLines[0]);
if (line.charAt(0) == '{') {
json = loadJSONObject(configFile);
} else {
String jsonString = configToJson(configTextLines);
json = parseJSONObject(jsonString);
}
} catch(Exception e) {
println("Error loading data from '" + configFile + "'");
e.printStackTrace();
}
}
// Return just the lines that have config data
String[] getDataLines(String[] configTextLines) {
String[] dataLines = {};
String line;
for (int i=0; i < configTextLines.length; i++) {
line = trim(configTextLines[i]);
if ( ( line.indexOf("#") != 0 ) && ( line.indexOf(":") > 0 ) ) {
dataLines = append(dataLines, line);
}
}
return dataLines ;
}
// Assumes we have a set of congig lines, each being of the form
// keyName: validJsonExpression
String configToJson(String[] configTextLines) {
String line;
String[] jsonStrings = {"{" };
String[] splitString;
String newJsonLine = "";
for (int i=0; i < configTextLines.length; i++) {
line = trim(configTextLines[i]);
if ( ( line.indexOf("#") != 0 ) && ( line.indexOf(":") > 0 ) ) {
splitString = split(configTextLines[i], ':');
newJsonLine = "\"" + splitString[0] + "\":" + join(subset(splitString,1), ":" ) + ", ";
jsonStrings = append(jsonStrings, newJsonLine);
}
}
jsonStrings = append(jsonStrings, "}" );
return join(jsonStrings, "\n");
}
// Assorted accessor methods
String getValue(String k) { return json.getString(k); }
String getString(String k) { return json.getString(k); }
int getInt(String k) { return json.getInt(k); }
float getFloat(String k) { return json.getFloat(k); }
boolean getBoolean(String k) { return json.getBoolean(k); }
String[] getStrings(String k) {
JSONArray values = json.getJSONArray(k);
String[] strings = new String[values.size()];
for (int i = 0; i < values.size(); i++) {
strings[i] = values.getString(i);
}
return strings;
}
// Assumes the JSON "hashmap" is string:string.
// Might look at adding some ways to get other kinds of maps.
// However, if the client really needs, say, strings and ints then
// the values returned can be casted or otherwise converted.
HashMap<String,String> getHashMap(String k) {
HashMap<String, String> h = new HashMap<String, String>();
JSONObject jo = json.getJSONObject(k);
Iterator keys = jo.keyIterator();
while( keys.hasNext() ) {
String key_name = (String) keys.next();
h.put(key_name, jo.getString(key_name));
}
return h;
}
int[] getInts(String k) {
JSONArray values = json.getJSONArray(k);
int[] ints = new int[values.size()];
for (int i = 0; i < values.size(); i++) {
ints[i] = values.getInt(i);
}
return ints;
}
float[] getFloats(String k) {
JSONArray values = json.getJSONArray(k);
float[] floats = new float[values.size()];
for (int i = 0; i < values.size(); i++) {
floats[i] = values.getFloat(i);
}
return floats;
}
boolean[] getBooleans(String k) {
JSONArray values = json.getJSONArray(k);
boolean[] booleans= new boolean[values.size()];
for (int i = 0; i < values.size(); i++) {
booleans[i] = values.getBoolean(i);
}
return booleans;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment