public String serializeAsJSON() { return asJSON().toString(); }
public static <T> T loadFromJSON(Class<T> targetClass, JSONObject data) throws JSONException { try { return targetClass.cast(doLoadFromJSON(data, targetClass, targetClass.newInstance())); } catch (Exception e) { throw new JSONException(e); } }
public static <T> T loadFromJSON(Class<T> targetClass, String dataStr) throws JSONException { JSONObject data = new JSONObject(dataStr); return loadFromJSON(targetClass, data); } }
public static List<ConnectProject> getAvailableProjectsForRegistration(String login, String password) { String url = getBaseUrl() + GET_PROJECTS_SUFFIX; List<ConnectProject> result = new ArrayList<>(); try (CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(url), getHttpClientContext(url, login, password))) { int rc = httpResponse.getStatusLine().getStatusCode(); if (rc == HttpStatus.SC_OK) { HttpEntity responseEntity = httpResponse.getEntity(); if (responseEntity != null) { String json = EntityUtils.toString(responseEntity); JSONArray array = new JSONArray(json); for (int i = 0; i < array.length(); i++) { JSONObject ob = (JSONObject) array.get(i); result.add(AbstractJSONSerializableData.loadFromJSON(ConnectProject.class, ob)); } } } else { log.error("Unhandled response code: " + rc); } } catch (IOException e) { throw new RuntimeException(e); } catch (JSONException e) { log.debug(e, e); } return result; }
protected static Object doLoadFromJSON(JSONObject data, Class<?> klass, Object instance) throws JSONException { if (klass.getSuperclass() != null) { instance = doLoadFromJSON(data, klass.getSuperclass(), instance); } List<String> fieldNames = new ArrayList<>(); for (Method method : instance.getClass().getDeclaredMethods()) { if (method.getAnnotation(JSONImportMethod.class) != null) { try { String name = method.getAnnotation(JSONImportMethod.class).name(); fieldNames.add(name); Object value = data.get(name); method.invoke(instance, new Object[] { value }); } catch (Exception e) { // NOP } } } for (Field field : instance.getClass().getDeclaredFields()) { if (field.getAnnotation(JSONExportableField.class) != null && (!fieldNames.contains(field.getName()))) { try { field.set(instance, data.get(field.getName())); } catch (Exception e) { // NOP } } } return instance; }
for (int i = 0; i < array.length(); i++) { JSONObject ob = (JSONObject) array.get(i); result.add(AbstractJSONSerializableData.loadFromJSON(PackageDescriptor.class, ob));
for (int i = 0; i < array.length(); i++) { JSONObject ob = (JSONObject) array.get(i); result.add(AbstractJSONSerializableData.loadFromJSON(PackageDescriptor.class, ob));
@Override public SubscriptionStatus getConnectStatus() throws ConnectServerError { String url = getBaseUrl() + GET_STATUS_SUFFIX; ConnectServerResponse response = execCall(url); String json = response.getString(); if (json == null) { throw new ConnectServerError("null response from server"); } try { return AbstractJSONSerializableData.loadFromJSON(SubscriptionStatus.class, json); } catch (Throwable t) { throw new ConnectServerError("Unable to parse response: " + json, t); } finally { response.release(); } }
return null; pkg = AbstractJSONSerializableData.loadFromJSON(PackageDescriptor.class, json); } catch (JSONException e) { throw new ConnectServerError("Unable to parse response", e);