Groovier JSON

June 1st, 2007

It’s time for a batch of updates on what’s been going on the Groovy side of Json-lib.

  1. Because JSONObject and JSONArray now implement java.util.Map and java.util.List you can use them as such in Groovy.
  2. JSONObject and JSONArray also implement java.util.Comparable, making it very easy to use the comparison operators (even the spaceship operator). I’ve found a caveat so far, because JSONArray is also a List, the interpreter will throw an Exception stating that it can compare the two values, but you can use compareTo() (as in Java) for the time being.
  3. The leftshift ( << ) operator is supported by JSONObject, the accepted values are Map and List and it works as a shorthand for element(), it follows these rules:
    • If the shifted arg is a Map, it will call putAll() on the object.
    • If the shifted arg is a List and its size == 2, the first element will be the key, and the second will be the value.
    • If the shifted arg is a List and its size > 2, the first element will be the key, the arg will be shifted by 1 and passed as the value (will create a JSONArray because it is a List).
    • Any other type will be discarded, the object will not be affected nor an exception will be thrown.
  4. The JsonGrovyBuilder is finished, I used Grail’s BeansBuilder as a reference instead of going with the traditional way of extending BuilderSupport, this approach allows for the following code to be transformed into the same JSON:

 

  1. def books1 = builder.books {
  2. book = [title: “The Definitive Guide to Grails”, author: “Graeme Rocher”]
  3. book = [title: “The Definitive Guide to Grails”, author: “Graeme Rocher”]
  4. }
  5. def books2 = builder.books {
  6. book = new Book(title: “The Definitive Guide to Grails”,
  7. author: “Graeme Rocher”)
  8. book = new Book(title: “The Definitive Guide to Grails”,
  9. author: “Graeme Rocher”)
  10. }
  11. def books3 = builder.books {
  12. book = {
  13. title = “The Definitive Guide to Grails”
  14. author= “Graeme Rocher”
  15. }
  16. book = {
  17. title = “The Definitive Guide to Grails”
  18. author= “Graeme Rocher”
  19. }
  20. }
  21. def books4 = builder.books {
  22. book {
  23. title = “The Definitive Guide to Grails”
  24. author= “Graeme Rocher”
  25. }
  26. book {
  27. title = “The Definitive Guide to Grails”
  28. author= “Graeme Rocher”
  29. }
  30. }
  31. def books5 = builder.books {
  32. 2.times {
  33. book = {
  34. title = “The Definitive Guide to Grails”
  35. author= “Graeme Rocher”
  36. }
  37. }
  38. }
  39. def books6 = builder.books {
  40. 2.times {
  41. book {
  42. title = “The Definitive Guide to Grails”
  43. author= “Graeme Rocher”
  44. }
  45. }
  46. }
  47. /*
  48. all 6 books variables output the same JSON
  49. {”books”: {
  50. “book”: [{
  51. “title”: “The Definitive Guide to Grails”,
  52. “author”: “Graeme Rocher”
  53. },{
  54. “title”: “The Definitive Guide to Grails”,
  55. “author”: “Graeme Rocher”
  56. }]
  57. }
  58. }
  59. */

Andres Almiray

Tags:



Leave a Comment