Search in sources :

Example 6 with JsonWriterFactory

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

the class AbstractApiBeanTest method testMessagesNoJsonObject.

@Test
public void testMessagesNoJsonObject() {
    String message = "myMessage";
    Response response = sut.ok(message);
    JsonReader jsonReader = Json.createReader(new StringReader((String) response.getEntity().toString()));
    JsonObject jsonObject = jsonReader.readObject();
    Map<String, Boolean> config = new HashMap<>();
    config.put(JsonGenerator.PRETTY_PRINTING, true);
    JsonWriterFactory jwf = Json.createWriterFactory(config);
    StringWriter sw = new StringWriter();
    try (JsonWriter jsonWriter = jwf.createWriter(sw)) {
        jsonWriter.writeObject(jsonObject);
    }
    logger.info(sw.toString());
    assertEquals(message, jsonObject.getJsonObject("data").getString("message"));
}
Also used : MockResponse(edu.harvard.iq.dataverse.util.MockResponse) Response(javax.ws.rs.core.Response) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) JsonWriter(javax.json.JsonWriter) JsonWriterFactory(javax.json.JsonWriterFactory) Test(org.junit.Test)

Example 7 with JsonWriterFactory

use of javax.json.JsonWriterFactory in project iaf by ibissource.

the class Misc method jsonPretty.

public static String jsonPretty(String json) {
    StringWriter sw = new StringWriter();
    JsonReader jr = Json.createReader(new StringReader(json));
    JsonObject jobj = jr.readObject();
    Map<String, Object> properties = new HashMap<>(1);
    properties.put(JsonGenerator.PRETTY_PRINTING, true);
    JsonWriterFactory writerFactory = Json.createWriterFactory(properties);
    try (JsonWriter jsonWriter = writerFactory.createWriter(sw)) {
        jsonWriter.writeObject(jobj);
    }
    return sw.toString().trim();
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) INamedObject(nl.nn.adapterframework.core.INamedObject) JsonObject(javax.json.JsonObject) JsonWriter(javax.json.JsonWriter) JsonWriterFactory(javax.json.JsonWriterFactory)

Aggregations

JsonWriterFactory (javax.json.JsonWriterFactory)7 HashMap (java.util.HashMap)6 JsonWriter (javax.json.JsonWriter)6 JsonObject (javax.json.JsonObject)4 JsonReader (javax.json.JsonReader)4 StringWriter (java.io.StringWriter)3 Map (java.util.Map)3 JsonArrayBuilder (javax.json.JsonArrayBuilder)3 JsonObjectBuilder (javax.json.JsonObjectBuilder)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 Writer (java.io.Writer)2 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 Collection (java.util.Collection)2 List (java.util.List)2 Optional.ofNullable (java.util.Optional.ofNullable)2 Collectors (java.util.stream.Collectors)2