Search in sources :

Example 11 with JsonParser

use of edu.harvard.iq.dataverse.util.json.JsonParser in project dataverse by IQSS.

the class WorkflowsAdmin method addWorkflow.

@POST
public Response addWorkflow(JsonObject jsonWorkflow) {
    JsonParser jp = new JsonParser();
    try {
        Workflow wf = jp.parseWorkflow(jsonWorkflow);
        Workflow managedWf = workflows.save(wf);
        return created("/admin/workflows/" + managedWf.getId(), json(managedWf));
    } catch (JsonParseException ex) {
        return badRequest("Can't parse Json: " + ex.getMessage());
    }
}
Also used : Workflow(edu.harvard.iq.dataverse.workflow.Workflow) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) JsonParser(edu.harvard.iq.dataverse.util.json.JsonParser) POST(javax.ws.rs.POST)

Example 12 with JsonParser

use of edu.harvard.iq.dataverse.util.json.JsonParser in project dataverse by IQSS.

the class SchemaDotOrgExporterTest method testExportDataset.

/**
 * Test of exportDataset method, of class SchemaDotOrgExporter.
 */
@Test
public void testExportDataset() throws Exception {
    System.out.println("exportDataset");
    File datasetVersionJson = new File("src/test/resources/json/dataset-finch1.json");
    String datasetVersionAsJson = new String(Files.readAllBytes(Paths.get(datasetVersionJson.getAbsolutePath())));
    JsonReader jsonReader1 = Json.createReader(new StringReader(datasetVersionAsJson));
    JsonObject json1 = jsonReader1.readObject();
    JsonParser jsonParser = new JsonParser(datasetFieldTypeSvc, null, null);
    DatasetVersion version = jsonParser.parseDatasetVersion(json1.getJsonObject("datasetVersion"));
    version.setVersionState(DatasetVersion.VersionState.RELEASED);
    SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd");
    Date publicationDate = dateFmt.parse("19551105");
    version.setReleaseTime(publicationDate);
    version.setVersionNumber(1l);
    // TODO: It might be nice to test TermsOfUseAndAccess some day
    version.setTermsOfUseAndAccess(null);
    Dataset dataset = new Dataset();
    dataset.setProtocol("doi");
    dataset.setAuthority("myAuthority");
    dataset.setIdentifier("myIdentifier");
    version.setDataset(dataset);
    Dataverse dataverse = new Dataverse();
    dataverse.setName("LibraScholar");
    dataset.setOwner(dataverse);
    System.setProperty(SITE_URL, "https://librascholar.org");
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    schemaDotOrgExporter.exportDataset(version, json1, byteArrayOutputStream);
    String jsonLd = byteArrayOutputStream.toString();
    System.out.println("schema.org JSON-LD: " + JsonUtil.prettyPrint(jsonLd));
    JsonReader jsonReader2 = Json.createReader(new StringReader(jsonLd));
    JsonObject json2 = jsonReader2.readObject();
    assertEquals("http://schema.org", json2.getString("@context"));
    assertEquals("Dataset", json2.getString("@type"));
    assertEquals("https://doi.org/myAuthority/myIdentifier", json2.getString("identifier"));
    assertEquals("Darwin's Finches", json2.getString("name"));
    assertEquals("Finch, Fiona", json2.getJsonArray("author").getJsonObject(0).getString("name"));
    assertEquals("Birds Inc.", json2.getJsonArray("author").getJsonObject(0).getString("affiliation"));
    assertEquals("1955-11-05", json2.getString("dateModified"));
    assertEquals("1", json2.getString("version"));
    assertEquals("Darwin's finches (also known as the Galápagos finches) are a group of about fifteen species of passerine birds.", json2.getString("description"));
    assertEquals("Medicine, Health and Life Sciences", json2.getJsonArray("keywords").getString(0));
    assertEquals("https://schema.org/version/3.3", json2.getString("schemaVersion"));
    assertEquals("DataCatalog", json2.getJsonObject("includedInDataCatalog").getString("@type"));
    assertEquals("LibraScholar", json2.getJsonObject("includedInDataCatalog").getString("name"));
    assertEquals("https://librascholar.org", json2.getJsonObject("includedInDataCatalog").getString("url"));
    assertEquals("Organization", json2.getJsonObject("provider").getString("@type"));
    assertEquals("Dataverse", json2.getJsonObject("provider").getString("name"));
}
Also used : Dataset(edu.harvard.iq.dataverse.Dataset) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Dataverse(edu.harvard.iq.dataverse.Dataverse) Date(java.util.Date) JsonParser(edu.harvard.iq.dataverse.util.json.JsonParser) Test(org.junit.Test)

Aggregations

JsonParser (edu.harvard.iq.dataverse.util.json.JsonParser)12 StringReader (java.io.StringReader)9 JsonObject (javax.json.JsonObject)9 JsonReader (javax.json.JsonReader)9 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)8 JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)6 Gson (com.google.gson.Gson)5 File (java.io.File)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Test (org.junit.Test)4 Dataset (edu.harvard.iq.dataverse.Dataset)3 GsonBuilder (com.google.gson.GsonBuilder)2 DatasetField (edu.harvard.iq.dataverse.DatasetField)2 DatasetFieldValue (edu.harvard.iq.dataverse.DatasetFieldValue)2 Dataverse (edu.harvard.iq.dataverse.Dataverse)2 ForeignMetadataFormatMapping (edu.harvard.iq.dataverse.ForeignMetadataFormatMapping)2 DatasetDTO (edu.harvard.iq.dataverse.api.dto.DatasetDTO)2 IpGroup (edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup)2 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)2