Search in sources :

Example 46 with JsonReader

use of javax.json.JsonReader in project dataverse by IQSS.

the class SchemaDotOrgExporter method exportDataset.

@Override
public void exportDataset(DatasetVersion version, JsonObject json, OutputStream outputStream) throws ExportException {
    String jsonLdAsString = version.getJsonLd();
    StringReader stringReader = new StringReader(jsonLdAsString);
    JsonReader jsonReader = Json.createReader(stringReader);
    JsonObject jsonLdJsonObject = jsonReader.readObject();
    try {
        outputStream.write(jsonLdJsonObject.toString().getBytes("UTF8"));
    } catch (IOException ex) {
        logger.info("IOException calling outputStream.write: " + ex);
    }
    try {
        outputStream.flush();
    } catch (IOException ex) {
        logger.info("IOException calling outputStream.flush: " + ex);
    }
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) IOException(java.io.IOException)

Example 47 with JsonReader

use of javax.json.JsonReader in project javaee7-samples by javaee-samples.

the class JsonReaderFromReaderTest method testArray.

@Test
public void testArray() throws JSONException {
    JsonReader jsonReader = Json.createReader(new StringReader("[" + "  { \"apple\":\"red\" }," + "  { \"banana\":\"yellow\" }" + "]"));
    JsonArray jsonArr = jsonReader.readArray();
    assertNotNull(jsonArr);
    assertEquals(2, jsonArr.size());
    JSONAssert.assertEquals("{\"apple\":\"red\"}", jsonArr.get(0).toString(), JSONCompareMode.STRICT);
    JSONAssert.assertEquals("{\"banana\":\"yellow\"}", jsonArr.get(1).toString(), JSONCompareMode.STRICT);
}
Also used : JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) Test(org.junit.Test)

Example 48 with JsonReader

use of javax.json.JsonReader in project javaee7-samples by javaee-samples.

the class JsonReaderFromReaderTest method testEmptyObject.

@Test
public void testEmptyObject() throws JSONException {
    JsonReader jsonReader = Json.createReader(new StringReader("{}"));
    JsonObject json = jsonReader.readObject();
    assertNotNull(json);
    assertTrue(json.isEmpty());
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 49 with JsonReader

use of javax.json.JsonReader in project javaee7-samples by javaee-samples.

the class JsonReaderFromReaderTest method testSimpleObjectWithTwoElements.

@Test
public void testSimpleObjectWithTwoElements() throws JSONException {
    JsonReader jsonReader = Json.createReader(new StringReader("{" + "  \"apple\":\"red\"," + "  \"banana\":\"yellow\"" + "}"));
    JsonObject json = jsonReader.readObject();
    assertNotNull(json);
    assertFalse(json.isEmpty());
    assertTrue(json.containsKey("apple"));
    assertEquals("red", json.getString("apple"));
    assertTrue(json.containsKey("banana"));
    assertEquals("yellow", json.getString("banana"));
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 50 with JsonReader

use of javax.json.JsonReader in project javaee7-samples by javaee-samples.

the class JsonReaderFromStreamTest method testArray.

@Test
public void testArray() throws JSONException {
    JsonReader jsonReader = Json.createReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("/3.json"));
    JsonArray jsonArr = jsonReader.readArray();
    assertNotNull(jsonArr);
    assertEquals(2, jsonArr.size());
    JSONAssert.assertEquals("{\"apple\":\"red\"}", jsonArr.get(0).toString(), JSONCompareMode.STRICT);
    JSONAssert.assertEquals("{\"banana\":\"yellow\"}", jsonArr.get(1).toString(), JSONCompareMode.STRICT);
}
Also used : JsonArray(javax.json.JsonArray) JsonReader(javax.json.JsonReader) Test(org.junit.Test)

Aggregations

JsonReader (javax.json.JsonReader)130 JsonObject (javax.json.JsonObject)110 StringReader (java.io.StringReader)78 Test (org.junit.Test)47 JsonArray (javax.json.JsonArray)44 JsonString (javax.json.JsonString)42 HashMap (java.util.HashMap)21 IOException (java.io.IOException)17 ArrayList (java.util.ArrayList)13 File (java.io.File)10 LinkedHashMap (java.util.LinkedHashMap)10 JsonParser (edu.harvard.iq.dataverse.util.json.JsonParser)9 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)8 ByteArrayInputStream (java.io.ByteArrayInputStream)8 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)8 InputStream (java.io.InputStream)7 Gson (com.google.gson.Gson)6 AsyncCompletionHandler (com.ning.http.client.AsyncCompletionHandler)6 Response (com.ning.http.client.Response)6 JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)5