-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get the Map as it where for JSON or XML #22
Comments
On 16.09.2014 00:40, genuinefafa wrote:
|
English is not my native lang. I'm sorry! 😄 If I do a marshaling of the object, I get a JSON representation of the data contained in the object. In other words, the properties and not the methods. That JSON representation can be converted to a Groovy Map. What I need/want is a Groovy Map as it were marshaled so I can do my own "magic" there. |
On 16.09.2014 20:21, genuinefafa wrote:
|
I think it can be a nice feature to add, since it allow treatment of the object as it were received as JSON. I can do the development of the feature, just point my in the right direction. I belive it should be easy. |
On 16.09.2014 20:43, genuinefafa wrote:
|
Remote Computer RT Case 1RT request for a data Case 2This cases tries to repeat exactly the same behavior without using a remote computer There you are. |
On 16.09.2014 20:50, genuinefafa wrote:
|
genuinefafa, I'm not sure I fully understand your requirements, but maybe this will help. I've not tested this code myself so I'm sure there are bugs :)
Or if you are not using Grails:
A more convenient way to do this in Grails is to use the "request" object which is automatically injected into controllers for you, or you can retrieve it from anywhere via WebUtils.retrieveGrailsWebRequest().request:
I'm not sure how to directly convert an arbitrary object to nested maps/lists without a little more work.
If MyDomainClass is flat (i.e., no other domain class references inside MyDomainClass) then the above will suffice. If it's not flat and you want to end up with nested maps, then you need to walk the object tree yourself, and for each nested object use the ".properties" notation to create a map. A different option which I won't really recommend is this roundabout technique. Maybe you can learn something from it:
|
You also might want to look at:
|
@vahidpaz DataBindingUtils is interesting. But, I refuse to do a Domain Obj->JSON and a JSON->Map. What I ended up doing was this: def domainObject = DomainObject.get(id)
def newMap = [:]
domainObject.domainClass.persistentProperties.each {
if (!it.association) newMap.put it.name, domainObject."${it.name}"
}
def c = newMap + [ id: domainObject.id ] But I don't really like because it doesn't honor the marshalling setup. |
I'm using this plugin and sometimes I need the Map equivalent for a JSON marshaling. Off course, I dont want it to be JSON, but a real Groovy Map.
Is there any way to make that? I know that internally the plugin does a Map to work on (#21).
The text was updated successfully, but these errors were encountered: