JSON Blob is a helper class to build a JSON String using a simple interface.
val jsonBlob = JsonBlob.obj(
kv("name", "value"),
kv("isOpen", true),
kv("primes", array(2,3,5,7,11)),
kv("address", obj(
kv("streetAddress", "1 Apple Park Way",
kv("city", "Cupertino"))
)),
kv("keys", array(
obj(
kv("key1", "value")
)
)),
kv("empty", unit()),
kv("masked", mask("secret", "***"))
)
val json = jsonBlob.toJson()
val maskedJson = jsonBlob.toJson(true)
Produces:
{"name":"value","isOpen":true,"primes":[2,3,5,7,11],"address":{"streetAddress":"1 Apple Park Way","city":"Cupertino"},"keys":[{"key1":"value"}],"empty":null,"masked":"secret"}
{"name":"value","isOpen":true,"primes":[2,3,5,7,11],"address":{"streetAddress":"1 Apple Park Way","city":"Cupertino"},"keys":[{"key1":"value"}],"empty":null,"masked":"***"}
JSON Blob is available from JCenter as well as Github Packages
repositories {
jcenter()
}
dependencies {
implementation("tech.basepair:json-blob:0.1.0")
}
The JSON Blob API consists of several methods:
- obj(varargs JsonKeyValue): JsonObject
- kv(String, JsonValue): JsonKeyValue
- array(varargs JsonValue): JsonArray
- v(Boolean | Number | String): JsonValue
In Java there are named methods for arrays:
- numArray
- boolArray
- strArray
For each of the methods above there is a supplier version which can be passed to optionally return a value.
val jsonBlob = obj(kv { if (someValue) { kv("b", 1) } else { null } })
...
obj(kv("a", v { if (someValue) { v("hello") } else { null } })
By default the obj()
returned from JsonBlob exposes a toJson(mask: Boolean)
and toJson(printer: Printer)
methods that return the String
representation of the JSON and underlying use a default Printer that utilises a StringBuilder. A custom printer can be passed in.
The masked JSON replaces any content with the masked value supplied and the mask()
method can take any of the JsonBlob types.
For example
val jsonBlob = kv("password", mask(v("goodbye"), "******")
json = jsonBlob.toJson() => "{'password':'goodbye'}"
maskedJson = jsonBlob.toJson(true) => "{'password':'******'}"
A custom printer can be defined that allows outputting the json to an Appendable source. For example a FilePrinter could be used to write the JSON to a file.
class FilePrinter(_printWriter: PrintWriter, override val mask: Boolean) : JsonBlob.Printer {
private val printWriter = _printWriter
override fun append(p0: CharSequence?): Appendable {
printWriter.append(p0)
return this
}
override fun append(p0: CharSequence?, p1: Int, p2: Int): Appendable {
printWriter.append(p0, p1, p2)
return this
}
override fun append(p0: Char): Appendable {
printWriter.append(p0)
return this
}
}
val blob = JsonBlob.obj(kv("a", mask(v("asd"), "***")))
File("path-to-file").printWriter().use { blob.toJson(FilePrinter(it, false)) }
File("path-to-masked-file").printWriter().use { blob.toJson(FilePrinter(it, true)) }
The json-blob artifact is published to jcenter. To publish the following command need to be run:
./gradlew bintrayUpload
This is using the Gradle release plugin https://github.com/researchgate/gradle-release
The release is configured to publish the artifact to Bintray and depends on the bintrayUpload
task.
./gradlew release