Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Parent {
- String name
- Integer age
- Date dob
- Child child
- }
- class Child {
- String name
- Integer age
- Date dob
- static hasMany = [qualities: Quality]
- GrandChild grandChild
- }
- class GrandChild {
- String name
- Integer noOfTeeth
- Date dob
- Address address
- }
- class Quality {
- Integer key
- String value
- }
- class Address {
- String street
- String apt
- String city
- Integer zip
- String country
- }
- //Parent Controller
- import grails.converters.JSON
- import grails.converters.XML
- def index() {
- JSON.use('deep')
- render Parent.all as JSON
- //XML.use('deep')
- //render Parent.all as XML
- }
- //Bootstrap
- def fastQuality = new Quality(key: 1, value: "Fast").save()
- def furiousQuality = new Quality(key: 2, value: "Furious").save()
- def programmerQuality = new Quality(key: 3, value: "Programmer").save()
- def address = new Address(street: 'Main St', apt: '123', zip: 43230, city: 'Maple Shade', country: 'USA').save()
- def gc = new GrandChild(name: "GrandChild", noOfTeeth: 10, dob: new Date(), address: address).save()
- def child = new Child(name: "Child", age: 30, dob: new Date(), grandChild: gc).save()
- [fastQuality, furiousQuality, programmerQuality].each{child.addToQualities(it)}
- def parent = new Parent(name: "Parent", age: 70, dob: new Date(), child: child).save(flush: true, failOnError: true)
- //JSON output
- [
- {
- "class": "com.example.Parent",
- "id": 1,
- "age": 70,
- "child": {
- "class": "com.example.Child",
- "id": 1,
- "age": 30,
- "dob": "2013-06-28T05:53:58Z",
- "grandChild": {
- "class": "com.example.GrandChild",
- "id": 1,
- "address": {
- "class": "com.example.Address",
- "id": 1,
- "apt": "123",
- "city": "Maple Shade",
- "country": "USA",
- "street": "Main St",
- "zip": 43230
- },
- "dob": "2013-06-28T05:53:58Z",
- "name": "GrandChild",
- "noOfTeeth": 10
- },
- "name": "Child",
- "qualities": [
- {
- "class": "com.example.Quality",
- "id": 1,
- "key": 1,
- "value": "Fast"
- },
- {
- "class": "com.example.Quality",
- "id": 2,
- "key": 2,
- "value": "Furious"
- },
- {
- "class": "com.example.Quality",
- "id": 3,
- "key": 3,
- "value": "Programmer"
- }
- ]
- },
- "dob": "2013-06-28T05:53:58Z",
- "name": "Parent"
- }
- ]
- //XML Output
- <?xml version="1.0" encoding="UTF-8"?>
- <list>
- <parent id="1">
- <age>70</age>
- <child id="1">
- <age>30</age>
- <dob>2013-06-28 02:09:06.793 EDT</dob>
- <grandChild id="1">
- <address id="1">
- <apt>123</apt>
- <city>Maple Shade</city>
- <country>USA</country>
- <street>Main St</street>
- <zip>43230</zip>
- </address>
- <dob>2013-06-28 02:09:06.782 EDT</dob>
- <name>GrandChild</name>
- <noOfTeeth>10</noOfTeeth>
- </grandChild>
- <name>Child</name>
- <qualities>
- <quality id="1">
- <key>1</key>
- <value>Fast</value>
- </quality>
- <quality id="3">
- <key>3</key>
- <value>Programmer</value>
- </quality>
- <quality id="2">
- <key>2</key>
- <value>Furious</value>
- </quality>
- </qualities>
- </child>
- <dob>2013-06-28 02:09:06.807 EDT</dob>
- <name>Parent</name>
- </parent>
- </list>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement