Advertisement
dmahapatro

DeepJson

Jun 28th, 2013
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 4.21 KB | None | 0 0
  1. class Parent {
  2.         String name
  3.         Integer age
  4.         Date dob
  5.    
  6.         Child child
  7.     }
  8.    
  9.     class Child {
  10.         String name
  11.         Integer age
  12.         Date dob
  13.         static hasMany = [qualities: Quality]
  14.         GrandChild grandChild
  15.     }
  16.    
  17.     class GrandChild {
  18.         String name
  19.         Integer noOfTeeth
  20.         Date dob
  21.         Address address
  22.     }
  23.    
  24.     class Quality {
  25.         Integer key
  26.         String value
  27.     }
  28.    
  29.     class Address {
  30.         String street
  31.         String apt
  32.         String city
  33.         Integer zip
  34.         String country
  35.     }
  36.    
  37.     //Parent Controller
  38.     import grails.converters.JSON
  39.     import grails.converters.XML
  40.     def index() {
  41.             JSON.use('deep')
  42.             render Parent.all as JSON
  43.    
  44.             //XML.use('deep')
  45.             //render Parent.all as XML
  46.         }
  47.  
  48.     //Bootstrap
  49.         def fastQuality = new Quality(key: 1, value: "Fast").save()
  50.         def furiousQuality = new Quality(key: 2, value: "Furious").save()
  51.         def programmerQuality = new Quality(key: 3, value: "Programmer").save()
  52.  
  53.         def address = new Address(street: 'Main St', apt: '123', zip: 43230, city: 'Maple Shade', country: 'USA').save()
  54.  
  55.         def gc = new GrandChild(name: "GrandChild", noOfTeeth: 10, dob: new Date(), address: address).save()
  56.  
  57.         def child =  new Child(name: "Child", age: 30, dob: new Date(), grandChild: gc).save()
  58.         [fastQuality, furiousQuality, programmerQuality].each{child.addToQualities(it)}
  59.  
  60.         def parent = new Parent(name: "Parent", age: 70, dob: new Date(), child: child).save(flush: true, failOnError: true)
  61.  
  62.  
  63. //JSON output
  64. [
  65.     {
  66.         "class": "com.example.Parent",
  67.         "id": 1,
  68.         "age": 70,
  69.         "child": {
  70.             "class": "com.example.Child",
  71.             "id": 1,
  72.             "age": 30,
  73.             "dob": "2013-06-28T05:53:58Z",
  74.             "grandChild": {
  75.                 "class": "com.example.GrandChild",
  76.                 "id": 1,
  77.                 "address": {
  78.                     "class": "com.example.Address",
  79.                     "id": 1,
  80.                     "apt": "123",
  81.                     "city": "Maple Shade",
  82.                     "country": "USA",
  83.                     "street": "Main St",
  84.                     "zip": 43230
  85.                 },
  86.                 "dob": "2013-06-28T05:53:58Z",
  87.                 "name": "GrandChild",
  88.                 "noOfTeeth": 10
  89.             },
  90.             "name": "Child",
  91.             "qualities": [
  92.                 {
  93.                     "class": "com.example.Quality",
  94.                     "id": 1,
  95.                     "key": 1,
  96.                     "value": "Fast"
  97.                 },
  98.                 {
  99.                     "class": "com.example.Quality",
  100.                     "id": 2,
  101.                     "key": 2,
  102.                     "value": "Furious"
  103.                 },
  104.                 {
  105.                     "class": "com.example.Quality",
  106.                     "id": 3,
  107.                     "key": 3,
  108.                     "value": "Programmer"
  109.                 }
  110.             ]
  111.         },
  112.         "dob": "2013-06-28T05:53:58Z",
  113.         "name": "Parent"
  114.     }
  115. ]
  116.  
  117. //XML Output
  118. <?xml version="1.0" encoding="UTF-8"?>
  119. <list>
  120.   <parent id="1">
  121.     <age>70</age>
  122.     <child id="1">
  123.       <age>30</age>
  124.       <dob>2013-06-28 02:09:06.793 EDT</dob>
  125.       <grandChild id="1">
  126.         <address id="1">
  127.           <apt>123</apt>
  128.           <city>Maple Shade</city>
  129.           <country>USA</country>
  130.           <street>Main St</street>
  131.           <zip>43230</zip>
  132.         </address>
  133.         <dob>2013-06-28 02:09:06.782 EDT</dob>
  134.         <name>GrandChild</name>
  135.         <noOfTeeth>10</noOfTeeth>
  136.       </grandChild>
  137.       <name>Child</name>
  138.       <qualities>
  139.         <quality id="1">
  140.           <key>1</key>
  141.           <value>Fast</value>
  142.         </quality>
  143.         <quality id="3">
  144.           <key>3</key>
  145.           <value>Programmer</value>
  146.         </quality>
  147.         <quality id="2">
  148.           <key>2</key>
  149.           <value>Furious</value>
  150.         </quality>
  151.       </qualities>
  152.     </child>
  153.     <dob>2013-06-28 02:09:06.807 EDT</dob>
  154.     <name>Parent</name>
  155.   </parent>
  156. </list>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement