3

I have this simple domain class:

class Settings {
static constraints = {
    uid(nullable: false, unique: true)
    person()
}

String uid
Map person
}

and a web UI that update the data using a json request:

{"uid":1234 , person:{"first_name" : "jhon" , "last_name" : "doe"}}

in the controller code:

def json = request.JSON;
def s = new Settings(json);

it seems that s.uid is being set however the s.person Map remains empty. What am I missing?

Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186
  • this is probably a Grails bug http://jira.grails.org/browse/GRAILS-9220?page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel – Shlomi Schwartz Feb 26 '13 at 09:18

3 Answers3

2

You can do something like the following in your controller:

def json = request.JSON;
def s = new Settings(json);
s.person = json.person;

it's ugly, but the data binding doesn't seem to handle nested json

aldrin
  • 4,482
  • 1
  • 33
  • 50
1

If you want that to work you need to convert your structure to this:

{"uid":1234 , "person.first_name": "jhon" , "person.last_name": "doe"}
James Kleeh
  • 12,094
  • 5
  • 34
  • 61
0

If you add this line before instantiating Settings, it will bind recursively.

JSON.use('deep')
Dustin
  • 574
  • 6
  • 13