Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Not A Problem
-
2.2.1
-
None
Description
I tried to create the following JSON string with JsonBuilder:
{ "globalId": "system=17003", "object": { "url": "http://foo/CC-17003", "title": "Link Title", "icon": { "url16x16": "http://foo/favicon.ico", "title": "Link Title" } } }
But I found no way to use the builder syntax to create it, I tried stuff like
new JsonBuilder().root { globalId: 'system=17003', object: { url 'http://foo/CC-17003' title 'Link Title' icon { url16x16 'http://foo/favicon.ico' title 'Link Title' } } }.toString()
or
new JsonBuilder(globalId: 'system=17003').object { url 'http://foo/CC-17003' title 'Link Title' icon { url16x16 'http://foo/favicon.ico' title 'Link Title' } }.toString()
or
def jsonBuilder = new JsonBuilder() jsonBuilder.globalId 'system=17003' jsonBuilder.object { url 'http://foo/CC-17003' title 'Link Title' icon { url16x16 'http://foo/favicon.ico' title 'Link Title' } } jsonBuilder.toString()
but was not able to produce the needed result.
What I do now is the following workaround which works:
new JsonBuilder( [ globalId: 'system=17003', object: { url 'http://foo/CC-17003' title 'Link Title' icon { url16x16 'http://foo/favicon.ico' title 'Link Title' } } ]).toString()
But this is not too nice and groovy builder style.
If I just didn't see how to do it, please improve the documentation for JsonBuilder. If it is really not possible somehow except with my workaround, please enhance the JsonBuilder to support building the desired result which is pure valid JSON and is also consumed by JsonSlurper without any problems.
PS: The first three examples could be syntactically not 100% ok as I wrote them here on the top of my head but show what I basically tried