Java Reference
In-Depth Information
Generating JSON data with the JSON-P
streaming API
When using the JSON-P streaming API, we generate JSON data via the
JsonGenerator class, invoking one or more of its several overloaded write()
methods to add JSON properties and corresponding values to the JSON data.
The following example illustrates how to generate data via the JSON-P
streaming API:
package com.ensode.jsonpstreamingapi;
//imports omitted
public class JsonPStreamingApiBean {
private Person person;
private String jsonStr;
public String generateJson() {
StringWriter stringWriter = new StringWriter();
try (JsonGenerator jsonGenerator
= Json.createGenerator(stringWriter)) {
write("firstName", person.getFirstName()).
write("middleName", person.getMiddleName()).
write("lastName", person.getLastName()).
write("gender", person.getGender()).
write("age", person.getAge()).
return "generated_json";
This example is part of a JSF application, specifically a CDI named bean,
and we are only showing code that is relevant to our discussion.
Search WWH ::

Custom Search