Serialize Pojos to JSON using new standard javax.json

Java API for JSON Processing (JSR-353) does not cover object binding. This will be covered in a separate JSR.


See JSR-367, Java API for JSON Binding (JSON-B), a headline feature in Java™ EE 8.

Document: Json Binding 1.0 Users Guide

// Create Jsonb and serialize
Jsonb jsonb = JsonbBuilder.create();
String result = jsonb.toJson(dog);

// Deserialize back
dog = jsonb.fromJson("{name:\"Falco\", age:4, bitable:false}", Dog.class);

Maybe it's because this question is almost 5 years old (I didn't check which java release has these classes) but there is a standard way with javax.json.* classes:

JsonObject json = Json.createObjectBuilder()
        .add("key", "value")
        .build();
try(JsonWriter writer = Json.createWriter(outputStream)) {
    writer.write(json);
}