You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Bertrand Delacretaz <bd...@apache.org> on 2016/07/01 13:19:49 UTC
[RT] JsonBuilder script engine based on Groovy?
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
Re: [RT] JsonBuilder script engine based on Groovy?
Posted by Nicolas Peltier <np...@adobe.com>.
Hi Bertrand,
for the archive, sling pipes [0] can be used for outputing json (and doing complex jcr extraction), with bits of script in it. It’s mostly jcr manipulation though, and there are still a few enhancements to make them more friendly i’m working on for adaptTo.
Nicolas
[0] https://sling.apache.org/documentation/bundles/sling-pipes.html
> On 01 Jul 2016, at 15:19, Bertrand Delacretaz <bd...@apache.org> wrote:
>
> 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
Re: [RT] JsonBuilder script engine based on Groovy?
Posted by Bertrand Delacretaz <bd...@apache.org>.
On Tue, Jul 12, 2016 at 3:16 PM, Robert Munteanu <ro...@apache.org> wrote:
> ...How do you see this code snippet deployed in a Sling instance - is this
> a regular script deployed under /libs|/apps or something else?...
I first thought we'd need a new flavor of the Groovy engine for this,
but it's probably not necessary.
See [1] for an example that works out of the box as a .groovy script.
That example shows that simple Groovy-friendly wrappers that make the
current Sling Resource more "navigable" can be useful, but we might
just implement those via a BindingsValuesProvider, and end up with a
.groovy script like
def mimeTypes =
sling.getService(org.apache.sling.commons.mime.MimeTypeService.class)
builder . "${resource.name}" {
date new java.util.Date()
"mime-type" mimeTypes.getMimeType(request.requestPathInfo.extension)
title resource.props.title
description resource.props.description
}
-Bertrand
[1] https://gist.githubusercontent.com/bdelacretaz/dc0a0ec4fada97fa2151bf3b74aa4788
Re: [RT] JsonBuilder script engine based on Groovy?
Posted by Robert Munteanu <ro...@apache.org>.
On Fri, 2016-07-01 at 15:19 +0200, Bertrand Delacretaz wrote:
> Hi,
>�
> 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 }
> }
>
>�
Nice and concise, looks good!
How do you see this code snippet deployed in a Sling instance - is this
a regular script deployed under /libs|/apps or something else?
Thanks,
Robert
Re: [RT] JsonBuilder script engine based on Groovy?
Posted by Chetan Mehrotra <ch...@gmail.com>.
On Fri, Jul 1, 2016 at 6:49 PM, Bertrand Delacretaz
<bd...@apache.org> wrote:
> 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.
+1 for using Groovy to generate such output. It removes quite a bit of
boilerplate and simplifies stuff quite a bit
Chetan Mehrotra