/** * Encode the JSON array to a string * * @return the string encoding */ public String encode() { return Json.encode(list); }
private void fromJson(String json) { map = Json.decodeValue(json, Map.class); }
/** * Encode this JSON object a a string, with whitespace to make the object easier to read by a human, or other * sentient organism. * * @return the pretty string encoding. */ public String encodePrettily() { return Json.encodePrettily(map); }
@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")); }
private void addOne(RoutingContext routingContext) { final Whisky whisky = Json.decodeValue(routingContext.getBodyAsString(), Whisky.class); mongo.insert(COLLECTION, whisky.toJson(), r -> routingContext.response() .setStatusCode(201) .putHeader("content-type", "application/json; charset=utf-8") .end(Json.encodePrettily(whisky.setId(r.result())))); }
/** * Encode this JSON object as buffer. * * @return the buffer encoding. */ public Buffer toBuffer() { return Json.encodeToBuffer(map); }
@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")); }
private void putDiscoveryNode(String id, String body, Handler<ExtendedAsyncResult<String>> fut) { logger.debug("Int: putDiscoveryNode: " + id + " " + body); final NodeDescriptor nd = Json.decodeValue(body, NodeDescriptor.class); discoveryManager.updateNode(id, nd, res -> { if (res.failed()) { fut.handle(new Failure<>(res.getType(), res.cause())); return; } final String s = Json.encodePrettily(res.result()); fut.handle(new Success<>(s)); }); }
/** * Encode this JSON object as buffer. * * @return the buffer encoding. */ public Buffer toBuffer() { return Json.encodeToBuffer(list); }
/** * Encode this JSON object as a string. * * @return the string encoding. */ public String encode() { return Json.encode(map); }
private void fromBuffer(Buffer buf) { list = Json.decodeValue(buf, List.class); }
/** * Encode the JSON array prettily as a string * * @return the string encoding */ public String encodePrettily() { return Json.encodePrettily(list); }
@Test public void encodeToBuffer() { Buffer json = Json.encodeToBuffer("Hello World!"); assertNotNull(json); // json strings are always UTF8 assertEquals("\"Hello World!\"", json.toString("UTF-8")); }
@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 fromBuffer(Buffer buf) { map = Json.decodeValue(buf, Map.class); }
/** * Encode this JSON object a a string, with whitespace to make the object easier to read by a human, or other * sentient organism. * * @return the pretty string encoding. */ public String encodePrettily() { return Json.encodePrettily(map); }
/** * Encode this JSON object as buffer. * * @return the buffer encoding. */ public Buffer toBuffer() { return Json.encodeToBuffer(map); }
@Test public void encodeCustomTypeBinaryNull() { byte[] data = null; String json = Json.encode(data); assertNotNull(json); assertEquals("null", json); }
private void fromJson(String json) { list = Json.decodeValue(json, List.class); }
/** * Encode the JSON array prettily as a string * * @return the string encoding */ public String encodePrettily() { return Json.encodePrettily(list); }