io.vertx.core.json.Json.encode()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(6.3k)|赞(0)|评价(0)|浏览(125)

本文整理了Java中io.vertx.core.json.Json.encode()方法的一些代码示例,展示了Json.encode()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Json.encode()方法的具体详情如下:
包路径:io.vertx.core.json.Json
类名称:Json
方法名:encode

Json.encode介绍

[英]Encode a POJO to JSON using the underlying Jackson mapper.
[中]使用底层Jackson映射器将POJO编码为JSON。

代码示例

代码示例来源:origin: eclipse-vertx/vert.x

/**
 * Encode the JSON array to a string
 *
 * @return the string encoding
 */
public String encode() {
 return Json.encode(list);
}

代码示例来源:origin: eclipse-vertx/vert.x

/**
 * Encode this JSON object as a string.
 *
 * @return the string encoding.
 */
public String encode() {
 return Json.encode(map);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void encodeCustomTypeInstant() {
 Instant now = Instant.now();
 String json = Json.encode(now);
 assertNotNull(json);
 // the RFC is one way only
 Instant decoded = Instant.from(ISO_INSTANT.parse(json.substring(1, json.length() - 1)));
 assertEquals(now, decoded);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void encodeCustomTypeBinaryNull() {
 byte[] data = null;
 String json = Json.encode(data);
 assertNotNull(json);
 assertEquals("null", json);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void encodeCustomTypeInstantNull() {
 Instant now = null;
 String json = Json.encode(now);
 assertNotNull(json);
 assertEquals("null", json);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void encodeCustomTypeBinary() {
 byte[] data = new byte[] { 'h', 'e', 'l', 'l', 'o'};
 String json = Json.encode(data);
 assertNotNull(json);
 // base64 encoded hello
 assertEquals("\"aGVsbG8=\"", json);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testGenericDecoding() {
 Pojo original = new Pojo();
 original.value = "test";
 String json = Json.encode(Collections.singletonList(original));
 List<Pojo> correct;
 correct = Json.decodeValue(json, new TypeReference<List<Pojo>>() {});
 assertTrue(((List)correct).get(0) instanceof Pojo);
 assertEquals(original.value, correct.get(0).value);
 // same must apply if instead of string we use a buffer
 correct = Json.decodeValue(Buffer.buffer(json, "UTF8"), new TypeReference<List<Pojo>>() {});
 assertTrue(((List)correct).get(0) instanceof Pojo);
 assertEquals(original.value, correct.get(0).value);
 List incorrect = Json.decodeValue(json, List.class);
 assertFalse(incorrect.get(0) instanceof Pojo);
 assertTrue(incorrect.get(0) instanceof Map);
 assertEquals(original.value, ((Map)(incorrect.get(0))).get("value"));
}

代码示例来源:origin: io.vertx/vertx-core

/**
 * Encode this JSON object as a string.
 *
 * @return the string encoding.
 */
public String encode() {
 return Json.encode(map);
}

代码示例来源:origin: io.vertx/vertx-core

/**
 * Encode the JSON array to a string
 *
 * @return the string encoding
 */
public String encode() {
 return Json.encode(list);
}

代码示例来源:origin: apache/servicecomb-java-chassis

protected void runTask() {
  try {
   InstanceCacheChecker checker = new InstanceCacheChecker(appManager);
   InstanceCacheSummary instanceCacheSummary = checker.check();
   eventBus.post(instanceCacheSummary);

   LOGGER.info("check instance cache, result={}.", Json.encode(instanceCacheSummary));
  } catch (Throwable e) {
   LOGGER.error("failed check instance cache..", e);
  }
 }
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void encodeCustomTypeInstant() {
 Instant now = Instant.now();
 String json = Json.encode(now);
 assertNotNull(json);
 // the RFC is one way only
 Instant decoded = Instant.from(ISO_INSTANT.parse(json.substring(1, json.length() - 1)));
 assertEquals(now, decoded);
}

代码示例来源:origin: apache/servicecomb-java-chassis

private void convertOperation(Operation operation) {
 ProtoMethod protoMethod = new ProtoMethod();
 fillRequestType(operation, protoMethod);
 fillResponseType(operation, protoMethod);
 appendLine(serviceBuilder, "  //%s%s", ProtoConst.ANNOTATION_RPC, Json.encode(protoMethod));
 appendLine(serviceBuilder, "  rpc %s (%s) returns (%s);\n", operation.getOperationId(),
   protoMethod.getArgTypeName(),
   protoMethod.findResponse(Status.OK.getStatusCode()).getTypeName());
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void encodeCustomTypeBinaryNull() {
 byte[] data = null;
 String json = Json.encode(data);
 assertNotNull(json);
 assertEquals("null", json);
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void encodeCustomTypeBinary() {
 byte[] data = new byte[] { 'h', 'e', 'l', 'l', 'o'};
 String json = Json.encode(data);
 assertNotNull(json);
 // base64 encoded hello
 assertEquals("\"aGVsbG8=\"", json);
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void encodeCustomTypeInstantNull() {
 Instant now = null;
 String json = Json.encode(now);
 assertNotNull(json);
 assertEquals("null", json);
}

代码示例来源:origin: apache/servicecomb-java-chassis

Json.encode(swaggerType)));

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testGenericDecoding() {
 Pojo original = new Pojo();
 original.value = "test";
 String json = Json.encode(Collections.singletonList(original));
 List<Pojo> correct;
 correct = Json.decodeValue(json, new TypeReference<List<Pojo>>() {});
 assertTrue(((List)correct).get(0) instanceof Pojo);
 assertEquals(original.value, correct.get(0).value);
 // same must apply if instead of string we use a buffer
 correct = Json.decodeValue(Buffer.buffer(json, "UTF8"), new TypeReference<List<Pojo>>() {});
 assertTrue(((List)correct).get(0) instanceof Pojo);
 assertEquals(original.value, correct.get(0).value);
 List incorrect = Json.decodeValue(json, List.class);
 assertFalse(incorrect.get(0) instanceof Pojo);
 assertTrue(incorrect.get(0) instanceof Map);
 assertEquals(original.value, ((Map)(incorrect.get(0))).get("value"));
}

代码示例来源:origin: apache/servicecomb-java-chassis

String local = Json.encode(microserviceVersions.getPulledInstances());
String remote = Json.encode(remoteInstances);
if (local.equals(remote)) {
 instanceCacheResult.setStatus(Status.NORMAL);

代码示例来源:origin: apiman/apiman

default <T> void writeBody(RoutingContext context, T object) {
  context.response().putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
    .setChunked(true)
    .setStatusCode(HttpResponseStatus.OK.code())
    .end(Json.encode(object), "UTF-8");
}

代码示例来源:origin: PegaSysEng/pantheon

@Test
public void requestWithNetMethodShouldSucceedWhenNetApiIsEnabled() throws Exception {
 service = createJsonRpcHttpServiceWithRpcApis(RpcApis.NET);
 final String id = "123";
 final RequestBody body =
   RequestBody.create(
     JSON,
     "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"net_version\"}");
 try (final Response resp = client.newCall(buildRequest(body)).execute()) {
  assertThat(resp.code()).isEqualTo(200);
 }
}

相关文章