Build and parse Siren hypermedia compliant JSON representations of entities.
val properties = mapOf(
"orderNumber" to 42,
"itemCount" to 3,
"status" to "pending"
)
// Building.
val siren = Root
.newBuilder()
.clazz("order")
.properties(properties)
.links(
Link.newBuilder("self", URI.create("https://example.com/orders/42")).build()
)
.build()
val json = siren.toJson()
// Parsing.
val parsed = Root.fromJson(json)
if (parsed.getProperties() != null) {
parsed.getProperties().get("status")
}
Result:
{
"class": [
"order"
],
"properties": {
"orderNumber": 42,
"itemCount": 3,
"status": "pending"
},
"links": [
{
"rel": [
"self"
],
"href": "https://example.com/orders/42"
}
]
}
See tests for more examples.
Files in src/test/resources
that ends with .snapshot
are generated by
the tests and updated automatically by setting REGENERATE_SNAPSHOTS=true
system property.
Example:
mvn test -DREGENERATE_SNAPSHOTS=true
When a snapshot fails the easiest way to compare the changes are by regenering snapshots and using Git diff.
This project uses ktlint to enforce
code style. Use mvn ktlint:format
to sort most formatting issues if the
build fails.