/** * Encode the JSON array to a string * * @return the string encoding */ public String encode() { return Json.encode(list); }
/** * Encode this JSON object as a string. * * @return the string encoding. */ public String encode() { return Json.encode(map); }
@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); }
@Test public void encodeCustomTypeBinaryNull() { byte[] data = null; String json = Json.encode(data); assertNotNull(json); assertEquals("null", json); }
@Test public void encodeCustomTypeInstantNull() { Instant now = null; String json = Json.encode(now); assertNotNull(json); assertEquals("null", json); }
@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); }
@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")); }
/** * Encode this JSON object as a string. * * @return the string encoding. */ public String encode() { return Json.encode(map); }
/** * Encode the JSON array to a string * * @return the string encoding */ public String encode() { return Json.encode(list); }
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); } } }
@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); }
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()); }
@Test public void encodeCustomTypeBinaryNull() { byte[] data = null; String json = Json.encode(data); assertNotNull(json); assertEquals("null", json); }
@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); }
@Test public void encodeCustomTypeInstantNull() { Instant now = null; String json = Json.encode(now); assertNotNull(json); assertEquals("null", json); }
Json.encode(swaggerType)));
@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")); }
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"); }
@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); } }