protected JSONObject toJSONObject(Resource resource) throws JSONException, ServletException {
JSONObject config = null;
Node node = resource.adaptTo(Node.class);
if (node != null) {
JsonItemWriter writer = new JsonItemWriter(null);
StringWriter string = new StringWriter();
try {
writer.dump(node, string, -1);
} catch (RepositoryException e) {
throw new ServletException(e);
}
config = new JSONObject(string.toString());