Search in sources :

Example 71 with JsonObject

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();
}
Also used : QName(javax.xml.namespace.QName) ChildDescriptor(org.glassfish.hk2.xml.internal.ChildDescriptor) JsonObject(jakarta.json.JsonObject) ParentedModel(org.glassfish.hk2.xml.internal.ParentedModel) BaseHK2JAXBBean(org.glassfish.hk2.xml.jaxb.internal.BaseHK2JAXBBean) JsonObject(jakarta.json.JsonObject) LinkedList(java.util.LinkedList) List(java.util.List) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) ModelImpl(org.glassfish.hk2.xml.internal.ModelImpl) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) HashMap(java.util.HashMap) Map(java.util.Map)

Example 72 with JsonObject

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;
}
Also used : HashMap(java.util.HashMap) JsonReader(jakarta.json.JsonReader) JsonObject(jakarta.json.JsonObject)

Example 73 with JsonObject

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.");
    }
}
Also used : Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) IContentId(org.eclipse.dash.licenses.IContentId) ISettings(org.eclipse.dash.licenses.ISettings) StandardCharsets(java.nio.charset.StandardCharsets) Json(jakarta.json.Json) Consumer(java.util.function.Consumer) Inject(javax.inject.Inject) IContentData(org.eclipse.dash.licenses.IContentData) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) URLEncoder(java.net.URLEncoder) StringReader(java.io.StringReader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonObject(jakarta.json.JsonObject) IHttpClientService(org.eclipse.dash.licenses.http.IHttpClientService) JsonReader(jakarta.json.JsonReader) JsonArray(jakarta.json.JsonArray) ILicenseDataProvider(org.eclipse.dash.licenses.ILicenseDataProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringReader(java.io.StringReader) JsonReader(jakarta.json.JsonReader) JsonObject(jakarta.json.JsonObject)

Example 74 with JsonObject

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;
}
Also used : JsonReader(jakarta.json.JsonReader) JsonObject(jakarta.json.JsonObject)

Example 75 with JsonObject

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());
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JsonValue(jakarta.json.JsonValue) JsonReader(jakarta.json.JsonReader) JsonObject(jakarta.json.JsonObject) ClearlyDefinedContentData(org.eclipse.dash.licenses.clearlydefined.ClearlyDefinedContentData) Test(org.junit.jupiter.api.Test)

Aggregations

JsonObject (jakarta.json.JsonObject)356 Test (org.junit.Test)125 Test (org.junit.jupiter.api.Test)92 JsonArray (jakarta.json.JsonArray)29 Response (jakarta.ws.rs.core.Response)29 JsonObjectBuilder (jakarta.json.JsonObjectBuilder)24 JsonString (jakarta.json.JsonString)24 JsonValue (jakarta.json.JsonValue)20 WebClientResponse (io.helidon.webclient.WebClientResponse)19 Client (jakarta.ws.rs.client.Client)18 StringReader (java.io.StringReader)17 HelidonTest (io.helidon.microprofile.tests.junit5.HelidonTest)15 JsonReader (jakarta.json.JsonReader)14 List (java.util.List)14 JsonException (jakarta.json.JsonException)13 Map (java.util.Map)13 Logger (java.util.logging.Logger)13 Routing (io.helidon.webserver.Routing)11 Json (jakarta.json.Json)11 Produces (jakarta.ws.rs.Produces)11