Search in sources :

Example 1 with JSONException

use of kong.unirest.json.JSONException in project jabref by JabRef.

the class OpenLibraryFetcher method jsonItemToBibEntry.

private BibEntry jsonItemToBibEntry(JSONObject item) throws ParseException {
    try {
        BibEntry entry = new BibEntry(StandardEntryType.Book);
        String authors = toAuthors(item.optJSONArray("authors"));
        if (authors.isEmpty()) {
            JSONArray works = item.optJSONArray("works");
            authors = fromWorksToAuthors(works);
        }
        entry.setField(StandardField.AUTHOR, authors);
        entry.setField(StandardField.PAGES, item.optString("number_of_pages"));
        entry.setField(StandardField.ISBN, Optional.ofNullable(item.optJSONArray("isbn_13")).map(array -> array.getString(0)).or(() -> Optional.ofNullable(item.optJSONArray("isbn_10")).map(array -> array.getString(0))).orElse(""));
        entry.setField(StandardField.TITLE, Optional.ofNullable(item.optString("full_title", null)).or(() -> Optional.ofNullable(item.optString("title", null))).orElse(""));
        entry.setField(StandardField.SUBTITLE, item.optString("subtitle"));
        Optional<String> yearOpt = Date.parse(item.optString("publish_date")).flatMap(Date::getYear).map(year -> year.toString());
        yearOpt.ifPresent(year -> {
            entry.setField(StandardField.YEAR, year);
        });
        entry.setField(StandardField.PUBLISHER, Optional.ofNullable(item.optJSONArray("publishers")).map(array -> array.getString(0)).orElse(""));
        return entry;
    } catch (JSONException exception) {
        throw new ParseException("CrossRef API JSON format has changed", exception);
    }
}
Also used : IntStream(java.util.stream.IntStream) URL(java.net.URL) StringUtil(org.jabref.model.strings.StringUtil) URISyntaxException(java.net.URISyntaxException) ImportFormatPreferences(org.jabref.logic.importer.ImportFormatPreferences) LoggerFactory(org.slf4j.LoggerFactory) JsonReader(org.jabref.logic.importer.util.JsonReader) JsonNode(kong.unirest.JsonNode) Author(org.jabref.model.entry.Author) StandardField(org.jabref.model.entry.field.StandardField) StandardEntryType(org.jabref.model.entry.types.StandardEntryType) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) URIBuilder(org.apache.http.client.utils.URIBuilder) FetcherException(org.jabref.logic.importer.FetcherException) BibEntry(org.jabref.model.entry.BibEntry) Collectors(java.util.stream.Collectors) Date(org.jabref.model.entry.Date) Unirest(kong.unirest.Unirest) JSONArray(kong.unirest.json.JSONArray) Parser(org.jabref.logic.importer.Parser) AuthorListParser(org.jabref.logic.importer.AuthorListParser) ParseException(org.jabref.logic.importer.ParseException) AuthorList(org.jabref.model.entry.AuthorList) List(java.util.List) Stream(java.util.stream.Stream) JSONException(kong.unirest.json.JSONException) JSONObject(kong.unirest.json.JSONObject) Optional(java.util.Optional) Collections(java.util.Collections) BibEntry(org.jabref.model.entry.BibEntry) JSONArray(kong.unirest.json.JSONArray) JSONException(kong.unirest.json.JSONException) ParseException(org.jabref.logic.importer.ParseException)

Example 2 with JSONException

use of kong.unirest.json.JSONException in project jabref by JabRef.

the class SemanticScholar method jsonItemToBibEntry.

/**
 * This is copy-paste from CrossRef, need to be checked.
 *
 * @param item an entry received, needs to be parsed into a BibEntry
 * @return The BibEntry that corresponds to the received object
 * @throws ParseException if the JSONObject could not be parsed
 */
private BibEntry jsonItemToBibEntry(JSONObject item) throws ParseException {
    try {
        BibEntry entry = new BibEntry(StandardEntryType.Article);
        entry.setField(StandardField.URL, item.optString("url"));
        entry.setField(StandardField.TITLE, item.optString("title"));
        entry.setField(StandardField.ABSTRACT, item.optString("abstract"));
        entry.setField(StandardField.VENUE, item.optString("venue"));
        entry.setField(StandardField.YEAR, item.optString("year"));
        entry.setField(StandardField.AUTHOR, IntStream.range(0, item.optJSONArray("authors").length()).mapToObj(item.optJSONArray("authors")::getJSONObject).map((author) -> author.has("name") ? author.getString("name") : "").collect(Collectors.joining(" and ")));
        JSONObject externalIds = item.optJSONObject("externalIds");
        entry.setField(StandardField.DOI, externalIds.optString("DOI"));
        if (externalIds.has("ArXiv")) {
            entry.setField(StandardField.EPRINT, externalIds.getString("ArXiv"));
            entry.setField(StandardField.EPRINTTYPE, "arXiv");
        }
        entry.setField(StandardField.PMID, externalIds.optString("PubMed"));
        return entry;
    } catch (JSONException exception) {
        throw new ParseException("SemanticScholar API JSON format has changed", exception);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) JSONObject(kong.unirest.json.JSONObject) JSONException(kong.unirest.json.JSONException) ParseException(org.jabref.logic.importer.ParseException)

Example 3 with JSONException

use of kong.unirest.json.JSONException in project jabref by JabRef.

the class DoiFetcher method getAgency.

/**
 * Returns registration agency. Optional.empty() if no agency is found.
 *
 * @param doi the DOI to be searched
 */
public Optional<String> getAgency(DOI doi) throws IOException {
    Optional<String> agency = Optional.empty();
    try {
        URLDownload download = getUrlDownload(new URL(DOI.AGENCY_RESOLVER + "/" + doi.getDOI()));
        JSONObject response = new JSONArray(download.asString()).getJSONObject(0);
        if (response != null) {
            agency = Optional.ofNullable(response.optString("RA"));
        }
    } catch (JSONException e) {
        LOGGER.error("Cannot parse agency fetcher repsonse to JSON");
        return Optional.empty();
    }
    return agency;
}
Also used : JSONObject(kong.unirest.json.JSONObject) JSONArray(kong.unirest.json.JSONArray) JSONException(kong.unirest.json.JSONException) URLDownload(org.jabref.logic.net.URLDownload) URL(java.net.URL)

Example 4 with JSONException

use of kong.unirest.json.JSONException in project jabref by JabRef.

the class ShortDOIService method makeRequest.

private JSONObject makeRequest(DOI doi) throws ShortDOIServiceException {
    URIBuilder uriBuilder = null;
    URL url = null;
    try {
        uriBuilder = new URIBuilder(BASIC_URL);
        uriBuilder.setPath(uriBuilder.getPath() + doi.getDOI());
        uriBuilder.addParameter("format", "json");
        URI uri = uriBuilder.build();
        url = uri.toURL();
    } catch (URISyntaxException | MalformedURLException e) {
        throw new ShortDOIServiceException("Cannot get short DOI", e);
    }
    URLDownload urlDownload = new URLDownload(url);
    try {
        JSONObject resultAsJSON = JsonReader.toJsonObject(urlDownload.asInputStream());
        if (resultAsJSON.isEmpty()) {
            throw new ShortDOIServiceException("Cannot get short DOI");
        }
        return resultAsJSON;
    } catch (ParseException | IOException | JSONException e) {
        throw new ShortDOIServiceException("Cannot get short DOI", e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) JSONException(kong.unirest.json.JSONException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URLDownload(org.jabref.logic.net.URLDownload) URI(java.net.URI) URL(java.net.URL) URIBuilder(org.apache.http.client.utils.URIBuilder) JSONObject(kong.unirest.json.JSONObject) ParseException(org.jabref.logic.importer.ParseException)

Example 5 with JSONException

use of kong.unirest.json.JSONException in project seleniumRobot by bhecquet.

the class SeleniumRobotServerConnector method createVersion.

/**
 * create version
 * If version name already exists on server, it's id will be returned. Else, a new one will be created
 */
public void createVersion() {
    if (!active) {
        return;
    }
    if (applicationId == null) {
        createApplication();
    }
    try {
        JSONObject versionJson = getJSonResponse(buildPostRequest(url + VERSION_API_URL).field(FIELD_NAME, SeleniumTestsContextManager.getApplicationVersion()).field("application", applicationId.toString()));
        versionId = versionJson.getInt("id");
    } catch (UnirestException | JSONException | SeleniumRobotServerException e) {
        throw new SeleniumRobotServerException("cannot create version", e);
    }
}
Also used : JSONObject(kong.unirest.json.JSONObject) UnirestException(kong.unirest.UnirestException) JSONException(kong.unirest.json.JSONException) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException)

Aggregations

JSONException (kong.unirest.json.JSONException)25 JSONObject (kong.unirest.json.JSONObject)19 UnirestException (kong.unirest.UnirestException)12 SeleniumRobotServerException (com.seleniumtests.customexception.SeleniumRobotServerException)11 JSONArray (kong.unirest.json.JSONArray)8 ArrayList (java.util.ArrayList)7 URL (java.net.URL)6 ParseException (org.jabref.logic.importer.ParseException)6 MalformedURLException (java.net.MalformedURLException)5 FetcherException (org.jabref.logic.importer.FetcherException)5 URLDownload (org.jabref.logic.net.URLDownload)5 BibEntry (org.jabref.model.entry.BibEntry)5 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)4 File (java.io.File)4 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 URIBuilder (org.apache.http.client.utils.URIBuilder)4 Collections (java.util.Collections)3 List (java.util.List)3 Optional (java.util.Optional)3