user1052610
user1052610

Reputation: 4719

Working with Vertx JsonArray and RxJava Observables

A vertx JsonObject contains a vertx JsonArray, similar to:

{
 "myArray":[
   {"id":"1", name:"Yael" },
   {"id":"2", name:"Haddasa"}
 ]
}

What is the correct way to create an RxJava Observable using the array, so that the observable will handle each element in the array separately. Have tried the following:

JsonArray vertxJsonArray = h.getJsonArray("myArray");
Observable<Object> observable = Observable.fromArray(vertxJsonArray);
observable.flatMapSingle(s -> {
   ...

But using the above, the array is not split up into separate elements. Thanks

Upvotes: 0

Views: 197

Answers (1)

homerman
homerman

Reputation: 3579

since JsonArray implements Iterable, you can use Observable.fromIterable(), like so:

@Test
public void from_iterable_test() {
    final TestObserver<Object> testObserver = new TestObserver<>();

    final String json = "{\"myArray\": [{\"id\": \"1\", \"name\": \"Yael\"}, {\"id\": \"2\", \"name\": \"Haddasa\"}] }";

    final JsonObject jsonObject = new JsonObject(json);

    final JsonArray jsonArray = jsonObject.getJsonArray("myArray");

    Observable.fromIterable(jsonArray).subscribe(testObserver);

    assertEquals(jsonArray.size(), testObserver.valueCount());
}

Upvotes: 1

Related Questions