Package io.rxson

Class RxSON

java.lang.Object
io.rxson.RxSON

public final class RxSON
extends Object
Since:
11
Author:
Mohamed Aly Bou Hanane ©2020

An Asynchronous Reactive JSON REST Client to stream any REST resource. RxSON makes it easy for the application to use JSON streamed chunks from the response as soon as they arrive, and rendering you code fastr. It treats the HTTP response as a series of small, useful chunks and map them to Java Objects

An RxSON can be used to send requests and retrieve their response as a reactive event-driven, and asynchronous by using observable sequences. It is useful to read lage jason payload without running out og Memory.

An HttpClient is created through a RxSON.Builder().client(HttpClient) builder. The ReactiveSubscriber determines how to handle the data chunks received through stream and publish them to subscribers as java objects after mapping .

Simple Example


         String serviceURL = "https://think.cs.vt.edu/corgis/datasets/json/airlines/airlines.json";
         HttpRequest req = HttpRequest.newBuilder(URI.create(serviceURL)).GET().build();
         RxSON rxson = new RxSON.Builder().build();

         String jsonPath = "$[*].Airport.Name";
         Flowable<String> airportStream = rxson.create(String.class, req, jsonPath);
         airportStream
             .doOnNext(it -> System.out.println("Received new item: " + it))
             //Just for test
             .toList()
             .blockingGet();
     
     

Detailed Example Create a class Model

public class Airline {
     @Reactive(path = "$[*].Airport")
     private Flowable<JsonNode> result;
     public Flowable<JsonNode> getResult() {
         return result;
     }
     public void setResult(final Flowable<JsonNode> result) {
         this.result = result;
     }
 }
 
 
   RxSON rxrest = new RxSON.Builder().build();
 HttpRequest req = HttpRequest.newBuilder(URI.create("my.service.com/airlines")).GET().build();
 var airlinesStream = rxest.create(Airline.class, req);
         airlinesStream.getResult()
             .observeOn(Schedulers.io())
             .subscribeOn(Schedulers.io())
             .doOnNext(it -> System.out.println("Received a flow item: " + it.get("Name")))
             .subscribe();
             //Just for testing
             //.toList().blockingGet();
       
  • Nested Class Summary

    Nested Classes
    Modifier and Type Class Description
    static class  RxSON.Builder
    Builder for RxSON
  • Method Summary

    Modifier and Type Method Description
    <T> T create​(Class<T> clazz)
    Sends the given request asynchronously, create instance of the class and create a reactive stream for class properties It will use default client with GET and http://localhost:8080/
    <T> T create​(Class<T> clazz, HttpRequest req)
    Sends the given request asynchronously, create instance of the class and create a reactive stream for class properties
    <T> io.reactivex.Flowable<T> create​(Class<T> clazz, HttpRequest req, String jsonPath)
    Sends the given request asynchronously, create instance of the class and create a reactive flowable of T
    <T extends CompletableStream<T>>
    T
    createCompletable​(Class<T> clazz, HttpRequest req)
    Send async request, create instance of the class and create a reactive stream for class properties, as well as getting access to the response
    Optional<HttpClient> getClient()  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      public <T> T create​(Class<T> clazz)
      Sends the given request asynchronously, create instance of the class and create a reactive stream for class properties It will use default client with GET and http://localhost:8080/
      Type Parameters:
      T - the response body type
      Parameters:
      clazz - Target class
      Returns:
      T an instance of the class
    • create

      public <T> T create​(Class<T> clazz, HttpRequest req)
      Sends the given request asynchronously, create instance of the class and create a reactive stream for class properties

      Example: Flowable<Airport> airportStream = rxson.create(Airport.class, req, "$[*].Airport.Name");

      Type Parameters:
      T - the response body type
      Parameters:
      clazz - Target class
      req - HttpRequest to send asynchronously
      Returns:
      T an instance of the class
    • create

      public <T> io.reactivex.Flowable<T> create​(Class<T> clazz, HttpRequest req, String jsonPath)
      Sends the given request asynchronously, create instance of the class and create a reactive flowable of T

      Example: Flowable<String> flow = rxson.create(String.class, req, "$[*].Airport.Name");

      Type Parameters:
      T - A flowable response of clazz
      Parameters:
      clazz - Target class to create a flowable of
      req - HttpRequest to send asynchronously
      jsonPath - The json path to subscribe for
      Returns:
      Flowable a flowable publisher of target type
    • createCompletable

      public <T extends CompletableStream<T>> T createCompletable​(Class<T> clazz, HttpRequest req)
      Send async request, create instance of the class and create a reactive stream for class properties, as well as getting access to the response
      Type Parameters:
      T - The generic type of the class
      Parameters:
      clazz - Target class model that extends CompletableStream
      req - HttpRequest to send asynchronously
      Returns:
      CompletableStream of target type which allows access to the response as CompletableStream.getAsyncResponse()

      Example Example getAsyncResponse() .whenComplete((r, t) -> System.out.println("--- Status code " + r.statusCode()))

    • getClient

      public Optional<HttpClient> getClient()
      Returns:
      An HttpClient used to send requests and retrieve their responses.