Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Why JsonGroovyBuilder print []

Help
2009-02-10
2013-04-29
  • jameslorenzen
    jameslorenzen
    2009-02-10

    When i do a simple example using the JsonGroovyBuilder and print it out, it appears it uses [] instead of {}.

    For example
    def b = new JsonGroovyBuilder()
    def books6 = b.json {
          formData = {
              issues = "issues"
          }
          time = 5001
    }
    println "json $books6"

    prints
    json ["formData":["issues":"issues"], "time":5001]

    When I expect it to print out
    json {"formData":{"issues":"issues"}, "time":5001}

     
    • aalmiray
      aalmiray
      2009-02-10

      Hi James,

      The problem lies in that JSONObject implements Map.
      Maps in Groovy are automatically pretty printed without calling the default toString() method, which means that in order to get the desired output you must call toString() directly.

      Cheers,
      Andres

       
    • jameslorenzen
      jameslorenzen
      2009-02-11

      Dang it.  I thought the same thing last night but I guess it was getting late. I thought I tried that thinking the same thing, but now I tried it again and it does print out the {}. Oh well. I appreciate the quick feedback and the comments on my blog. Stay tuned. I plan on writing about this as well. You might think about updating the documentation on json lib. I could see this getting developers like me hung up early and not give json lib another try.