Java Code Examples for javax.json.JsonWriter#writeObject()

The following examples show how to use javax.json.JsonWriter#writeObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HFCAClient.java    From fabric-sdk-java with Apache License 2.0 8 votes vote down vote up
String toJson(JsonObject toJsonFunc) {
    StringWriter stringWriter = new StringWriter();
    JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter));
    jsonWriter.writeObject(toJsonFunc);
    jsonWriter.close();
    return stringWriter.toString();
}
 
Example 2
Source File: JavaxJsonpTest.java    From thorntail with Apache License 2.0 6 votes vote down vote up
@Test
@RunAsClient
public void testJavaxJsonp() throws IOException {
    JsonObject jsonOut = Json.createObjectBuilder()
            .add("greeting", "hi")
            .build();
    StringWriter stringWriter = new StringWriter(); 
    JsonWriter jsonWriter = Json.createWriter(stringWriter);
    jsonWriter.writeObject(jsonOut);

    String response = Request.Post("http://localhost:8080/jsonp").body(new StringEntity(stringWriter.toString()))
        .execute().returnContent().asString().trim();
    JsonReader jsonReader = Json.createReader(new StringReader(response));
    JsonObject jsonIn = jsonReader.readObject();

    assertThat(jsonIn.getString("greeting")).isEqualTo("hi");
    assertThat(jsonIn.getString("fromServlet")).isEqualTo("true");
}
 
Example 3
Source File: JsonUtil.java    From hadoop-etl-udfs with MIT License 5 votes vote down vote up
public static String prettyJson(JsonObject obj) {
    Map<String, Boolean> config = new HashMap<>();
    config.put(JsonGenerator.PRETTY_PRINTING, true);
    StringWriter strWriter = new StringWriter();
    PrintWriter pw = new PrintWriter(strWriter);
    JsonWriter jsonWriter = Json.createWriterFactory(config).createWriter(pw);
    jsonWriter.writeObject(obj);
    return strWriter.toString();
}
 
Example 4
Source File: Util.java    From jcypher with Apache License 2.0 5 votes vote down vote up
/**
 * write a JsonObject formatted in a pretty way into a String
 * @param jsonObject
 * @return a String containing the JSON
 */
public static String writePretty(JsonObject jsonObject) {
	JsonWriterFactory factory = createPrettyWriterFactory();
	StringWriter sw = new StringWriter();
	JsonWriter writer = factory.createWriter(sw);
	writer.writeObject(jsonObject);
	String ret = sw.toString();
	writer.close();
	return ret;
}
 
Example 5
Source File: JsonMarshaller.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Override
public void marshal(Object obj, OutputStream out) {
    JsonObject jsonObj = marshal((Event)obj);
    JsonWriter writer = Json.createWriter(out);
    writer.writeObject(jsonObj);
    writer.close();
}
 
Example 6
Source File: EventHandler.java    From flowing-retail-old with Apache License 2.0 5 votes vote down vote up
public String asString(JsonObject jsonObject) {
  StringWriter eventStringWriter = new StringWriter();
  JsonWriter writer = Json.createWriter(eventStringWriter);
  writer.writeObject(jsonObject);
  writer.close();

  return eventStringWriter.toString();
}
 
Example 7
Source File: EventProducer.java    From flowing-retail-old with Apache License 2.0 5 votes vote down vote up
public String asString(JsonObjectBuilder builder) {
  JsonObject jsonObject = builder.build();

  StringWriter eventStringWriter = new StringWriter();
  JsonWriter writer = Json.createWriter(eventStringWriter);
  writer.writeObject(jsonObject);
  writer.close();

  return eventStringWriter.toString();
}
 
Example 8
Source File: JsonpServlet.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    JsonReader jsonReader = Json.createReader(req.getReader());
    JsonObject jsonIn = jsonReader.readObject();
    JsonWriter jsonWriter = Json.createWriter(resp.getWriter());
    JsonObject jsonOut =
        Json.createObjectBuilder()
            .add("greeting", jsonIn.getString("greeting"))
            .add("fromServlet", "true")
            .build();
    jsonWriter.writeObject(jsonOut);
}
 
Example 9
Source File: EmployeeJSONWriter.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) throws FileNotFoundException {

		Employee emp = createEmployee();

		JsonObjectBuilder empBuilder = Json.createObjectBuilder();
		JsonObjectBuilder addressBuilder = Json.createObjectBuilder();
		JsonArrayBuilder phoneNumBuilder = Json.createArrayBuilder();

		for (long phone : emp.getPhoneNumbers()) {
			phoneNumBuilder.add(phone);
		}
		
		addressBuilder.add("street", emp.getAddress().getStreet())
						.add("city", emp.getAddress().getCity())
							.add("zipcode", emp.getAddress().getZipcode());
		
		empBuilder.add("id", emp.getId())
					.add("name", emp.getName())
						.add("permanent", emp.isPermanent())
							.add("role", emp.getRole());
		
		empBuilder.add("phoneNumbers", phoneNumBuilder);
		empBuilder.add("address", addressBuilder);
		
		JsonObject empJsonObject = empBuilder.build();
		
		System.out.println("Employee JSON String\n"+empJsonObject);
		
		//write to file
		OutputStream os = new FileOutputStream("emp.txt");
		JsonWriter jsonWriter = Json.createWriter(os);
		/**
		 * We can get JsonWriter from JsonWriterFactory also
		JsonWriterFactory factory = Json.createWriterFactory(null);
		jsonWriter = factory.createWriter(os);
		*/
		jsonWriter.writeObject(empJsonObject);
		jsonWriter.close();
	}
 
Example 10
Source File: IdemixCredRequest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
public String toJson() {
    StringWriter stringWriter = new StringWriter();
    JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter));
    jsonWriter.writeObject(toJsonObject());
    jsonWriter.close();
    return stringWriter.toString();
}
 
Example 11
Source File: IdemixEnrollmentRequest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
String toJson() {
    StringWriter stringWriter = new StringWriter();
    JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter));
    jsonWriter.writeObject(toJsonObject());
    jsonWriter.close();
    return stringWriter.toString();
}
 
Example 12
Source File: RegistrationRequest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
String toJson() {
    StringWriter stringWriter = new StringWriter();
    JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter));
    jsonWriter.writeObject(toJsonObject());
    jsonWriter.close();
    return stringWriter.toString();
}
 
Example 13
Source File: RevocationRequest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
String toJson() {
    StringWriter stringWriter = new StringWriter();
    JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter));
    jsonWriter.writeObject(this.toJsonObject());
    jsonWriter.close();
    return stringWriter.toString();
}
 
Example 14
Source File: EnrollmentRequest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
String toJson() {
    StringWriter stringWriter = new StringWriter();
    JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter));
    jsonWriter.writeObject(toJsonObject());
    jsonWriter.close();
    return stringWriter.toString();
}
 
Example 15
Source File: SmallRyeHealthReporter.java    From smallrye-health with Apache License 2.0 5 votes vote down vote up
public void reportHealth(OutputStream out, SmallRyeHealth health) {
    if (health.isDown() && HealthLogging.log.isInfoEnabled()) {
        // Log reason, as not reported by container orchestrators, yet container may get killed.
        HealthLogging.log.healthDownStatus(health.getPayload().toString());
    }

    JsonWriterFactory factory = jsonProvider.createWriterFactory(JSON_CONFIG);
    JsonWriter writer = factory.createWriter(out);

    writer.writeObject(health.getPayload());
    writer.close();
}
 
Example 16
Source File: HFCAClient.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
/**
 * Generate certificate revocation list.
 *
 * @param registrar     admin user configured in CA-server
 * @param revokedBefore Restrict certificates returned to revoked before this date if not null.
 * @param revokedAfter  Restrict certificates returned to revoked after this date if not null.
 * @param expireBefore  Restrict certificates returned to expired before this date if not null.
 * @param expireAfter   Restrict certificates returned to expired after this date if not null.
 * @throws InvalidArgumentException
 */

public String generateCRL(User registrar, Date revokedBefore, Date revokedAfter, Date expireBefore, Date expireAfter)
        throws InvalidArgumentException, GenerateCRLException {

    if (cryptoSuite == null) {
        throw new InvalidArgumentException("Crypto primitives not set.");
    }

    if (registrar == null) {
        throw new InvalidArgumentException("registrar is not set");
    }

    try {
        setUpSSL();

        //---------------------------------------
        JsonObjectBuilder factory = Json.createObjectBuilder();
        if (revokedBefore != null) {
            factory.add("revokedBefore", Util.dateToString(revokedBefore));
        }
        if (revokedAfter != null) {
            factory.add("revokedAfter", Util.dateToString(revokedAfter));
        }
        if (expireBefore != null) {
            factory.add("expireBefore", Util.dateToString(expireBefore));
        }
        if (expireAfter != null) {
            factory.add("expireAfter", Util.dateToString(expireAfter));
        }
        if (caName != null) {
            factory.add(HFCAClient.FABRIC_CA_REQPROP, caName);
        }

        JsonObject jsonObject = factory.build();

        StringWriter stringWriter = new StringWriter();
        JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter));
        jsonWriter.writeObject(jsonObject);
        jsonWriter.close();
        String body = stringWriter.toString();

        //---------------------------------------

        // send revoke request
        JsonObject ret = httpPost(url + HFCA_GENCRL, body, registrar);

        return ret.getString("CRL");

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new GenerateCRLException(e.getMessage(), e);
    }
}
 
Example 17
Source File: VTypeToJsonTest.java    From diirt with MIT License 4 votes vote down vote up
public void compareJson(JsonObject json, String text) {
    StringWriter writer = new StringWriter();
    JsonWriter jsonWriter = Json.createWriter(writer);
    jsonWriter.writeObject(json);
    assertThat(writer.toString(), equalTo(text));
}
 
Example 18
Source File: BookMessageBodyWriter.java    From tutorials with MIT License 3 votes vote down vote up
/**
 * Marsahl Book to OutputStream
 *
 * @param book
 * @param type
 * @param genericType
 * @param annotations
 * @param mediaType
 * @param httpHeaders
 * @param entityStream
 * @throws IOException
 * @throws WebApplicationException
 */
@Override
public void writeTo(Book book, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    JsonWriter jsonWriter = Json.createWriter(entityStream);
    JsonObject jsonObject = BookMapper.map(book);
    jsonWriter.writeObject(jsonObject);
    jsonWriter.close();
}
 
Example 19
Source File: BookMessageBodyWriter.java    From tutorials with MIT License 3 votes vote down vote up
/**
 * Marsahl Book to OutputStream
 *
 * @param book
 * @param type
 * @param genericType
 * @param annotations
 * @param mediaType
 * @param httpHeaders
 * @param entityStream
 * @throws IOException
 * @throws WebApplicationException
 */
@Override
public void writeTo(Book book, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    JsonWriter jsonWriter = Json.createWriter(entityStream);
    JsonObject jsonObject = BookMapper.map(book);
    jsonWriter.writeObject(jsonObject);
    jsonWriter.close();
}