@Nullable private static ArrayList<String> getStringOrArray(String name, JsonObject object) { final JsonElement jsonElement = object.get(name); final ArrayList<String> strings = Lists.newArrayList(); if (jsonElement == null) return null; if (jsonElement.isJsonArray()) { final JsonArray array = jsonElement.getAsJsonArray(); for (JsonElement element : array) { strings.add(element.getAsString()); } } else { strings.add(jsonElement.getAsString()); } return strings; }
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); }