javax.jsonis not hard, but a bit cumbersome. The TornadoFX JSON support comes in two forms: Enhancements to the
javax.jsonobjects and functions and a specialized REST client that does HTTP as well as automatic conversion between JSON and your domain models.
updateModelis called to convert a JSON object to your domain model. It receives a JSON object from which you can update the properties of your model object.
toJSONis called to convert your model object to a JSON payload. It receives a
JsonBuilderwhere you can set the values of the model object.
JsonModel with getters/setters and property() accessor functions to be JavaFX Property compatible
JsonModelyou also get the
copyfunction, which creates a copy of your model object.
datetime(key)function used to retrieve a
LocalDateTimeobject from JSON will by default expect a value of "Seconds since epoch". If your external webservice expects "Milliseconds since epoch" instead, you can either send
datetime(key, millis = true)or configure it globally by setting
JsonConfig.DefaultDateTimeMillis = true.
JsonBuilderis an abstraction over
javax.json.JsonObjectBuilderthat supports null values. Instead of blowing up, it silently dismisses the missing entry, which enables you to build your JSON object graph more fluently without checking for nulls.
Rest.Engineto support other http client libraries if needed.
initmethod of your App class and include the
org.apache.httpcomponents:httpclientdependency in your project descriptor.
initfunction of your
Appclass is a good place to do it.
CustomerController with loadCustomers call
loadCustomersfunction? First we call
api.get("customers")which will perform the call and return a
Responseobject. We then call
Response.list()which will consume the response and convert it to a
javax.json.JsonArray. Lastly, we call the extension function
JsonArray.toModel()which creates one
JsonObjectin the array and calls
JsonModel.updateModelon it. In this example, the type argument is taken from the function return type, but you could also write the above method like this if you prefer:
toModelfunction is a matter of taste, so choose the syntax you are most comfortable with.
JsonModelthat will be the payload of your request, converted to a JSON string.
toModel()to convert it back into our model object.
Map.queryStringextension value will turn any map into a properly URL encoded query string:
Responsebefore you convert the result to JSON. Make sure you always call
consume()on the response if you don't extract data from it using any of the methods
Extract status code and reason from
response.ok()is shorthand for
response.statusCode == 200.
requestInterceptorof the engine to add custom headers etc to the request. For example, this is how the basic authentication is implemented for the
requestInterceptorand casting the request to
HttpURLRequestbefore yo operate on it.
HTTPUrlConnectionobject above in the same way.
Restclass by subclassing it and configuring each subclass as you wish. Injection of subclasses work seamlessly. Override the
engineproperty if you want to use another engine than the default.
engineProviderof the Rest class. This is what happens when you call
engineProviderreturns a concrete
engineimplementation that is given the current
Restinstance as argument.
Restinstance at any time.
Response.seqvalue which will contain a
RestProgressBarinto a ToolBar or any other parent container: