当前位置: 首页>>代码示例>>Java>>正文


Java Json.createGenerator方法代码示例

本文整理汇总了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);
        }
    }

}
 
开发者ID:RETIT,项目名称:continuous-performance-evaluation,代码行数:26,代码来源:RealtimeCargoTrackingService.java

示例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();
    }
 
开发者ID:wwu-pi,项目名称:tap17-muggl-javaee,代码行数:20,代码来源:StatusEndpoint.java

示例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();
    }
 
开发者ID:readlearncode,项目名称:JSON-Processing-with-Java-EE,代码行数:38,代码来源:StreamingExample3.java

示例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();
    }
 
开发者ID:wwu-pi,项目名称:tap17-muggl-javaee,代码行数:16,代码来源:StatusEndpoint.java

示例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();
        }

    }
 
开发者ID:readlearncode,项目名称:JSON-Processing-with-Java-EE,代码行数:48,代码来源:StreamingExample4.java


注:本文中的javax.json.Json.createGenerator方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。