本文整理汇总了Java中javax.json.JsonObject.toString方法的典型用法代码示例。如果您正苦于以下问题:Java JsonObject.toString方法的具体用法?Java JsonObject.toString怎么用?Java JsonObject.toString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.json.JsonObject
的用法示例。
在下文中一共展示了JsonObject.toString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testLogin
import javax.json.JsonObject; //导入方法依赖的package包/类
@Test
public void testLogin() {
try {
String authUrl = String.format(AUTH_URL_BASE, port);
JsonObject request = Json.createObjectBuilder()
.add("id", DEFAULT_ID)
.add("key", DEFAULT_KEY)
.build();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(request.toString(), headers);
RestTemplate rest = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
ResponseEntity<String> response = rest.exchange(authUrl, HttpMethod.POST, entity, String.class);
assertEquals(200, response.getStatusCodeValue());
JsonReader reader = Json.createReader(new StringReader(response.getBody()));
JsonObject o = reader.readObject();
String token = o.getString("token");
assertTrue(!StringUtils.isEmpty(token));
logger.info(token);
} catch (Exception e) {
logger.error(e.getMessage(), e);
fail(e.getMessage());
}
}
示例2: testScript
import javax.json.JsonObject; //导入方法依赖的package包/类
@Override
public Object testScript() throws Exception {
String result = "";
webDriver = this.getWebDriverList().get(0);
JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
for (String target: subjectList.keySet()){
subject = target;
this.takeAction();
result = this.testJavaScript();
subjectList.replace(target,result);
jsonObjectBuilder.add(target, result);
}
JsonObject payload = jsonObjectBuilder.build();
if (logger.isInfoEnabled())
logger.info(payload.toString());
return payload.toString();
}
示例3: testGetGroupInfo
import javax.json.JsonObject; //导入方法依赖的package包/类
/**
* Add a new group object to the database. Call GET using the id of the new mongo object. Verify
* group info returned matches the group info in the database
*/
@Test
public void testGetGroupInfo() throws Exception {
System.out.println("\nStarting testGetGroupInfo");
// Create group in database
Group group = new Group(null, "testGroup", new String[] {"12345"});
BasicDBObject dbGroup = group.getDBObject(false);
db.getCollection(Group.DB_COLLECTION_NAME).insert(dbGroup);
group.setId(dbGroup.getObjectId(Group.DB_ID).toString());
// Make GET call with group id
String url = groupServiceURL + "/" + dbGroup.getObjectId(Group.DB_ID);
JsonObject response = makeConnection("GET", url, null, 200);
Group returnedGroup = new Group(response.toString());
// Verify the returned group info is correct
assertTrue(
"Group info returned does not match the group info in the database ",
group.isEqual(returnedGroup));
}
示例4: testLoginUpperCaseUUIDs
import javax.json.JsonObject; //导入方法依赖的package包/类
@Test
public void testLoginUpperCaseUUIDs() {
try {
String authUrl = String.format(AUTH_URL_BASE, port);
JsonObject request = Json.createObjectBuilder()
.add("id", DEFAULT_ID_UPPER)
.add("key", DEFAULT_KEY_UPPER)
.build();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(request.toString(), headers);
RestTemplate rest = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
ResponseEntity<String> response = rest.exchange(authUrl, HttpMethod.POST, entity, String.class);
assertEquals(200, response.getStatusCodeValue());
JsonReader reader = Json.createReader(new StringReader(response.getBody()));
JsonObject o = reader.readObject();
String token = o.getString("token");
assertTrue(!StringUtils.isEmpty(token));
logger.info(token);
} catch (Exception e) {
logger.error(e.getMessage(), e);
fail(e.getMessage());
}
}
示例5: getJson
import javax.json.JsonObject; //导入方法依赖的package包/类
/**
* Retrieves representation of an instance of io.rerum.messages.MessagesResource
* @return an instance of java.lang.String
* @throws java.lang.Exception
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getJson() throws Exception {
JsonObject messages = getMessages();
return messages.toString();
}
示例6: postJson
import javax.json.JsonObject; //导入方法依赖的package包/类
/**
*
* @param content
* @return
*/
@POST
@Consumes(javax.ws.rs.core.MediaType.APPLICATION_JSON)
@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
public String postJson(String content) {
JsonObject announcement;
try (JsonReader reader = Json.createReader(new StringReader(content))) {
announcement = reader.readObject();
}
// Simple check for parts
Logger.getLogger(Message.class.getName()).log(Level.INFO, announcement.toString());
// ERRORS
if (announcement.containsKey("@id")) {
// There should not be one already; these are new annoucements.
Response.status(HttpURLConnection.HTTP_BAD_REQUEST).entity("POST is for new annoucements only.").build();
return "{\"error\":\"Property '@id' indicates this is not a new annoucement.\"}";
}
if (!announcement.containsKey("motivation")) {
// This should always exist.
Response.status(HttpURLConnection.HTTP_BAD_REQUEST).entity("Missing 'motivation' property.").build();
return "{\"error\":\"Annoucements without 'motivation' are not allowed on this server.\"}";
}
//NORMALIZATIONS
if (!announcement.containsKey("@context")) {
// Put in a default, please.
announcement = new Message().generate(announcement, "@context", CONTEXT);
}
// add timestamp
Date time = new Date();
announcement = new Message().generate(announcement, "published", time.toString());
try {
announcement = postMessage(announcement);
} catch (Exception ex) {
Logger.getLogger(Message.class.getName()).log(Level.SEVERE, null, ex);
}
return announcement.toString();
}
示例7: buildLspNotification
import javax.json.JsonObject; //导入方法依赖的package包/类
private String buildLspNotification(int type, List<String> artifacts) {
JsonArrayBuilder changes = Json.createArrayBuilder();
for (String sUrl : artifacts) {
changes.add(Json.createObjectBuilder().add("uri", "file://" + sUrl).add("type", type).build());
}
JsonObject bodyObj = Json.createObjectBuilder()
.add("jsonrpc", "2.0")
.add("method", "workspace/didChangeWatchedFiles")
.add("params", Json.createObjectBuilder()
.add("changes", changes.build())
.build())
.build();
String body = bodyObj.toString();
return String.format("Content-Length: %d\r\n\r\n%s", body.length(), body);
}
示例8: toJsonString
import javax.json.JsonObject; //导入方法依赖的package包/类
public String toJsonString(){
JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder()
.add("connectionState",this.getState())
.add("numberOfParticipants", this.getHeadCount());
JsonObject result = jsonObjectBuilder.build();
return result.toString();
}
示例9: toOrgJson
import javax.json.JsonObject; //导入方法依赖的package包/类
/** Converts a {@link JsonObject} to a {@link JSONObject}. */
public static JSONObject toOrgJson(JsonObject json) {
try {
return new JSONObject(json.toString());
} catch (JSONException e) {
// Should never happen, because we start from a valid JSON object.
throw new VerifyException(e);
}
}
示例10: cast
import javax.json.JsonObject; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T cast(JsonObject response) {
JsonObject body = getResponseBody(response);
try {
return castResponseBody(body);
} catch (Exception e) {
throw new WebDriverException(e + " " + body.toString());
}
}
示例11: main
import javax.json.JsonObject; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
Properties props = new Properties();
props.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
// props.put(ACKS_CONFIG, "all");
// props.put(RETRIES_CONFIG, 0);
// props.put(BATCH_SIZE_CONFIG, 32000);
// props.put(LINGER_MS_CONFIG, 100);
// props.put(BUFFER_MEMORY_CONFIG, 33554432);
props.put(KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
props.put(VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
JsonObject json = Json.createObjectBuilder()
.add("windrad", 6)
.add("kw/h",33)
.build();
String msg= json.toString();
for(int i = 1; i <= 10; i++) {
String key = String.valueOf(round(random() * 1000));
double value = new Double(round(random()*10000000L)).intValue()/1000.0;
producer.send(new ProducerRecord<>("produktion", key,msg ));
}
System.out.println("fertig!");
producer.close();
}
示例12: main
import javax.json.JsonObject; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
Properties props = new Properties();
props.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ACKS_CONFIG, "all");
props.put(RETRIES_CONFIG, 0);
props.put(BATCH_SIZE_CONFIG, 16000);
props.put(LINGER_MS_CONFIG, 100);
props.put(BUFFER_MEMORY_CONFIG, 33554432);
props.put(KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
props.put(VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
long t1 = System.currentTimeMillis();
int i = 0;
for(; i < 10; i++) {
String key = String.valueOf(round(random() * 1000));
double value = new Double(round(random()*10000000L)).intValue()/1000.0;
JsonObject json = Json.createObjectBuilder()
.add("windrad", key)
.add("kw",value)
.build();
ProducerRecord<String, String> record = new ProducerRecord<>("produktion", key, json.toString());
producer.send(record);
}
System.out.println("fertig " + i + " Nachrichten in " + (System.currentTimeMillis() - t1 + " ms"));
producer.close();
}
示例13: main
import javax.json.JsonObject; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
Properties props = new Properties();
props.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ACKS_CONFIG, "all");
props.put(RETRIES_CONFIG, 0);
props.put(BATCH_SIZE_CONFIG, 0);
props.put(LINGER_MS_CONFIG, 0);
props.put(BUFFER_MEMORY_CONFIG, 33554432);
props.put(KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
props.put(VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
JsonObject json = Json.createObjectBuilder()
.add("windrad", 6)
.add("kw/h",33)
.build();
String msg= json.toString();
long t1 = System.currentTimeMillis();
for(int i = 1; i <= 10; i++) {
String key = String.valueOf(round(random() * 1000));
double value = new Double(round(random()*10000000L)).intValue()/1000.0;
producer.send(new ProducerRecord<>("produktion", key,msg ));
}
System.out.println("Zeit: " + (System.currentTimeMillis() - t1)/1000 + "s");
producer.close();
}
示例14: testPost
import javax.json.JsonObject; //导入方法依赖的package包/类
private void testPost(JsonPClient client, String clientType) {
reset();
stubFor(post(urlEqualTo("/")).willReturn(aResponse().withStatus(200)));
JsonObject jsonObject = Json.createObjectBuilder().add("someKey", "newValue").build();
String jsonObjectAsString = jsonObject.toString();
Response response = client.post(jsonObject);
response.close();
assertEquals(response.getStatus(), 200, "Expected a 200 OK on client "+clientType);
verify(1, postRequestedFor(urlEqualTo("/")).withRequestBody(equalTo(jsonObjectAsString)));
}
示例15: testPut
import javax.json.JsonObject; //导入方法依赖的package包/类
private void testPut(JsonPClient client, String clientType) {
reset();
stubFor(put(urlEqualTo("/id")).willReturn(aResponse().withStatus(200).withBody("{\"someOtherKey\":\"newValue\"}")));
JsonObject jsonObject = Json.createObjectBuilder().add("someKey", "newValue").build();
String jsonObjectAsString = jsonObject.toString();
JsonObject response = client.update("id", jsonObject);
assertEquals(response.getString("someOtherKey"), "newValue",
"The value of 'someOtherKey' on response should be 'someOtherKey' in client "+clientType);
verify(1, putRequestedFor(urlEqualTo("/id")).withRequestBody(equalTo(jsonObjectAsString)));
}