09.04.2018 Views

tornadofx-guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

JSON and REST<br />

}<br />

}<br />

with(json) {<br />

add("id", id)<br />

add("firstName", firstName)<br />

add("lastName", lastName)<br />

add("phones", phones.toJSON())<br />

}<br />

class Phone : JsonModel {<br />

val idProperty = SimpleIntegerProperty()<br />

var id by idProperty<br />

val numberProperty = SimpleStringProperty()<br />

var number by numberProperty<br />

override fun updateModel(json: JsonObject) {<br />

with(json) {<br />

id = int("id")<br />

number = string("number")<br />

}<br />

}<br />

}<br />

override fun toJSON(json: JsonBuilder) {<br />

with(json) {<br />

add("id", id)<br />

add("number", number)<br />

}<br />

}<br />

JsonModel with getters/setters and property() accessor functions to be JavaFX<br />

Property compatible<br />

When you implement JsonModel you also get the copy function, which creates a copy of<br />

your model object.<br />

Tornado FX also comes with special support functions for reading and writing JSON<br />

properties. Please see the bottom of Json.kt for an exhaustive list.<br />

All the JSON retrieval functions accepts a vararg argument for the key in the JSON<br />

document. The first key available in the document will be used to retrieve the value. This<br />

makes it easier to work with slightly inconsistent JSON schemes or can be used as a ternary<br />

to provide a fallback value for example.<br />

Configuring datetime<br />

259

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!