本文整理汇总了Java中javax.json.Json.createGenerator方法的典型用法代码示例。如果您正苦于以下问题:Java Json.createGenerator方法的具体用法?Java Json.createGenerator怎么用?Java Json.createGenerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.json.Json
的用法示例。
在下文中一共展示了Json.createGenerator方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onCargoInspected
import javax.json.Json; //导入方法依赖的package包/类
public void onCargoInspected(@Observes @CargoInspected Cargo cargo) {
Writer writer = new StringWriter();
try (JsonGenerator generator = Json.createGenerator(writer)) {
generator
.writeStartObject()
.write("trackingId", cargo.getTrackingId().getIdString())
.write("origin", cargo.getOrigin().getName())
.write("destination", cargo.getRouteSpecification().getDestination().getName())
.write("lastKnownLocation", cargo.getDelivery().getLastKnownLocation().getName())
.write("transportStatus", cargo.getDelivery().getTransportStatus().toString())
.writeEnd();
}
String jsonValue = writer.toString();
for (Session session : sessions) {
try {
session.getBasicRemote().sendText(jsonValue);
} catch (IOException ex) {
logger.log(Level.WARNING, "Unable to publish WebSocket message", ex);
}
}
}
示例2: jsonStudentList
import javax.json.Json; //导入方法依赖的package包/类
private static String jsonStudentList(List<Student> students) {
StringWriter swriter = new StringWriter();
try (JsonGenerator gen = Json.createGenerator(swriter)) {
gen.writeStartObject();
gen.write("type", "list");
gen.writeStartArray("body");
for (Student student : students) {
gen.writeStartObject();
gen.write("name", student.getName());
gen.write("status", student.getStatus().toString());
gen.writeEnd();
}
gen.writeEnd();
gen.writeEnd();
}
return swriter.toString();
}
示例3: writeJsonStreamToString
import javax.json.Json; //导入方法依赖的package包/类
public String writeJsonStreamToString(){
StringWriter stringWriter = new StringWriter();
JsonGenerator generator = Json.createGenerator(stringWriter);
generator.writeStartObject()
.write("title","JSON-Processing With Java EE")
.writeStartArray("chapters")
.write("Introduction")
.write("1. JSON and Java")
.write("2. JSON-Processing API features")
.write("3. The Java EE JSON Object model")
.write("4. The Java EE JSON Streaming model")
.write("Conclusion")
.writeEnd()
.write("released",JsonValue.TRUE)
.write("length",90)
.writeStartObject("sourceCode")
.write("repositoryName", "JSON-Processing-with-Java-EE")
.write("url","github.com/readlearncode")
.writeEnd()
.writeStartArray("complementaryCourse")
.writeStartObject()
.write("title", "RESTful Service with JAX-RS 2.0")
.write("length", 120)
.writeEnd()
.writeStartObject()
.write("title", "Java Enterprise Edition Introduction")
.write("length", 130)
.writeEnd()
.writeEnd()
.write("notes", JsonValue.NULL)
.writeEnd();
generator.close();
return stringWriter.toString();
}
示例4: jsonStudentUpdate
import javax.json.Json; //导入方法依赖的package包/类
private static String jsonStudentUpdate(Student student) {
StringWriter swriter = new StringWriter();
try (JsonGenerator gen = Json.createGenerator(swriter)) {
gen.writeStartObject();
gen.write("type", "update");
gen.writeStartObject("body");
gen.write("name", student.getName());
gen.write("status", student.getStatus().toString());
gen.writeEnd();
gen.writeEnd();
}
return swriter.toString();
}
示例5: main
import javax.json.Json; //导入方法依赖的package包/类
public static void main(String... args) throws IOException {
File file = new File("json-output.json");
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
if (!file.exists()) {
file.createNewFile();
}
JsonGenerator generator = Json.createGenerator(fileOutputStream);
generator.writeStartObject()
.write("title","JSON-Processing With Java EE")
.writeStartArray("chapters")
.write("Introduction")
.write("1. JSON and Java")
.write("2. JSON-Processing API features")
.write("3. The Java EE JSON Object model")
.write("4. The Java EE JSON Streaming model")
.write("Conclusion")
.writeEnd()
.write("released",JsonValue.TRUE)
.write("length",90)
.writeStartObject("sourceCode")
.write("repositoryName", "JSON-Processing-with-Java-EE")
.write("url","github.com/readlearncode")
.writeEnd()
.writeStartArray("complementaryCourse")
.writeStartObject()
.write("title", "RESTful Service with JAX-RS 2.0")
.write("length", 120)
.writeEnd()
.writeStartObject()
.write("title", "Java Enterprise Edition Introduction")
.write("length", 130)
.writeEnd()
.writeEnd()
.write("notes", JsonValue.NULL)
.writeEnd();
generator.close();
// Flush and close file output streams
fileOutputStream.flush();
fileOutputStream.close();
}
}