/** * Get the configurations from the configurations File as a {@link JsonElement} * * @return {@link JsonElement} The configuration tree contained inside the configuration File. If * the File doesn't exist or an error occurred, the default configuration tree is returned */ private JsonElement getConfigurationTreeFromFile() { if (mConfigurationFile.exists()) { // Check, if the configuration File exists on disk JsonParser parser = new JsonParser(); try { JsonElement result = parser.parse(new FileReader(mConfigurationFile)); if (result.isJsonObject()) { // Check if content is a valid JsonObject return result; } } catch (FileNotFoundException e) { e.printStackTrace(); } } return mDefaultConfigurations; // If the configurations File doesn't exist on disk, return the // default configurations }
private static boolean loadConfig( JsonElement parsed, Table<String, String, IConfigPropertyHolder> properties) { if (!parsed.isJsonObject()) return true; Gson gson = new Gson(); JsonObject rootNode = parsed.getAsJsonObject(); boolean missingFields = false; for (Map.Entry<String, Map<String, IConfigPropertyHolder>> e : properties.rowMap().entrySet()) { JsonElement categoryTmp = rootNode.get(e.getKey()); missingFields |= parseCategory(categoryTmp, e.getValue(), gson); } return missingFields; }
/** * Method to recursively convert a Json tree, consisting of {@link JsonElement}s, to a to a * configuration tree, consisting of {@link ConfigurationElement}s * * @param parentConfiguration {@link ConfigurationElement} The parent configuration of the new * configurations added in this method call * @param parentJsonElement {@link JsonElement} The parent of the {@link JsonElement}s which will * be converted to {@link ConfigurationElement}s */ private void recConvertJsonTreeToConfigurationTree( ConfigurationElement parentConfiguration, JsonElement parentJsonElement) { if (parentJsonElement.isJsonObject()) { Set<Map.Entry<String, JsonElement>> childConfigurations = ((JsonObject) parentJsonElement).entrySet(); for (Map.Entry<String, JsonElement> childConfiguration : childConfigurations) { if (childConfiguration.getValue().isJsonObject()) { ConfigurationElement newConfigurationSection = new ConfigurationElement(childConfiguration.getKey()); parentConfiguration.addChildConfiguration(newConfigurationSection); recConvertJsonTreeToConfigurationTree( newConfigurationSection, childConfiguration.getValue()); } else { parentConfiguration.addChildConfiguration( new ConfigurationElement( childConfiguration.getKey(), new JsonConfigurationValue(childConfiguration.getValue()))); } } } }
public Object __parse_response__(String resp) throws RemoteException { JsonParser parser = new JsonParser(); JsonElement main_response = (JsonElement) parser.parse(resp); if (main_response.isJsonArray()) { List<Object> res = new Vector<Object>(); for (Object o : main_response.getAsJsonArray()) { res.add(__parse_response__(gson.toJson(o))); } return res; } JsonObject response = main_response.getAsJsonObject(); if (response.has("error")) { JsonObject e = response.get("error").getAsJsonObject().get("data").getAsJsonObject(); throw new RemoteException(e.get("exception").getAsString(), e.get("message").getAsString()); } JsonElement value = response.get("result"); String valuestr = gson.toJson(response.get("result")); if (valuestr.contains("hash:")) { Class<? extends RpcClient> klass = this.getClass(); try { Constructor<? extends RpcClient> m = klass.getDeclaredConstructor(String.class, String.class); String new_endpoint = gson.fromJson(value, String.class); return m.newInstance(this.base_endpoint, new_endpoint.replace("hash:", "")); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } else if (valuestr.contains("funcs")) { return gson.fromJson(valuestr, Interface.class); } if (value.isJsonPrimitive()) { JsonPrimitive val = value.getAsJsonPrimitive(); if (val.isBoolean()) { return val.getAsBoolean(); } else if (val.isNumber()) { return val.getAsNumber(); } else if (val.isString()) { DateTimeFormatter dtparser = ISODateTimeFormat.dateHourMinuteSecond(); try { return dtparser.parseDateTime(val.getAsString()); } catch (Exception ex) { } return val.getAsString(); } else { return null; } } else if (value.isJsonNull()) { return null; } else if (value.isJsonObject() && !value.getAsJsonObject().has("__meta__")) { return gson.fromJson(value, HashMap.class); } else if (value.isJsonArray()) { if (value.getAsJsonArray().size() == 0) return new LinkedList(); JsonElement obj = value.getAsJsonArray().get(0); if (obj.isJsonObject()) { if (!obj.getAsJsonObject().has("__meta__")) { return gson.fromJson(value, LinkedList.class); } } } return __resolve_references__(valuestr); }