use of io.vertx.core.json.JsonArray in project vert.x by eclipse.
the class JsonArrayTest method testCommentsInJson.
// Strict JSON doesn't allow comments but we do so users can add comments to config files etc
@Test
public void testCommentsInJson() {
String jsonWithComments = "// single line comment\n" + "/*\n" + " This is a multi \n" + " line comment\n" + "*/\n" + "[\n" + "// another single line comment this time inside the JSON array itself\n" + " \"foo\", \"bar\" // and a single line comment at end of line \n" + "/*\n" + " This is a another multi \n" + " line comment this time inside the JSON array itself\n" + "*/\n" + "]";
JsonArray json = new JsonArray(jsonWithComments);
assertEquals("[\"foo\",\"bar\"]", json.encode());
}
use of io.vertx.core.json.JsonArray in project vert.x by eclipse.
the class JsonArrayTest method testDecode.
@Test
public void testDecode() {
byte[] bytes = TestUtils.randomByteArray(10);
String strBytes = Base64.getEncoder().encodeToString(bytes);
Instant now = Instant.now();
String strInstant = ISO_INSTANT.format(now);
String json = "[\"foo\",123,1234,1.23,2.34,true,\"" + strBytes + "\",\"" + strInstant + "\",null,{\"foo\":\"bar\"},[\"foo\",123]]";
JsonArray arr = new JsonArray(json);
assertEquals("foo", arr.getString(0));
assertEquals(Integer.valueOf(123), arr.getInteger(1));
assertEquals(Long.valueOf(1234l), arr.getLong(2));
assertEquals(Float.valueOf(1.23f), arr.getFloat(3));
assertEquals(Double.valueOf(2.34d), arr.getDouble(4));
assertEquals(true, arr.getBoolean(5));
assertTrue(TestUtils.byteArraysEqual(bytes, arr.getBinary(6)));
assertEquals(now, arr.getInstant(7));
assertTrue(arr.hasNull(8));
JsonObject obj = arr.getJsonObject(9);
assertEquals("bar", obj.getString("foo"));
JsonArray arr2 = arr.getJsonArray(10);
assertEquals("foo", arr2.getString(0));
assertEquals(Integer.valueOf(123), arr2.getInteger(1));
}
use of io.vertx.core.json.JsonArray in project vert.x by eclipse.
the class JsonArrayTest method testCreateFromListNestedList.
@Test
public void testCreateFromListNestedList() {
List<Object> list = new ArrayList<>();
list.add("foo");
list.add(123);
List<Object> list2 = new ArrayList<>();
list2.add("blah");
list2.add("wibble");
list.add(list2);
JsonArray arr = new JsonArray(list);
assertEquals("foo", arr.getString(0));
assertEquals(Integer.valueOf(123), arr.getInteger(1));
assertSame(list, arr.getList());
JsonArray arr2 = arr.getJsonArray(2);
assertSame(list2, arr2.getList());
}
use of io.vertx.core.json.JsonArray in project nem2-sdk-java by nemtech.
the class TransactionMappingTest method validateTransferTx.
void validateTransferTx(TransferTransaction transaction, JsonObject transactionDTO) {
assertEquals(Address.createFromEncoded(transactionDTO.getJsonObject("transaction").getString("recipient")), transaction.getRecipient());
JsonArray mosaicsDTO = transactionDTO.getJsonObject("transaction").getJsonArray("mosaics");
if (mosaicsDTO != null && mosaicsDTO.size() > 0) {
assertEquals(extractBigInteger(mosaicsDTO.getJsonObject(0).getJsonArray("id")), transaction.getMosaics().get(0).getId().getId());
assertEquals(extractBigInteger(mosaicsDTO.getJsonObject(0).getJsonArray("amount")), transaction.getMosaics().get(0).getAmount());
}
try {
assertEquals(new String(Hex.decode(transactionDTO.getJsonObject("transaction").getJsonObject("message").getString("payload")), "UTF-8"), transaction.getMessage().getPayload());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
assertTrue(transactionDTO.getJsonObject("transaction").getJsonObject("message").getInteger("type") == transaction.getMessage().getType());
}
use of io.vertx.core.json.JsonArray in project nem2-sdk-java by nemtech.
the class TransactionHttp method getTransactions.
@Override
public Observable<List<Transaction>> getTransactions(List<String> transactionHashes) {
JsonObject requestBody = new JsonObject();
requestBody.put("transactionIds", transactionHashes);
return this.client.postAbs(this.url.toString()).as(BodyCodec.jsonArray()).rxSendJson(requestBody).toObservable().map(HttpResponse::body).map(json -> new JsonArray(json.toString()).stream().map(s -> (JsonObject) s).collect(Collectors.toList())).flatMapIterable(item -> item).map(new TransactionMapping()).toList().toObservable();
}
Aggregations