The Juneau serializers will now serialize the contents of Readers and InputStreams directly to the output stream.  They used to just call call the toString() method on these objects (which wasn't particularly useful).

When used in conjunction with swaps, you can now provide low-level serialization of POJOs for any of the non-RDF languages:

@Pojo(swap=MyBeanSwap.class)
   public class MyBean {...}
   
   public class MyBeanSwap extends PojoSwap<MyBean,Object> {
      public Object swap(BeanSession session, MyPojo o) throws Exception {
         MediaType mt = session.getMediaType();
         if (mt.hasSubType("json"))
            return new StringReader("{foo:'bar'}");  // Custom JSON output
         return o; // Otherwise treat as normal bean
      }
   }
   
   // Produces "{foo:'bar'}"
   String json = JsonSerializer.DEFAULT_LAX.toString(new MyBean());


* This doesn't work for RDF because we don't have access to the underlying stream.  Instead, we use an intermediate Jena model.  In this case, the contents of the Reader/InputStream are converted to a literal.

 

This is a stepping stone to supporting FreeMarker template support for POJOs.  Instead of a solution for just HTML, I'm interested in finding a generalized template solution for any language that builds upon the existing Swap API.  

The general idea is that we can define template strings against POJOs using an annotation such as the following:

@Pojo(templates={"freemarker:/MemberAddressRender.div.ftl"})
public class MemberAddress {
}

Or they can be defined on @RestResource and @RestMethod annotations...

@RestMethod(
   templates={
      "MemberAddress=freemarker:/MemberAddressRender.div.ftl"
   }, 
   templateEngines={
      FreeMarkerTemplateEngine.class
   }
)

Engines would be tied to media types.  So for example, the FreeMarkerTemplateEngine would be mapped to the "text/html" media type and thus only be invoked by HtmlSerializer.  The engine would swap out POJOs with either Readers or HTML5 beans.  

 

 

 

  • No labels