Package dev.relism.flash.ext.jackson
Class Json
java.lang.Object
dev.relism.flash.ext.jackson.Json
Thread-safe JSON toolbox. Single point of access for all JSON I/O operations
within a Flash application.
Retrieve once at boot time via require(Json.class) inside
onInit(), cache in a private field, and call on the hot path
with zero lookup or allocation overhead:
@Route(method = HttpMethod.POST, path = "/api/items")
public class CreateItemHandler extends RequestHandler {
private Json json;
@Override
protected void onInit() {
json = require(Json.class);
}
public Object handle(Request req, Response res) throws Exception {
CreateItemRequest body = json.body(req, CreateItemRequest.class);
return json.write(res, itemService.create(body));
}
}
The underlying ObjectMapper is shared across all handlers in the same
scope (one instance per app / per child scope). Jackson's ObjectMapper
is fully thread-safe after configuration — no synchronization is needed.
Install via JacksonExtension before calling scan() or
register().
-
Method Summary
Modifier and TypeMethodDescription<T> TDeserializes the full request body into an instance oftype.<T> TDeserializes the request body via the rawInputStream, avoiding the intermediatebyte[]allocation.com.fasterxml.jackson.databind.ObjectMappermapper()Returns the underlyingObjectMapperfor advanced operations (custom serialization, schema generation, etc.) not covered by the methods above.Serializesobjto a JSON string and setsContent-Type: application/jsonon the response.Likewrite(dev.relism.flash.models.Response, java.lang.Object)but applies a Jackson@JsonViewfilter, restricting serialization to fields visible underview.
-
Method Details
-
body
Deserializes the full request body into an instance oftype.Reads
req.body().bytes()in one shot. For streaming bodies usebodyFrom(Request, Class)instead.- Throws:
HttpException- 400 if the body cannot be parsed astypeException
-
bodyFrom
Deserializes the request body via the rawInputStream, avoiding the intermediatebyte[]allocation. Prefer this for large bodies or when allocation budget is tight.- Throws:
HttpException- 400 on parse failureException
-
write
Serializesobjto a JSON string and setsContent-Type: application/jsonon the response.The returned string is used as the response body by the Flash runtime.
- Throws:
Exception
-
writeView
Likewrite(dev.relism.flash.models.Response, java.lang.Object)but applies a Jackson@JsonViewfilter, restricting serialization to fields visible underview.- Throws:
Exception
-
mapper
public com.fasterxml.jackson.databind.ObjectMapper mapper()Returns the underlyingObjectMapperfor advanced operations (custom serialization, schema generation, etc.) not covered by the methods above.
-