Hi,

I'm looking at simpler ways of customizing Sling's JSON output - one
can of course write servlets to do that, but as it's a common use case
it would be good to be able to do that with less code.

I've been playing with the Groovy JsonBuilder and quite like it - see
the Gist at [1] for an example which explores creating a simple script
engine for "JSON builder scripts" which can use the full power of
Groovy while having very little code to write for simple use cases.

Once the required engine is implemented, which is just a few lines of
code to wrap the script and let Groovy compile and run it, a script
like

// r is the current resource, in a Groovy wrapper
// that provides some navigation/aggregation functions
root {
  def kids = r.child("sub").children("kid", 3)
  path r.path
  title r.props.title
  childCount kids.size
  childPaths kids.collect { r -> path:r.path }
}

would produce output like

{
 "root": {
  "path": "/currentResource",
  "title": "This is /currentResource",
  "childCount": 3,
  "childPaths": [
   "/currentResource/sub/kid_1",
   "/currentResource/sub/kid_2",
   "/currentResource/sub/kid_3"
  ]
 }
}

WDYT?
-Bertrand

[1] https://gist.github.com/bdelacretaz/9cbb07de84f2ae74e6a54438522686ff

Reply via email to