Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6540

JsonBuilder does not support "multiple roots" in builder syntax

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Not A Problem
    • 2.2.1
    • None
    • JSON

    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

      Attachments

        Activity

          People

            guillaume Guillaume Sauthier
            vampire Björn Kautler
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: