Class Json

java.lang.Object
dev.relism.flash.ext.jackson.Json

public final class Json extends Object
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 Details

    • body

      public <T> T body(Request req, Class<T> type) throws Exception
      Deserializes the full request body into an instance of type.

      Reads req.body().bytes() in one shot. For streaming bodies use bodyFrom(Request, Class) instead.

      Throws:
      HttpException - 400 if the body cannot be parsed as type
      Exception
    • bodyFrom

      public <T> T bodyFrom(Request req, Class<T> type) throws Exception
      Deserializes the request body via the raw InputStream, avoiding the intermediate byte[] allocation. Prefer this for large bodies or when allocation budget is tight.
      Throws:
      HttpException - 400 on parse failure
      Exception
    • write

      public String write(Response res, Object obj) throws Exception
      Serializes obj to a JSON string and sets Content-Type: application/json on the response.

      The returned string is used as the response body by the Flash runtime.

      Throws:
      Exception
    • writeView

      public String writeView(Response res, Object obj, Class<?> view) throws Exception
      Like write(dev.relism.flash.models.Response, java.lang.Object) but applies a Jackson @JsonView filter, restricting serialization to fields visible under view.
      Throws:
      Exception
    • mapper

      public com.fasterxml.jackson.databind.ObjectMapper mapper()
      Returns the underlying ObjectMapper for advanced operations (custom serialization, schema generation, etc.) not covered by the methods above.