Grails - Initiating domain class using JSON
Asked Answered
C

3

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?

Calumet answered 25/2, 2013 at 12:49 Comment(1)
this is probably a Grails bug jira.grails.org/browse/…Calumet
B
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

Bevel answered 25/2, 2013 at 16:51 Comment(1)
Thanks for the reply ... this is odd, I will be stubborn and continue the search for a solutionCalumet
I
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"}
Isopiestic answered 25/2, 2013 at 13:22 Comment(1)
thanks for the reply however the UI JSON is a given and cannot be changed. I'm sure there is a smoother solution :)Calumet
I
0

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

JSON.use('deep')
Intersidereal answered 4/2, 2016 at 22:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.