JCR Node to JSON inside a DaemonModule

Hi, I’m trying to implement an event listener that triggered on document publish/save/delete/etc… In this event listener I want to get document being published/saved/deleted/etc., serialize it to json and the post the json to an external service.

What I want to know is how I can get a bean representation of a Node (the subject document) so I can serialize it to json using Gson.

I’ve seen an example of how to do this inside the site app where there is a HstRquestContext, like so:

HstRequestContext context = RequestContextProvider.get();
Object documentObject = context.getObjectConverter().getObject(node);

However, from inside a repository managed component (my event listener) there is no HstRequestContext available through which I can do a call of context.getObjectConverter().getObject(node) to get the document bean and then serialize it to json.

How can I convert a JCR Node to a document bean and from there serialize it to json inside a repository managed event listener?

to obtain HstRequestContext you need…drum rolls: a user request.
So, your approach will not work.
You can use something like Hippo JCR POJO Binding Documentation – Introduction
or map nodes to json yourself.

@machak I’m glad you understood my question and thank you for pointing out a solution.

Btw, there’s an alternative to getting the object converter from the request context. Starting from v13, hst is also in the cms webapp. So you can get the object converter from the component manager. You can find how that’s done in this post Get values for ValueListProvider from value list document

@bcanvural That works. Thank you very much.