Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert grails domain object to JSON and manipulate it

Tags:

json

grails

I have a grails object that I am converting using def json = object as JSON. After I have it converted I want to add one more property called pin to the JSON which looks like the following.

[location:[lat:23.03, lon:72.58]]

Only way to do this so far seems like following

  1. Serialize the DomainClass to JSON using grails.converters.json
  2. Convert the JSON to string
  3. Create JSONBoject using the string from Step 2
  4. Add the property
  5. Convert it back to String

Any other way to do this using grails.converters.json? I have tried using Gson but I do not want to go that route because I am getting many Circular Reference Errors

like image 296
Sap Avatar asked Jan 21 '14 10:01

Sap


1 Answers

Try this:

domainInstance.properties + [pin: pinInstance] as JSON
like image 194
Alexander Tokarev Avatar answered Oct 23 '22 11:10

Alexander Tokarev