Package javax.json.stream

Examples of javax.json.stream.JsonGenerator


        dataset.setValue(Tag.OverlayData, VR.OW, new BulkData(null, "file:/OverlayData", false));
        Fragments frags = dataset.newFragments(Tag.PixelData, VR.OB, 2);
        frags.add(null);
        frags.add(new BulkData(null, "file:/PixelData", false));
        StringWriter writer = new StringWriter();
        JsonGenerator gen = Json.createGenerator(writer);
        new JSONWriter(gen).write(dataset);
        gen.flush();
        assertEquals(RESULT, writer.toString());
    }
View Full Code Here


                Type type1,
                Annotation[] antns,
                MediaType mt,
                MultivaluedMap<String, Object> mm,
                OutputStream out) throws IOException, WebApplicationException {
        JsonGenerator gen = Json.createGenerator(out);
        gen.writeStartObject()
                .write("name", t.getName())
                .write("age", t.getAge())
                .writeEnd();
        gen.flush();
    }
View Full Code Here

    @Test
    public void testEmptyObject() throws JSONException {
        JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
        StringWriter w = new StringWriter();
        JsonGenerator gen = factory.createGenerator(w);
        gen.writeStartObject().writeEnd();
        gen.flush();

        JSONAssert.assertEquals("{}", w.toString(), JSONCompareMode.STRICT);
    }
View Full Code Here

    @Test
    public void testSimpleObject() throws JSONException {
        JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
        StringWriter w = new StringWriter();
        JsonGenerator gen = factory.createGenerator(w);

        gen
                .writeStartObject()
                .write("apple", "red")
                .write("banana", "yellow")
                .writeEnd();
        gen.flush();
        JSONAssert.assertEquals("{\"apple\" : \"red\", \"banana\" : \"yellow\" }", w.toString(), JSONCompareMode.STRICT);
    }
View Full Code Here

    @Test
    public void testArray() throws JSONException {
        JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
        StringWriter w = new StringWriter();
        JsonGenerator gen = factory.createGenerator(w);

        gen
                .writeStartArray()
                .writeStartObject()
                .write("apple", "red")
                .writeEnd()
                .writeStartObject()
                .write("banana", "yellow")
                .writeEnd()
                .writeEnd();
        gen.flush();
        JSONAssert.assertEquals("[{\"apple\":\"red\"},{\"banana\":\"yellow\"}]", w.toString(), JSONCompareMode.STRICT);
    }
View Full Code Here

    @Test
    public void testNestedStructure() throws JSONException {
        JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
        StringWriter w = new StringWriter();
        JsonGenerator gen = factory.createGenerator(w);

        gen
                .writeStartObject()
                .write("title", "The Matrix")
                .write("year", 1999)
                .writeStartArray("cast")
                .write("Keanu Reaves")
                .write("Laurence Fishburne")
                .write("Carrie-Anne Moss")
                .writeEnd()
                .writeEnd();
        gen.flush();
        JSONAssert.assertEquals("{\"title\":\"The Matrix\",\"year\":1999,\"cast\":[\"Keanu Reaves\",\"Laurence Fishburne\",\"Carrie-Anne Moss\"]}", w.toString(), JSONCompareMode.STRICT);
    }
View Full Code Here

    Map<String, Object> properties = new HashMap<String, Object>(1);
    if (myPrettyPrint) {
      properties.put(JsonGenerator.PRETTY_PRINTING, myPrettyPrint);
    }
    JsonGeneratorFactory jgf = Json.createGeneratorFactory(properties);
    JsonGenerator eventWriter = jgf.createGenerator(theWriter);
    return eventWriter;
  }
View Full Code Here

    return eventWriter;
  }

  @Override
  public void encodeBundleToWriter(Bundle theBundle, Writer theWriter) throws IOException {
    JsonGenerator eventWriter = createJsonGenerator(theWriter);
    eventWriter.writeStartObject();

    eventWriter.write("resourceType", "Bundle");

    writeTagWithTextNode(eventWriter, "title", theBundle.getTitle());
    writeTagWithTextNode(eventWriter, "id", theBundle.getBundleId());
    writeOptionalTagWithTextNode(eventWriter, "updated", theBundle.getUpdated());
    writeOptionalTagWithTextNode(eventWriter, "published", theBundle.getPublished());

    boolean linkStarted = false;
    linkStarted = writeAtomLink(eventWriter, "self", theBundle.getLinkSelf(), linkStarted);
    linkStarted = writeAtomLink(eventWriter, "first", theBundle.getLinkFirst(), linkStarted);
    linkStarted = writeAtomLink(eventWriter, "previous", theBundle.getLinkPrevious(), linkStarted);
    linkStarted = writeAtomLink(eventWriter, "next", theBundle.getLinkNext(), linkStarted);
    linkStarted = writeAtomLink(eventWriter, "last", theBundle.getLinkLast(), linkStarted);
    linkStarted = writeAtomLink(eventWriter, "fhir-base", theBundle.getLinkBase(), linkStarted);
    if (linkStarted) {
      eventWriter.writeEnd();
    }

    writeOptionalTagWithTextNode(eventWriter, "totalResults", theBundle.getTotalResults());

    writeAuthor(theBundle, eventWriter);

    eventWriter.writeStartArray("entry");
    for (BundleEntry nextEntry : theBundle.getEntries()) {
      eventWriter.writeStartObject();

      if (nextEntry.getDeletedAt() !=null&&nextEntry.getDeletedAt().isEmpty()==false) {
        writeTagWithTextNode(eventWriter, "deleted", nextEntry.getDeletedAt());
      }
      writeTagWithTextNode(eventWriter, "title", nextEntry.getTitle());
      writeTagWithTextNode(eventWriter, "id", nextEntry.getId());

      linkStarted = false;
      linkStarted = writeAtomLink(eventWriter, "self", nextEntry.getLinkSelf(), linkStarted);
      linkStarted = writeAtomLink(eventWriter, "alternate", nextEntry.getLinkAlternate(), linkStarted);
      if (linkStarted) {
        eventWriter.writeEnd();
      }

      writeOptionalTagWithTextNode(eventWriter, "updated", nextEntry.getUpdated());
      writeOptionalTagWithTextNode(eventWriter, "published", nextEntry.getPublished());

      if (nextEntry.getCategories() != null && nextEntry.getCategories().size() > 0) {
        eventWriter.writeStartArray("category");
        for (Tag next : nextEntry.getCategories()) {
          eventWriter.writeStartObject();
          eventWriter.write("term", defaultString(next.getTerm()));
          eventWriter.write("label", defaultString(next.getLabel()));
          eventWriter.write("scheme", defaultString(next.getScheme()));
          eventWriter.writeEnd();
        }
        eventWriter.writeEnd();
      }

      writeAuthor(nextEntry, eventWriter);

      IResource resource = nextEntry.getResource();
      if (resource != null && !resource.isEmpty()) {
        RuntimeResourceDefinition resDef = myContext.getResourceDefinition(resource);
        encodeResourceToJsonStreamWriter(resDef, resource, eventWriter, "content", false);
      }

      eventWriter.writeEnd(); // entry object
    }
    eventWriter.writeEnd(); // entry array

    eventWriter.writeEnd();
    eventWriter.flush();
  }
View Full Code Here

  @Override
  public void encodeResourceToWriter(IResource theResource, Writer theWriter) throws IOException {
    Validate.notNull(theResource, "Resource can not be null");

    JsonGenerator eventWriter = createJsonGenerator(theWriter);

    RuntimeResourceDefinition resDef = myContext.getResourceDefinition(theResource);
    encodeResourceToJsonStreamWriter(resDef, theResource, eventWriter, null,false);
    eventWriter.flush();
  }
View Full Code Here

    eventWriter.flush();
  }

  @Override
  public void encodeTagListToWriter(TagList theTagList, Writer theWriter) throws IOException {
    JsonGenerator eventWriter = createJsonGenerator(theWriter);

    eventWriter.writeStartObject();

    eventWriter.write("resourceType", TagList.ELEMENT_NAME);

    eventWriter.writeStartArray(TagList.ATTR_CATEGORY);
    for (Tag next : theTagList) {
      eventWriter.writeStartObject();

      if (isNotBlank(next.getTerm())) {
        eventWriter.write(Tag.ATTR_TERM, next.getTerm());
      }
      if (isNotBlank(next.getLabel())) {
        eventWriter.write(Tag.ATTR_LABEL, next.getLabel());
      }
      if (isNotBlank(next.getScheme())) {
        eventWriter.write(Tag.ATTR_SCHEME, next.getScheme());
      }

      eventWriter.writeEnd();
    }
    eventWriter.writeEnd();

    eventWriter.writeEnd();
    eventWriter.flush();
  }
View Full Code Here

TOP

Related Classes of javax.json.stream.JsonGenerator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.