sitebricks

Deserialize post/get params with Sitebricks


In sitebricks, I can easily deserialize a class from params in json format in Sitebricks @Service method like this:

request.read(Person.class).as(Json.class); 

But how do I deserialize a class from get/post params?

I know the Request object has access to the params (request.params()) but it would require more effort.


Solution

  • If the object that I want deserialize is not the service itself, then I would have to inject Json to do the deserialization.

    public class TestPage {
       @Inject Json json;
    
       @Post
       public void post(Request request) {
         String data = request.param("data");
         Person p = json.in(new ByteArrayInputStream(data.getBytes()), Person.class);
    
         ...
       }
    }