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();
}
}
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");
}
}
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);
}
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);
}
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);
}
Aggregations