Search in sources :

Example 36 with JsonArray

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());
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Test(org.junit.Test)

Example 37 with JsonArray

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));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Instant(java.time.Instant) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 38 with JsonArray

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());
}
Also used : JsonArray(io.vertx.core.json.JsonArray) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 39 with JsonArray

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());
}
Also used : JsonArray(io.vertx.core.json.JsonArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 40 with JsonArray

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();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) BodyCodec(io.vertx.reactivex.ext.web.codec.BodyCodec) MalformedURLException(java.net.MalformedURLException) Observable(io.reactivex.Observable) JsonObject(io.vertx.core.json.JsonObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Collectors(java.util.stream.Collectors) io.nem.sdk.model.transaction(io.nem.sdk.model.transaction) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

JsonArray (io.vertx.core.json.JsonArray)116 JsonObject (io.vertx.core.json.JsonObject)82 Test (org.junit.Test)72 ArrayList (java.util.ArrayList)19 Future (io.vertx.core.Future)15 Buffer (io.vertx.core.buffer.Buffer)15 Handler (io.vertx.core.Handler)14 HttpURLConnection (java.net.HttpURLConnection)13 HashMap (java.util.HashMap)13 TestContext (io.vertx.ext.unit.TestContext)10 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)10 RunWith (org.junit.runner.RunWith)10 StandardCharsets (java.nio.charset.StandardCharsets)9 Instant (java.time.Instant)9 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)9 Vertx (io.vertx.core.Vertx)8 Async (io.vertx.ext.unit.Async)8 Constants (org.eclipse.hono.util.Constants)8 CredentialsObject (org.eclipse.hono.util.CredentialsObject)8 Before (org.junit.Before)8