Message Boards

Running JSONFactoryUtil in standalone Java App

Kevin Neibarger, modified 4 Years ago.

Running JSONFactoryUtil in standalone Java App

Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts
I understand that the answer to my question is probably "Liferay doesn't support that", but I'll ask anyways.

So, I'm running a Java application to test code which builds a PDF. I use JSON data to build this PDF. When I try to use Liferay's JSON implementation from the portal-services.jar (com.liferay.portal.kernel.json.JSONObject) using the JSONFactoryUtil I get NullpointerExceptions

json = JSONFactoryUtil.createJSONArray(jsonString.toString());

The above line throws the Exception

Exception in thread "main" java.lang.NullPointerException
at com.liferay.portal.kernel.json.JSONFactoryUtil.createJSONObject(JSONFactoryUtil.java:68)
at com.healthmap.portlet.compass.test.TestPDFReportBuilder.readJSONFileIntoJSON(TestPDFReportBuilder.java:60)
at com.healthmap.portlet.compass.test.TestPDFReportBuilder.main(TestPDFReportBuilder.java:29)

Why can't I use Liferay's JSON implementation from the kernel to run a standalone Java App? How would this implementation be bound to a portlet container? I can't use org.json.JSONObject in my portlet so I have to write/re-write code to use the different classes.. This is too much for simply testing something outside the portlet container.. Also, I have portal-service.jar in my classpath so it's there. My code compiles but throws a runtime exception
thumbnail
David H Nebinger, modified 4 Years ago.

RE: Running JSONFactoryUtil in standalone Java App

Liferay Legend Posts: 14916 Join Date: 9/2/06 Recent Posts
Liferay's OSGi mechanisms will typically only work within an OSGi container. Most of the static util classes use an OSGi ServiceTracker to track when the implementation is created and registered.

You're getting an NPE because there was no registration, no instance available for the service tracker to wire in and make available, so when the util class goes to use the instance that it expects to be there, it isn't and you get an NPE.

Instead of using the util class, you should probably instantiate an implementation directly. Note that it will also be your responsibility to satisfy any dependencies it has (and all dependencies the dependencies might have).

In my opinion, if you're outside of a Liferay context, you'll probably be better off leveraging other frameworks outside of Liferay's. GSON or the standard JSON implementations are both good libs that provide similar functionality.
Kevin Neibarger, modified 4 Years ago.

RE: Running JSONFactoryUtil in standalone Java App

Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts
Thanks David, I figured that it would need to be in the OSGI container. Since I'm building my PDF in my portlet there are more steps to test and more complexity there. Hence the reason for wanting to run it outside of that container. 
I have leveraged other frameworks, but then I have to convert all references back to the Liferay implementation when I add to my portlet code. It would simply be more convenient. 
thumbnail
David H Nebinger, modified 4 Years ago.

RE: Running JSONFactoryUtil in standalone Java App

Liferay Legend Posts: 14916 Join Date: 9/2/06 Recent Posts
Well, you can continue to use the outside frameworks, just include them into your bundle.

It is a little known fact that Liferay's JSON implementation is designed to satisfy Liferay's needs for JSON building and processing. It tends to be small and lightweight, but the caveat is that it is designed and optimized really for working with small and lightweight JSON. As soon as you start using it to build large and complex JSON objects, you can start getting into problems the other frameworks don't have.

The other frameworks introduce their own dependencies and are based very much on their own design goals, but they are sometimes the better option to use instead of Liferay's JSON implementation.