JSON
To manipulate JSON object, Vert.x proposes its own implementation of JsonObject and
JsonArray.
This is because, unlike some other languages, Java does not have first class support for JSON.
There is a helper class (Json) to ease the creation of JsonObject/JsonArray instances.
|
Note
|
Most vert.x methods taking a JSON object as argument in their Java version, take a map instead. |
JSON objects
The JsonObject class represents JSON objects.
A JSON object is basically just a map which has string keys and values can be of one of the JSON supported types (string, number, boolean).
JSON objects also support null values.
Creating JSON objects
Empty JSON objects can be created with the default constructor or using .
You can create a JSON object as follows:
import io.vertx.lang.scala.json.Json
def object = Json.obj(("foo","bar"))
Putting entries into a JSON object
Use the put methods to put values into the JSON object.
The method invocations can be chained because of the fluent API:
import io.vertx.lang.scala.json.Json
val object = Json.emptyObj()
object.put("foo", "bar").put("num", 123).put("mybool", true)
Getting values from a JSON object
You get values from a JSON object using the getXXX methods, for example:
val stringVal = jsonObject.getString("some-key")
val intVal = jsonObject.getInteger("some-other-key")
Encoding the JSON object to a String
You use encode to encode the object to a String form. There is also a
encodePrettily that makes the output pretty (understand multi-line and
indented).
JSON arrays
The JsonArray class represents JSON arrays.
A JSON array is a sequence of values (string, number, boolean).
JSON arrays can also contain null values.
Creating JSON arrays
You can create a JSON array as follows:
import io.vertx.lang.scala.json.Json
val jsonArray = Json.arr("bar", "baz")
Adding entries into a JSON array
You add entries to a JSON array using the add methods.
import io.vertx.lang.scala.json.Json
val jsonArray = Json.emptyArray()
jsonArray.add("foo").add(123).add(false)
Getting values from a JSON array
You get values from a JSON array using the getXXX methods, for example:
val stringVal = array.getString(0)
val intVal = array.getInteger(1)
val boolVal = array.getBoolean(2)
Encoding the JSON array to a String
You use encode to encode the array to a String form. There is also a
encodePrettily that makes the output pretty (understand multi-line and
indented).