use of org.jose4j.json.internal.json_simple.JSONArray in project service-proxy by membrane.
the class GenericJsonParser method parse.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T parse(Class<T> clazz, JSONObject json) {
T obj = null;
try {
obj = clazz.newInstance();
for (Object property : json.entrySet()) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) property;
Method setter = getSetter(clazz, entry.getKey());
Object toSetValue = entry.getValue();
if (isStructured(setter)) {
if (isCollection(setter)) {
JSONArray collection = new JSONArray((Collection) toSetValue);
List<Object> coll = new ArrayList<>();
for (Object o : collection) {
JSONObject jo = new JSONObject((Map) o);
String key = (String) jo.keySet().stream().findFirst().orElseThrow(() -> new RuntimeException("Can't get key from " + jo));
JSONObject unwrapped = new JSONObject((Map<String, Object>) jo.get(key));
Class<?> child = K8sHelperGeneratorAutoGenerated.elementMapping.get(key);
coll.add(parse(child, unwrapped));
}
toSetValue = coll;
} else {
Class<?> childClass = K8sHelperGeneratorAutoGenerated.elementMapping.get(entry.getKey());
toSetValue = parse(childClass, new JSONObject((Map) json.get(entry.getKey())));
}
}
setSetter(obj, setter, toSetValue);
}
} catch (Throwable e) {
e.printStackTrace();
}
return obj;
}
use of org.jose4j.json.internal.json_simple.JSONArray in project open-ecard by ecsec.
the class VersionUpdateLoader method loadVersionUpdateList.
public VersionUpdateList loadVersionUpdateList() throws IllegalArgumentException {
try {
// load proxy if one is available
// make sure it is initialized
ProxySettings.getDefault();
List<Proxy> proxies = ProxySelector.getDefault().select(updateUrl.toURI());
Proxy p = Proxy.NO_PROXY;
for (Proxy next : proxies) {
if (next.type() != Proxy.Type.DIRECT) {
LOG.debug("Found a proxy for the update connection.");
p = next;
break;
}
}
LOG.info("Trying to load version list.");
URLConnection con = updateUrl.openConnection(p);
con.connect();
InputStream in = con.getInputStream();
Reader r = new InputStreamReader(in, StandardCharsets.UTF_8);
JSONObject rootObj = (JSONObject) new JSONParser().parse(r);
// get package specific download page
String downloadPageString = (String) rootObj.get(pkgType + "_download_page");
// access package specific list
JSONArray updatesRaw = (JSONArray) rootObj.get(pkgType);
ArrayList<VersionUpdate> updates = new ArrayList<>();
for (Object ur : updatesRaw) {
try {
VersionUpdate next = VersionUpdate.fromJson((JSONObject) ur);
updates.add(next);
} catch (InvalidUpdateDefinition ex) {
LOG.warn("Invalid version info contained in update list.", ex);
throw new IllegalArgumentException("Invalid version info contained in update list.", ex);
}
}
// make sure the versions are in the correct order
Collections.sort(updates);
VersionUpdateList list = new VersionUpdateList(updates, new URL(downloadPageString));
LOG.info("Successfully retrieved version update list.");
return list;
} catch (IOException ex) {
LOG.error("Failed to retrieve update list from server.", ex);
throw new IllegalArgumentException("Failed to retrieve update list from server.", ex);
} catch (NullPointerException ex) {
LOG.warn("Package type {} not supported in update list.", pkgType);
throw new IllegalArgumentException("Package type " + pkgType + " not supported in update list.", ex);
} catch (URISyntaxException ex) {
String msg = "Failed to convert Update URL to a URI.";
LOG.error(msg, ex);
throw new IllegalArgumentException(msg, ex);
} catch (ParseException ex) {
String msg = "Failed to deserialize JSON data.";
LOG.error(msg, ex);
throw new IllegalArgumentException(msg, ex);
}
}
Aggregations