Search in sources :

Example 6 with JsonWriter

use of javax.json.JsonWriter in project Payara by payara.

the class InstanceDescriptorImpl method toJsonString.

@Override
public String toJsonString(boolean verbose) {
    StringWriter writer = new StringWriter();
    Map<String, Object> writerConfig = new HashMap<>();
    writerConfig.put(PRETTY_PRINTING, TRUE);
    try (JsonWriter jsonWriter = Json.createWriterFactory(writerConfig).createWriter(writer)) {
        jsonWriter.writeObject(toJsonObject(verbose));
        return writer.toString();
    }
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) JsonWriter(javax.json.JsonWriter)

Example 7 with JsonWriter

use of javax.json.JsonWriter in project quickstart by wildfly.

the class JSONRaceStage method run.

@Override
public void run(Race.Registration registration) throws Exception {
    // 1. build an object with nested structure
    JsonObject jsonObject = Json.createObjectBuilder().add("firstName", "John").add("lastName", "Smith").add("age", 25).add("address", Json.createObjectBuilder().add("streetAddress", "21 2nd Street").add("city", "New York").add("state", "NY").add("postalCode", "10021")).add("phoneNumber", Json.createArrayBuilder().add(Json.createObjectBuilder().add("type", "home").add("number", "212 555-1234")).add(Json.createObjectBuilder().add("type", "fax").add("number", "646 555-4567"))).build();
    // 2. write the object to a string
    StringWriter stringWriter = new StringWriter();
    try (JsonWriter writer = Json.createWriter(stringWriter)) {
        writer.write(jsonObject);
    }
    String toString = stringWriter.toString();
    // 3. read object from the string
    JsonObject fromString = null;
    try (JsonReader jsonReader = Json.createReader(new StringReader(toString))) {
        fromString = jsonReader.readObject();
    }
    // 4. sanity check :)
    if (!jsonObject.equals(fromString)) {
        throw new IllegalStateException("json object read from string does not equal the one built");
    }
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) JsonReader(javax.json.JsonReader) JsonWriter(javax.json.JsonWriter)

Example 8 with JsonWriter

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

the class DOMGeneratorTest method testSimpleObject.

@Test
public void testSimpleObject() throws JSONException {
    JsonObject jsonObject = Json.createObjectBuilder().add("apple", "red").add("banana", "yellow").build();
    StringWriter w = new StringWriter();
    try (JsonWriter writer = Json.createWriter(w)) {
        writer.write(jsonObject);
    }
    JSONAssert.assertEquals("{\"apple\" : \"red\", \"banana\" : \"yellow\" }", w.toString(), JSONCompareMode.STRICT);
}
Also used : StringWriter(java.io.StringWriter) JsonObject(javax.json.JsonObject) JsonWriter(javax.json.JsonWriter) Test(org.junit.Test)

Example 9 with JsonWriter

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

the class DOMGeneratorTest method testEmptyObject.

@Test
public void testEmptyObject() throws JSONException {
    JsonObject jsonObject = Json.createObjectBuilder().build();
    StringWriter w = new StringWriter();
    try (JsonWriter writer = Json.createWriter(w)) {
        writer.write(jsonObject);
    }
    JSONAssert.assertEquals("{}", w.toString(), JSONCompareMode.STRICT);
}
Also used : StringWriter(java.io.StringWriter) JsonObject(javax.json.JsonObject) JsonWriter(javax.json.JsonWriter) Test(org.junit.Test)

Example 10 with JsonWriter

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

the class DOMGeneratorTest method testArray.

@Test
public void testArray() throws JSONException {
    JsonArray jsonArray = Json.createArrayBuilder().add(Json.createObjectBuilder().add("apple", "red")).add(Json.createObjectBuilder().add("banana", "yellow")).build();
    StringWriter w = new StringWriter();
    try (JsonWriter writer = Json.createWriter(w)) {
        writer.write(jsonArray);
    }
    JSONAssert.assertEquals("[{\"apple\":\"red\"},{\"banana\":\"yellow\"}]", w.toString(), JSONCompareMode.STRICT);
}
Also used : JsonArray(javax.json.JsonArray) StringWriter(java.io.StringWriter) JsonWriter(javax.json.JsonWriter) Test(org.junit.Test)

Aggregations

JsonWriter (javax.json.JsonWriter)13 StringWriter (java.io.StringWriter)10 JsonObject (javax.json.JsonObject)7 Test (org.junit.Test)5 JsonArrayBuilder (javax.json.JsonArrayBuilder)3 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 JsonReader (javax.json.JsonReader)2 AttributeReference (com.torodb.core.language.AttributeReference)1 IndexType (com.torodb.core.model.IndexedAttributes.IndexType)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 StringReader (java.io.StringReader)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 JsonArray (javax.json.JsonArray)1