use of jakarta.json.JsonObject in project glassfish-hk2 by eclipse-ee4j.
the class JsonParser method createJsonObject.
@SuppressWarnings("unchecked")
private JsonObject createJsonObject(BaseHK2JAXBBean bean, JsonObjectBuilder builder) {
if (bean == null) {
return builder.build();
}
ModelImpl model = bean._getModel();
Map<QName, ChildDescriptor> allChildren = model.getAllChildrenDescriptors();
for (Map.Entry<QName, ChildDescriptor> entry : allChildren.entrySet()) {
QName keyNameQName = entry.getKey();
String keyName = keyNameQName.getLocalPart();
if (!bean._isSet(keyName))
continue;
Object value = bean._getProperty(XmlService.DEFAULT_NAMESPACE, keyName);
ParentedModel parented = entry.getValue().getParentedModel();
if (parented != null) {
if (ChildType.DIRECT.equals(parented.getChildType())) {
if (value != null) {
builder = builder.add(keyName, createJsonObject((BaseHK2JAXBBean) value, Json.createObjectBuilder()));
}
} else if (ChildType.LIST.equals(parented.getChildType())) {
List<BaseHK2JAXBBean> list = (List<BaseHK2JAXBBean>) value;
if (list != null && !list.isEmpty()) {
JsonArrayBuilder jsonArray = Json.createArrayBuilder();
for (BaseHK2JAXBBean item : list) {
JsonObjectBuilder childBuilder = Json.createObjectBuilder();
JsonObject obj = createJsonObject(item, childBuilder);
jsonArray.add(obj);
}
builder.add(keyName, jsonArray);
}
} else if (ChildType.ARRAY.equals(parented.getChildType())) {
int length = Array.getLength(value);
if (length > 0) {
JsonArrayBuilder jsonArray = Json.createArrayBuilder();
for (int lcv = 0; lcv < length; lcv++) {
BaseHK2JAXBBean item = (BaseHK2JAXBBean) Array.get(value, lcv);
JsonObjectBuilder childBuilder = Json.createObjectBuilder();
JsonObject obj = createJsonObject(item, childBuilder);
jsonArray.add(obj);
}
builder.add(keyName, jsonArray);
}
} else {
throw new AssertionError("Unknown childType " + parented.getChildType());
}
} else {
if (value == null) {
builder.addNull(keyName);
} else if (value instanceof Integer) {
builder.add(keyName, ((Integer) value).intValue());
} else if (value instanceof Long) {
builder.add(keyName, ((Long) value).longValue());
} else if (value instanceof Boolean) {
builder.add(keyName, ((Boolean) value).booleanValue());
} else {
builder.add(keyName, value.toString());
}
}
}
return builder.build();
}
use of jakarta.json.JsonObject in project dash-licenses by eclipse.
the class LicenseSupport method getApprovedLicenses.
private static Map<String, String> getApprovedLicenses(Reader contentReader) {
JsonReader reader = Json.createReader(contentReader);
JsonObject read = (JsonObject) reader.read();
Map<String, String> licenses = new HashMap<>();
JsonObject approved = read.getJsonObject("approved");
if (approved != null) {
approved.forEach((key, name) -> licenses.put(key.toUpperCase(), name.toString()));
}
// Augment the official list with licenses that are acceptable, but
// not explicitly included in our approved list.
licenses.put("EPL-1.0", "Eclipse Public License, v1.0");
licenses.put("EPL-2.0", "Eclipse Public License, v2.0");
licenses.put("WTFPL", "WTFPL");
licenses.put("MIT-0", "MIT-0");
licenses.put("CC-BY-3.0", "CC-BY-3.0");
licenses.put("CC-BY-4.0", "CC-BY-4.0");
licenses.put("UNLICENSE", "Unlicense");
licenses.put("ARTISTIC-2.0", "Artistic-2.0");
licenses.put("BSD-2-Clause-FreeBSD", "BSD 2-Clause FreeBSD License");
licenses.put("0BSD", "BSD Zero Clause License");
return licenses;
}
use of jakarta.json.JsonObject in project dash-licenses by eclipse.
the class EclipseFoundationSupport method queryLicenseData.
@Override
public void queryLicenseData(Collection<IContentId> ids, Consumer<IContentData> consumer) {
if (ids.isEmpty())
return;
String url = settings.getLicenseCheckUrl();
if (url.isBlank()) {
logger.debug("Bypassing Eclipse Foundation.");
return;
}
logger.info("Querying Eclipse Foundation for license data for {} items.", ids.size());
String form = encodeRequestPayload(ids);
int code = httpClientService.post(url, "application/x-www-form-urlencoded", form, response -> {
AtomicInteger counter = new AtomicInteger();
JsonReader reader = Json.createReader(new StringReader(response));
JsonObject read = (JsonObject) reader.read();
JsonObject approved = read.getJsonObject("approved");
if (approved != null)
approved.forEach((key, each) -> {
FoundationData data = new FoundationData(each.asJsonObject());
logger.debug("EF approved: {} ({}) score: {} {} {}", data.getId(), data.getRule(), data.getScore(), data.getLicense(), data.getAuthority());
consumer.accept(data);
counter.incrementAndGet();
});
JsonObject restricted = read.getJsonObject("restricted");
if (restricted != null)
restricted.forEach((key, each) -> {
FoundationData data = new FoundationData(each.asJsonObject());
logger.debug("EF restricted: {} score: {} {} {}", data.getId(), data.getScore(), data.getLicense(), data.getAuthority());
consumer.accept(data);
counter.incrementAndGet();
});
logger.info("Found {} items.", counter.get());
});
if (code != 200) {
logger.error("Error response from the Eclipse Foundation {}", code);
throw new RuntimeException("Received an error response from the Eclipse Foundation.");
}
}
use of jakarta.json.JsonObject in project dash-licenses by eclipse.
the class JsonUtils method readJson.
public static JsonObject readJson(Reader content) {
JsonReader reader = Json.createReader(content);
JsonObject json = (JsonObject) reader.read();
return json;
}
use of jakarta.json.JsonObject in project dash-licenses by eclipse.
the class ClearlyDefinedContentDataTests method testSingleLicense.
@Test
void testSingleLicense() throws Exception {
InputStream input = this.getClass().getResourceAsStream("/write-1.0.3.json");
JsonReader reader = Json.createReader(new InputStreamReader(input, StandardCharsets.UTF_8));
JsonObject data = ((JsonValue) reader.read()).asJsonObject();
ClearlyDefinedContentData info = new ClearlyDefinedContentData("npm/npmjs/-/write/1.0.3", data);
assertEquals("npm/npmjs/-/write/1.0.3", info.getId().toString());
assertEquals("MIT", info.getLicense());
assertEquals("1.0.3", info.getRevision());
assertArrayEquals(new String[] { "MIT" }, info.discoveredLicenses().toArray(String[]::new));
assertEquals(94, info.getScore());
assertEquals(97, info.getEffectiveScore());
assertEquals("https://clearlydefined.io/definitions/npm/npmjs/-/write/1.0.3", info.getUrl());
assertEquals("https://github.com/jonschlinkert/write/tree/f5397515060bf42f75151fcc3c4722517e4e322a", info.getSourceLocation().getUrl());
assertEquals("https://github.com/jonschlinkert/write/archive/refs/tags/1.0.3.zip", info.getSourceLocation().getDownloadUrl());
assertNull(info.getStatus());
}
Aggregations